How to Write a Good Step in Gherkin?

0
160
Spread the love
  • 28
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    137
    Shares

cucumber- Gherkins
cucumber- Gherkins

How to Write a Good Step in Gherkin?

Steps

Lines are called steps. Good steps are easy to understand and as obvious as it can be- the text is written in a business readable domain language known as Gherkins. Gherkin will go through each step written in the step definition file. The step given in the feature file must match with the step definition file.

Step definition

To execute steps we need automation code. Steps are matched via Regular expression. Step definition should be written in a pure coding language like -Java. It is written by the developers for each step.

When Cucumber executes a Gherkin step in scenario, that links to the step definition for actual code.

Scenario: Visitor count
Given   :I have 15 visitors at the store

The equivalent step definition is as follows

package myTest;
import cucumber.api.java.en.Given;
public class MyStepDefinition{
    @Given("I have (int) visitors at the store")
    public void i_have_n_visiors_at_the_store(int visitors){
    System.out.println("Visitors"+visitors);
}
}

You can use Java’s advanced concept of Lambda expression too

package myTest;
import cucumber.api.java8.En;
public class MyStepDefinition implements En{
@Given("I have (int) visitors at the store",(Integer visitors)->{
System.out.println("Visitors"+visitors);
});
}
Step definition expression

A step definition expression can be of the following types

  • Regular expression
  • Cucumber expression.

State Management

A step definition may convert or change the present state of the application to a subsequent step definition. It can done by putting the state in an instance variable.

You need to create the test cases in such a way that the state from one scenario should not propagate to other scenario. Leaking states will not allow the test cases to run in isolation. Debugging will become tough.

However, It is possible to share the state between steps in a scenario. In that case store your object state in a variable in your step definition. However it will make the steps tightly coupled.

How to stop state leaking from Scenario to Scenario?

In order to avoid the state licking, you need to abide by the followings:

  • Clean your database in a Before hook. Most of the testers miss this.
  • Do not strictly use global or static variables. They the second most culprit for state leaking.
  • Delete cookies when a browser or session is shared between the scenarios.

Scope

If step definition is not attached to a feature file or a scenario,the file,class,library,package name of the step definition does not cause any harm to Gherkins steps that will match. It always depend the step definition expression.

Auto Code Snippet Generation

In case when Cucumber receives a Gherkin step without a matching step definition, it will automatically print a function/method snippet matching with the Cucumber expression. You can always execute the Gherkin script before writing any concrete script. The auto generated code snippet will guide you further. In that case you have to implement the method pointed out by Gherkin.

Note: In order to get the suggestion and summary printed, you need to summary plugin attached to your editor.

Let’s say we have one Gherkin step defined as:

Given : The book has 10 pages

Now if the matching step definition is not found, Cucumber will automatically generate:

@Given("The book has 10 pages")
public void The_book_has_pages(int int1)
{
//this place is for your implementation
}

Step Arguments

From the step definition, Cucumber can automatically extract text and convert to an equivalent data type(like int) and passes as an argument to the method. The number of parameters in the method should exactly match that is defined in the step definition. Otherwise cucumber will throw an error and will generate the equivalent code snippet.

Transformation of Steps during Implementation

A step is similar to the method call or a function call from implementation perspective. The steps will be defined in the *.feature file.

During transformation the below steps happen:

  • Cucumber will try to match a step against the step defined in the step definition using regular expression.
  • Cucumber accumulates all groups and variables.
  • Cucumber flats all the data provided in the step definition.
  • Cucumber will pass all these artifacts to the method.
  • Cucumber will start executing the test.

Note: Once Cucumber registers the methods , the keywords(Given,When,Then,And,But) do not have any significance.

Results if step execution:

Each step will have one of the following results:

  • Success– The corresponding step definition is found and the execution happens. It is marked as Green. The result does not depend on the outcome of the step executing.
  • Failed- The corresponding method is found but during execution, the step caused an error. The result does not depend on the outcome of the step executing. These steps are marked as Red.
  • Undefined- The corresponding step is not found and the step becomes undefined. All the remaining steps are skipped during execution. They are marked as yellow. If you use — strict command to execute, cucumber will exit with exit code 1.
  • Pending-  Similar like Undefined but the step function actually invokes a pending method. A pending method is an unfinished method. They are marked as yellow. If you use — strict command to execute, cucumber will exit with exit code 1.
  • Skipped-  Steps like Undefined,Pending or skipped due to failed steps are actually never get executed. They are marked as cyan.
  • Ambiguous- In case you do not have unique step definition, cucumber  will raise AmbigousStepDefinitionsException. You have no other choice to fix it.

 

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 28
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    137
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here