【问题标题】:Getting cucumber.runtime.DuplicateStepDefinitionException despite adding Background in the feature file calc.feature尽管在功能文件 calc.feature 中添加了背景,但仍获得 cucumber.runtime.DuplicateStepDefinitionException
【发布时间】:2019-09-03 16:15:12
【问题描述】:

我在使用Spring Boot 运行我的Cucumber Selenium 测试时遇到以下错误

我已经在功能文件中添加了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] ------------------------------------------------------------------------

calc.feature

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"

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);
    }
}   

DemoApplicationTests.java

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

}

GoogleSearchPage.java

公共类 GoogleSearchPage {

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

}

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 {

}

【问题讨论】:

    标签: java spring selenium cucumber bdd


    【解决方案1】:

    除了数值之外,您有两个相同的小黄瓜步骤,并且使用正则表达式将数值参数化,使它们完全相同。因此,虽然小黄瓜中的两个步骤是唯一的,但步骤定义中的绑定是重复的:

    @Then("^I should get result as \"([^\"]*)\"$")
    

    @Then("^I should get result as \"([^\"]*)\"$")
    

    两个When 步骤相同。您可以将 Regex 替换为与小黄瓜相同的硬编码值(可能不是您想要的),或者只是删除重复项,因为这些步骤看起来像是在正确处理输入参数。删除 dup 后,gherkin 中的两个步骤都将映射到剩余的单个步骤 def。

    【讨论】:

    • 感谢您解决了我的问题。我将在下面发布更新的答案,以便其他人将来可以参考。
    【解决方案2】:

    根据@Nathaniel 的回复,更新的代码库有助于解决问题。

    calc.feature(无需更改)

    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 input) {
            googleSearchPage.searchBox.sendKeys(input); //passing 2+2 and 5*5 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 and 5*5 is 25
            BrowserConfig.releaseResources(driver);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-15
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多