Reputation: 43
So I'm trying to test my feature files on steps class. I'm having an issue asserting multiple common scenarios linked to the same step definition.
Scenario: Validate menu items
So these 2 "And" steps are validating in single step definition. I'm trying to assert the "And" with two different locators with from the POM classes.
@And("user should see {string} text")
public void userShouldSeeText(String key) {
Assert.assertEquals(key, helpMeSearchPage.mainHeader.getText());
Assert.assertEquals(key, helpMeSearchPage.subHeader.getText());
Assert.assertEquals(key, helpMeSearchQaPage.header.getText());
Assert.assertEquals(key, helpMeSearchQaPage.subHeading.getText());
}
But it doesn't work, my each Assert line runs twice already and I couldn't find the solution. I'm trying to learn JUnit Cucumber but having issue on this step.
Upvotes: 1
Views: 287
Reputation: 17612
Cucumber uses regex to match the step definitions. So both of these steps:
And user should get "XXXX" text
And user should get "Some random text" text
match the step definition:
@And("user should get {string} text")
It's therefore running the whole step twice, because it matches twice.
(Note I've changed see
to get
in the step above, compared to yours - if the problem is that it isn't matching, that's your issue.)
If you want it to match two steps differently, you can either use a different regex expression for matching, or you can change the steps and add a different definition. For instance:
And user should get "XXXX" text on the main page
And user should get "Some random text" text in QA help
will match
@And("user should get {string} text on the main page")
@And("user should get {string} text in QA help")
respectively.
Note that in general, we try not to talk about clicking buttons and seeing text in pages; this usually leads to very detailed scenarios with too many steps and which are hard to maintain. Instead you might want to phrase the steps in terms of what the user achieves, for instance:
Given the user is on the homepage
When they select "Peaches" from the menu
Then they should see more information about "Peaches"
And relevant allergy warnings for "Peaches".
This makes it more about what the customer is trying to do with your site than the implementation details, keeping the language in the problem rather than the solution space. The automation that you use to verify what information about "Peaches"
looks like can remain the same.
Upvotes: 1