Tutorial 20 – Integrate Cucumber with Selenium – Part 7
What you will Learn :
Create Runner file
Execute Runner file
One important note
Cucumber report
Introduce an error to generate a screenshot
Exercise
This is continuation of previous tutorial.
Create Runner file
Let us create MyRunnerTest.java inside the TestRunner package. Make sure that the name of the file has a suffix ‘Test’ otherwise there would be problems in execution (we have already studied this in one of our previous tutorial)
Like studied earlier, we will add cucumber options as seen below
Execute Runner file
The 3 scenarios in our feature file get executed one by one
Let us now execute our runner file
Notice below that the execution starts and JUnit console points to execution of our first scenario
The browser opens and launches website. The login page title is verified using assertion
Below points towards execution of 2nd scenario
Browser gets launched and ‘Forgot password link’ gets asserted
Below points towards execution of 3rd scenario
The username/password is entered and scropt clicks the ‘Sign In’ button. The home page title is asserted
Below shows that all the 3 scenarios were executed successfully
Below is complete console o/p
Why is it that a new chrome browser session starts before starting every scenario? This is because of the @Before methods that we have written in ApplicationHooks file
One important note
There is one more point worth mentioning over here. Look at steps number 5 and 6 in our feature file below. These steps start with ‘When’ and ‘Then’.
As expected, our step definition file (right hand side) too starts with ‘@When’ and ‘@Then’ respectively for the steps number 5 and 6 in our feature file.
But the notable point to note here is that, the steps number 17 and 18 in our feature file do NOT start with ‘When’ and ‘Then’. These start with ‘Then’ and ‘And’. We do NOT have corresponding @Then and @And in our step definition file. Then how come these steps (17 and 18) are getting executed at runtime? Why are we not seeing any error at runtime saying that “could not find any step definition for these 2 steps…” ?
To answer this question, what matters is the regular expression inside the annotations of step definition file. So if you look at below, the regex "user gets the title of the page" and "page title should be {string}" in step definition file match with the description of steps in the feature file. So that is what matters
Cucumber report
Let us copy the cucumber report link seen in console
Paste the link in a browser, observe that all the scenarios are Pass
Introduce an error to generate a screenshot
In our hooks file (see below), we have @After hook that would take a screenshot if the test fails
So to test this, let us introduce an error in forgotPwdLink locator, let us add 123 as seen below.
So this would fail isForgotpwdLinkPresent() action
Let us re-run our runner file and copy the cucumber report link from console
Paste it in browser. Notice that the below scenario fails and an image is attached
Let us click ‘Attached image’, notice below the screenshot. Hence @After hook that takes a screenshot if the test fails is working fine
You can delete the report
Exercise
As an exercise, execute the scenarios in firefox browser.
This ends our core Cucumber BDD series.
Thank you for reading!