Cucumber automation code structure
Published: 2023-03-23 05:27:08

The structure of a cucumber automation code usually follows the following steps:

Feature file: This file contains the description of the feature to be tested. It should be written in the Gherkin language which is a business-readable, domain-specific language that describes scenarios using a set of predefined keywords like Given, When, Then, And, and But.

```
Feature: Login
  As a user
  I want to log in to the system
  So that I can access my account

  Scenario: Successful login
    Given I am on the login page
    When I enter my valid credentials
    And I click the login button
    Then I should be logged in successfully
```


Step Definitions: These are the implementation of the steps defined in the feature file. They are written in programming language, and their main purpose is to interact with the system under test, and perform actions or validations. Each step definition should correspond to a step in the feature file.

```
import io.cucumber.java.en.*;

public class LoginSteps {

    @Given("I am on the login page")
    public void navigateToLoginPage() {
        // code to navigate to login page
    }

    @When("I enter my valid credentials")
    public void enterCredentials() {
        // code to enter valid credentials
    }

    @When("I click the login button")
    public void clickLoginButton() {
        // code to click the login button
    }

    @Then("I should be logged in successfully")
    public void verifySuccessfulLogin() {
        // code to verify successful login
    }
}
```


Hooks: Hooks are code snippets that execute before or after each scenario or feature. They can be used for setup and teardown tasks such as launching the browser, closing the browser, or creating test data. Hooks can be global or specific to a feature or scenario.

Test Runner: A test runner is a program that runs the feature files and executes the step definitions. There are different test runners available for Cucumber, including JUnit, TestNG, and Cucumber-JVM. The test runner usually handles the configuration, setup, and execution of the tests.

```
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/myFeature.feature",
                 glue = {"com.example.stepdefinitions"})
public class TestRunner {
    // empty
}
```


Supporting files: These are files that contain additional information or configuration for the tests, such as test data, configuration files, or utility classes. They can be organized in folders for better structure and maintainability.

Here's an example of how a typical Cucumber project structure could look like:

```
- src
  - test
    - java
      - com
        - example
          - features
            - feature_file.feature
          - step_definitions
            - step_definition.java
          - runners
            - TestRunner.java
          - support
            - hooks.java
            - utils.java
          - resources
            - test_data.csv
    - resources
      - chromedriver.exe
  - main
    - java
    - resources

```