I am getting following error while running my Cucumber
Selenium
tests using Spring Boot
I have already added Background
in the feature file. Not sure how to generalize the parameters that are getting passed in it.
Please guide.
Error:
-------------------------------------------------------
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
public class 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 {
}
You have two gherkin steps that are identical save for the numeric value, and the numeric value was parameterized out with Regex, leaving them completely identical. So while the two steps in the gherkin are unique, the bindings in the step definition are duplicates:
@Then("^I should get result as \"([^\"]*)\"$")
and
@Then("^I should get result as \"([^\"]*)\"$")
Same for the two When
steps. You can either replace the Regex with the same hardcoded values as the gherkin (probably not what you want), or simply delete the duplicates since the steps look like they're properly handling the input argument. Once the dups are deleted, the two steps in the gherkin will both map to the single remaining step def.