Cucumber Java Selenium
Cucumber Java Selenium
Feature
Scenario
Each feature contains the required number of tests to test the feature.
Each test is named as a Scenario.
For example, feature login functionality can contain two scenarios, first for a
successful login and second for unsuccessful login.
Feature: Login
Given
Then
This keyword refers to the outcome of the previous step or upcoming
action.
But
This keyword is used to add negative conditions.
And
This keyword is used to add more conditions into your steps.
Background
This keyword defines the steps common to all tests in the feature file.
For example, Navigation to Home Page, click on the Login, Enter
Username and Password, Click on Submit button are the common steps
in almost all web applications.
Examples:
| email | password |
| name1 | pass 1 |
| name2 | pass 2 |
Tags
It is a concept used in cucumber. We can add tags to features and
scenarios.
By using @ symbol, we can add tags.
Ex:
@Login Test
Feature: As a user, I want to create credentials
@Smoke Test
Scenario: Successful login using valid account
Given Login form in login page
When Submit email and password
Then Success login to home page with displaying account button
Step Generator
In cucumber we have Step Generator used to implement the script
based on Feature file.
After completion of feature file Right click and run as cucumber Feature
Then it will give us Script based on Feature file in console.
Copy the Script and implement it in Step Generator.
In step definition we have Regular Expressions which are ^ and $
^ indicates Starting
$ indicates Ending
Example:
@Given("^login page$")
public void login_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Submit emailid and password$")
public void submit_emailid_and_password() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^Success login to home page$")
public void success_login_to_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Test Runner
Test Runner is used to run multiple feature files and generate reports in
different formats.
We can run required features or scenarios from test runner.
Example:
@RunWith(Cucumber.class)
@CucumberOptions(
features = "./src/test/resources/Features/file.feature",
glue = "stepDefinations",
tags = {"@LoginTest"},
plugin = {
"pretty",
"html:target/cucumber-reports/cucumber-pretty",
"json:target/cucumber-reports/CucumberTestReport.json",
"junit:target/cucumber-reports/CucumberTestReport.xml"
}
)
public class TestRunner {
}
We need to create a new class for Test Runner.
Above the class we need to write
@RunWith(Cucumber.class)
@CucumberOptions(
features = "Feature file path",
glue = "StepDefinations package name",
Tags= {"@Tag name"},
plugin = {
"pretty", // Format
"html:target/cucumber-reports/cucumber-pretty", // generate Html report
"json:target/cucumber-reports/CucumberTestReport.json", // Json Report
"junit:target/cucumber-reports/CucumberTestReport.xml" // Junit report
}