Saturday, April 22, 2023

 

How to write step definition in cucumber?

Java class is used for writing step definition file. This execute each given steps in feature file. We have to write a method for each step in step definition java class with same annotation and with the same steps given in feature file. Each step associate to one method which get executed.

Annotation is step definition

Below are the lists of annotations which are generally used in feature file and same are used in step definition.
@Given
@When
@Then
@And
@But

While writing step definition, we have to make sure the steps written in feature file, it should be same in step definition. Such as example given below.

Step in Feature file

 Given This is a book

Step definition

    
@Given (“^This is a book$”)
Public void method1()  
{
System.out.println(“test”);
}

Note: - Method name can be anything, but it’s better to have method name as per the steps.

“^”sign indicate the start of text and “$” sign indicate the end of text. These anchors are used to make our test step unique. If we are not using this anchors then 2 matching steps will be consider as same steps.



0 comments:

Post a Comment

Translate

Popular Posts

Total Pageviews