尽管在功能文件calc.feature中添加了Background,但仍获得Cucumber.runtime.DuplicateStepDefinitionException

Nit*_*tal 2 java bdd selenium spring cucumber

Cucumber Selenium使用以下命令运行测试时出现以下错误Spring Boot

我已经添加Background了功能文件。不确定如何概括其中传递的参数。

请指导。

错误:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.TestRunner
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.388 sec <<< FAILURE! - in com.example.TestRunner
initializationError(com.example.TestRunner)  Time elapsed: 0.004 sec  <<< ERROR!
cucumber.runtime.DuplicateStepDefinitionException: Duplicate step definitions in com.example.stepdefs.GoogleCalcStepDefinition.I_enter_in_search_textbox(String) in file:/I:/pet-projects/junit-cucumber-demo/target/test-classes/ and com.example.stepdefs.GoogleCalcSte
pDefinition.I_enter_in_search_textbox2(String) in file:/I:/pet-projects/junit-cucumber-demo/target/test-classes/


Results :

Tests in error:
  TestRunner.initializationError » DuplicateStepDefinition Duplicate step defini...

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[ERROR] There are test failures.
[INFO] About to generate Cucumber report.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.281 s
[INFO] Finished at: 2019-04-12T16:50:23-04:00
[INFO] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

计算功能

Feature: Check addition in Google calculatorcontent
  In order to verify that Google calculator work correctly
  As a user of Google
  I should be able to get correct addition result

  Background: Do some arithmetic on Google
    Given I open Google

  Scenario: Addition
    When I enter "2+2" in search textbox
    Then I should get result as "4"

  Scenario: Multiplication
    When I enter "5*5" in search textbox
    Then I should get result as "25"
Run Code Online (Sandbox Code Playgroud)

GoogleCalcStepDefinition.java

@Ignore
public class GoogleCalcStepDefinition extends DemoApplicationTests {

    WebDriver driver;
    GoogleSearchPage googleSearchPage;

    @Given("^I open Google$")
    public void I_open_google() {
        this.driver = BrowserConfig.getWebDriver();
        this.googleSearchPage = PageFactory.initElements(driver, GoogleSearchPage.class);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("https://www.google.com");
    }

    @When("^I enter \"([^\"]*)\" in search textbox$")
    public void I_enter_in_search_textbox(String additionTerms) {
        googleSearchPage.searchBox.sendKeys(additionTerms); //passing 2+2 here
        googleSearchPage.searchBtn.click();
    }

    @Then("^I should get result as \"([^\"]*)\"$")
    public void I_should_get_correct_result(String expectedResult) {
        String result = googleSearchPage.calculatorTextBox.getText();
        assertEquals(result, expectedResult); //Verify that result of 2+2 is 4
        BrowserConfig.releaseResources(driver);
    }

    @When("^I enter \"([^\"]*)\" in search textbox$")
    public void I_enter_in_search_textbox2(String multiplicationTerms) {
        googleSearchPage.searchBox.sendKeys(multiplicationTerms); //passing 5*5 here
        googleSearchPage.searchBtn.click();
    }

    @Then("^I should get result as \"([^\"]*)\"$")
    public void I_should_get_correct_result2(String expectedResult) {
        String result = googleSearchPage.calculatorTextBox.getText();
        assertEquals(result, expectedResult); //Verify that result of 5*5 is 25
        BrowserConfig.releaseResources(driver);
    }
}   
Run Code Online (Sandbox Code Playgroud)

DemoApplicationTests.java

@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class DemoApplicationTests {

}
Run Code Online (Sandbox Code Playgroud)

GoogleSearchPage.java

公共类GoogleSearchPage {

    @FindBy(name = "q")
    public WebElement searchBox;
    @FindBy(name = "btnK")
    public WebElement searchBtn;
    @FindBy(id = "cwos")
    public WebElement calculatorTextBox;

}
Run Code Online (Sandbox Code Playgroud)

TestRunner.java

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty", "json:target/cucumber-reports/cucumber.json"},
        glue = {"com.example.stepdefs"},
        features = {"src/test/resources/features"})
public class TestRunner {

}
Run Code Online (Sandbox Code Playgroud)

Nat*_*l C 5

除数值外,您有两个相同的小黄瓜步骤,并且使用Regex对数值进行了参数设置,使其完全相同。因此,尽管小黄瓜中的两个步骤是唯一的,但步骤定义中的绑定是重复的:

@Then("^I should get result as \"([^\"]*)\"$")
Run Code Online (Sandbox Code Playgroud)

@Then("^I should get result as \"([^\"]*)\"$")
Run Code Online (Sandbox Code Playgroud)

这两个When步骤相同。您可以用与小黄瓜相同的硬编码值替换正则表达式(可能不是您想要的值),或者只是删除重复项,因为这些步骤看起来像它们在正确地处理了输入参数。一旦删除了公仔,小黄瓜中的两个步骤都将映射到剩下的单个步骤def。