Cucumber Annotations
1. @Given
Explanation: Used to define a precondition or a setup step in a scenario.
Usage: Represents the initial context of the system.
Syntax:
@Given("the user is on the login page")
public void userIsOnLoginPage() {
// Code to navigate to the login page
2. @When
Explanation: Represents an action taken by the user or system.
Usage: Used to describe an event that triggers a change in the state of the application.
Syntax:
@When("the user enters valid credentials")
public void userEntersValidCredentials() {
// Code to enter credentials
3. @Then
Explanation: Used to describe the expected outcome or result of an action.
Usage: Defines the expected behavior of the system after the action has been performed.
Syntax:
@Then("the user should be redirected to the dashboard")
public void userIsRedirectedToDashboard() {
// Code to verify the redirection
4. @And
Explanation: Used to add additional conditions to a step.
Usage: Helps to keep the steps concise and readable by combining steps.
Syntax:
@And("the user clicks on the login button")
public void userClicksLoginButton() {
// Code to click the login button
5. @But
Explanation: Used to add a negative condition.
Usage: Indicates a contrasting action or outcome.
Syntax:
@But("the user is not logged in")
public void userIsNotLoggedIn() {
// Code to check if the user is not logged in
Cucumber Hooks
1. @Before
Explanation: Executes before each scenario.
Usage: Used for setup activities, like initializing browser instances or setting test data.
Syntax:
@Before
public void setUp() {
// Code to set up test environment
2. @After
Explanation: Executes after each scenario.
Usage: Used for cleanup activities, like closing the browser or clearing test data.
Syntax:
@After
public void tearDown() {
// Code to clean up test environment
3. @BeforeStep
Explanation: Executes before each step in a scenario.
Usage: Can be used for logging or performing actions before each step.
Syntax:
@BeforeStep
public void beforeStep() {
// Code to execute before each step
4. @AfterStep
Explanation: Executes after each step in a scenario.
Usage: Can be used for logging or taking screenshots after each step.
Syntax:
@AfterStep
public void afterStep() {
// Code to execute after each step
Cucumber Data Tables
1. Data Tables
Explanation: Used to pass multiple data values in a structured format (like a table).
Usage: Allows passing of data to step definitions in a tabular format.
Syntax:
@Given("the following users exist")
public void usersExist(DataTable dataTable) {
List<Map<String, String>> users = dataTable.asMaps(String.class, String.class);
for (Map<String, String> user : users) {
// Code to process user data
}
Example of a Data Table in Gherkin
gherkin
Copy code
Given the following users exist
| username | password |
| user1 | pass1 |
| user2 | pass2 |
Cucumber Scenario Outline
1. Scenario Outline
Explanation: Used to run the same scenario with different sets of data.
Usage: Helps in reducing redundancy when testing similar scenarios.
Syntax:
Scenario Outline: User login
Given the user is on the login page
When the user enters "<username>" and "<password>"
Then the user should be redirected to the dashboard
Examples:
| username | password |
| user1 | pass1 |
| user2 | pass2 |
Cucumber Tags
1. Tags
Explanation: Used to categorize scenarios for selective execution.
Usage: Allows running specific scenarios based on tags.
Syntax:
@smoke
Scenario: User login
Given the user is on the login page
When the user enters valid credentials
Then the user should be redirected to the dashboard
Running Tagged Scenarios
Command-line option: --tags @smoke
Cucumber Report Generation
1. Generate HTML Reports
Explanation: Cucumber can generate various reports after test execution.
Usage: Use plugins like Cucumber Reporting or ExtentReports for generating reports.
Syntax: Add the necessary dependencies in your pom.xml for Maven projects or build.gradle
for Gradle projects.
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>5.7.0</version>
</dependency>
Running Cucumber Tests
1. JUnit Runner
Explanation: Cucumber tests can be run using a JUnit runner.
Usage: Integrates Cucumber with JUnit for running tests.
Syntax:
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features", glue = "stepDefinitions")
public class RunCucumberTest {
}
2. TestNG Runner
Explanation: Cucumber tests can also be run using TestNG.
Usage: Integrates Cucumber with TestNG for running tests.
Syntax:
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(features = "src/test/resources/features", glue = "stepDefinitions")
public class RunCucumberTest extends AbstractTestNGCucumberTests {
Cucumber Options
1. CucumberOptions
Explanation: Used to configure various settings for Cucumber.
Usage: Defines the features, glue code, and plugin options.
Syntax:
@CucumberOptions(
features = "src/test/resources/features",
glue = "stepDefinitions",
plugin = {"pretty", "html:target/cucumber-reports.html"}