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.


Related Posts:

  • How to pass the Parameter in BDD cucumber? How to pass the Parameter?Passing parameter by using “” (double quotes) OR <> signFrom feature file, we can pass a single parameter by using “” in the step.Example is given below.Step in Feature file Given user lo… Read More
  • What Is Hooks In Cucumber? What Is Hooks In Cucumber?Hooks is nothing but the script which run before or after each scenarios. There are some annotation which are used to execute script before or after each scenarioes.Below are the lists of cucum… Read More
  • What is BDD in selenium? What is BDD in selenium?BDD (Behavior Driven Development) is software development approach that allows developer or tester to create the script base on the behavior or functionality of the application.The steps of behav… Read More
  • Test Runner class in BDD Test Runner classTest runner class is the starting point in BDD from where the script get executed with the help of junit annotation. Test runner class use junit class annotation which is @Runwith(). This gets exec… Read More
  • Regular expression in step definition in cucumber Regular expression in step definitionBelow are some lists of special character that can be used while writing step definition.1. .(dot)-We can use any character in place of "." In below example, single step definit… Read More

0 comments:

Post a Comment

Translate

Popular Posts

Total Pageviews

152,016