1.
@Given
Use: Defines the initial context of the test. It sets up the preconditions or starting state.
@Given("the user is on the login page")
public void userOnLoginPage() {
// Code to navigate to login page
2. @When
Use: Describes the action that the user performs or an event that occurs.
@When("the user enters valid credentials")
public void userEntersCredentials() {
// Code to enter username and password
3. @Then
Use: Describes the expected outcome or result after the action is performed.
@Then("the user should be redirected to the dashboard")
public void userRedirectedToDashboard() {
// Code to verify redirection
4. @And
Use: Used for additional conditions or actions that follow @Given, @When, or @Then.
@And("the user clicks the login button")
public void userClicksLoginButton() {
// Code to click login button
5. @But
Use: Used to express a negation or alternative scenario related to the previous step.
@But("the user does not see any error messages")
public void userDoesNotSeeErrorMessages() {
// Code to check for absence of error messages
6. @Scenario
Use: Represents a single scenario in the feature file.
@Scenario
public void scenario() {
// Code for scenario execution
7. @Before
Use: Runs before each scenario. It is often used for setup tasks.
@Before
public void setup() {
// Code to set up test environment
8. @After
Use: Runs after each scenario. It is often used for cleanup tasks.
@After
public void tearDown() {
// Code to clean up after tests
9. @BeforeStep
Use: Executes before each step in the scenario. Useful for logging or reporting.
@BeforeStep
public void beforeStep() {
// Code to execute before each step
}
10. @AfterStep
Use: Executes after each step in the scenario. Useful for logging or reporting.
@AfterStep
public void afterStep() {
// Code to execute after each step
11. @DataTable
Use: Provides a structured way to pass data to steps. Used for scenarios with multiple data
inputs.
@Given("the following users exist")
public void theFollowingUsersExist(DataTable dataTable) {
// Code to process the data table
12. @DocString
Use: Allows for multi-line text inputs within a step definition.
Given("the user provides the following details")
public void userProvidesDetails(String details) {
// Code to handle multi-line text input
13. @CucumberOptions
Use: Specifies options for the Cucumber runner, such as feature files, step definitions, and
plugins.
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features",
glue = "stepDefinitions",
plugin = {"pretty", "html:target/cucumber-reports"})
public class RunCucumberTest {
}
14. @ScenarioContext
Use: Used to share context between step definitions. This is helpful for maintaining state
throughout scenarios.
@ScenarioContext
private ScenarioContext scenarioContext;
15. @Tag
Use: Allows for tagging scenarios for grouping and filtering execution.
@smoke
Scenario: Validate login functionality
Given the user is on the login page
When the user enters valid credentials
Then the user should be redirected to the dashboard
16. @Hooks
Use: Allows for reusable setup or teardown methods. Hooks can be used to manage the
execution of scenarios.
@Before
public void setup() {
// Setup code that runs before each scenario
@After
public void tearDown() {
// Cleanup code that runs after each scenario
17. @Scenario Outline
Use: Defines a template for multiple scenarios, allowing you to run the same steps with
different inputs.
gherkin
Scenario Outline: Validate login with multiple users
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 |
18. @BeforeAll
Use: Runs once before all scenarios in the test suite.
@BeforeAll
public static void beforeAll() {
// Code to execute before all scenarios
19. @AfterAll
Use: Runs once after all scenarios in the test suite.
@AfterAll
public static void afterAll() {
// Code to execute after all scenarios
20. @BeforeAll / @AfterAll with Hooks
Use: Used for global setup/teardown actions for the entire test suite.
@BeforeAll
public static void globalSetup() {
// Code for global setup
}
@AfterAll
public static void globalTeardown() {
// Code for global cleanup
21. @RunWith(Cucumber.class)
Use: Indicates that the class is a Cucumber test runner.
@RunWith(Cucumber.class)
public class CucumberTestRunner {
22. @Given, @When, @Then with Regular Expressions
Use: Allows the use of regex patterns in step definitions to match steps.
@Given("^the user is on the (.+)$")
public void userOnPage(String page) {
// Code to navigate to the specified page