gpt4 book ai didi

java - 尽管在功能文件 calc.feature 中添加了背景,但仍获取 cucumber.runtime.DuplicateStepDefinitionException

转载 作者:行者123 更新时间:2023-11-30 06:02:09 25 4
gpt4 key购买 nike

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

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

请指导。

错误:

-------------------------------------------------------
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] ------------------------------------------------------------------------

计算功能

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

公共(public)类 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 {

}

最佳答案

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

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

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

两个 When 步骤相同。您可以使用与小 cucumber 相同的硬编码值替换正则表达式(可能不是您想要的),或者简单地删除重复项,因为这些步骤看起来像是正确处理输入参数。删除重复项后,小 cucumber 中的两个步骤都将映射到剩余的单个步骤 def。

关于java - 尽管在功能文件 calc.feature 中添加了背景,但仍获取 cucumber.runtime.DuplicateStepDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55659393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com