cucumbergherkin

How to re-use cucumber gherkin steps without adding custom suffixes?


I am trying to learn cucumber, gherkin and selenium all at the same time for the first time. I want to be able to re-use steps for various Given and And statements. I have an approach, but it doesn't feel right because I used unique suffixes in my gherkin language to avoid some compile time errors related to duplicate rules.

Below is a short example of what I did which currently works, but is not ideal.

// account.feature
Feature: Account

Scenario: Can access account
 Given Reset database with mock data
 And Login as admin ##and##
 When Go to account
 Then Confirm email exists

// automobile.feature
Feature: Automobile

Scenario: Can access automobile
 Given Login as admin
 When Go to first automobile
 Then Confirm automobile name exists

// LoginSteps.java
package StepDefinitions;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.And;

public class LoginSteps {
    
    public LoginSteps() throws Throwable {

    }
    
    @Given ("Reset database with mock data")
    public void resetDatabaseWithMockData() throws IOException {
        URL url = new URL("http://example.com/setup");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        System.out.println("GET Response Code :: " + responseCode);
    }
    @Given("Login as admin")
    public void givenLoginAdmin() throws Throwable {
       loginAdmin();
    }
      
    @And("Login as admin ##and##")
    public void andLoginAdmin() throws Throwable {
       loginAdmin();
    }
      
    public void loginAdmin() throws Throwable {
        // login as admin code
    }
}

In short, I have two feature files account.feature and automobile.feature. I expect account.feature to fire first followed by automobile.feature. Notice that both feature files require you to Login as admin, but only the account.feature requires me to reset the database with some mock data. Hence, in account.feature, I have a Login as admin ##and##, and in automobile.feature, I simply have Login as admin.

This doesn't feel right to me. My overall objective is that i want to have several *.feature files that run their tests against a mock data set1, then another set of feature files that run their tests again a mock data set 2.

Can anyone tell me if there's a better way to achieve my results?


Solution

  • In principle you should design your tests to be independent from each other. This may mean logging in for each scenario. This may mean re-starting your application for each scenario. This may mean creating fresh data for each scenario.

    This may seem wasteful, especially if you have written manual test scripts before, but with automation it should be fast enough that you can afford the independent test executions.

    So something like this:

    Given a new tenant of automobile website 
    And a new account "admin" with the admin role
    And a registered automobile "Ford 1"
    When I the "admin" opens the automobile page
    Then the automobile page contains a "Ford 1".
    

    This is assuming the automobile website is multi-tenant. If not, you may have to start a new application instead for each scenario.