使用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] ------------------------------------------------------------------------
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"
Google calc step definition.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);
}
}
demo application tests.Java
@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class DemoApplicationTests {
}
Google search Page.Java
公共类GoogleSearchPage {
@FindBy(name = "q")
public WebElement searchBox;
@FindBy(name = "btnK")
public WebElement searchBtn;
@FindBy(id = "cwos")
public WebElement calculatorTextBox;
}
test runner.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 {
}
除了数值之外,您有两个相同的小黄瓜步骤,并且使用正则表达式对数值进行参数化,使它们完全相同。因此,虽然小黄瓜中的两个步骤是唯一的,但步骤定义中的绑定是重复的:
@Then("^I should get result as \"([^\"]*)\"$")
和
@Then("^I should get result as \"([^\"]*)\"$")
两个When
步骤相同。您可以使用与小黄瓜相同的硬编码值替换正则表达式(可能不是您想要的),或者只是删除重复项,因为步骤看起来像是正确处理输入参数。删除重复后,小黄瓜中的两个步骤将映射到单个剩余步骤def。
基于@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);
}
}