0% found this document useful (0 votes)
64 views19 pages

Create Feature File and Step Definition File in Cucumber - Way2Automation

This document provides instructions for creating a feature file and step definition file in Cucumber. It explains how to create a new package called "Features" in Java and add a feature file with the ".feature" extension. It then demonstrates writing a sample feature using keywords like "Feature", "Scenario", "Given", "When", and "Then". It also shows how to configure Cucumber to run the feature file and see the steps that need implementing. Finally, it demonstrates creating a step definition file to map the steps to Java code by using annotations like "@Given", "@When", and "@Then" along with regular expressions to pass parameters between the feature file and step definitions.

Uploaded by

raja_2689
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views19 pages

Create Feature File and Step Definition File in Cucumber - Way2Automation

This document provides instructions for creating a feature file and step definition file in Cucumber. It explains how to create a new package called "Features" in Java and add a feature file with the ".feature" extension. It then demonstrates writing a sample feature using keywords like "Feature", "Scenario", "Given", "When", and "Then". It also shows how to configure Cucumber to run the feature file and see the steps that need implementing. Finally, it demonstrates creating a step definition file to map the steps to Java code by using annotations like "@Given", "@When", and "@Then" along with regular expressions to pass parameters between the feature file and step definitions.

Uploaded by

raja_2689
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Create feature file and Step definition file in Cucumber

 December 4, 2021  No Comments  Cucumber BDD

Tutorial 3​ –​ ​ Create​ feature​ and Step definition​ file​ in cucumber


What you will Learn :

o Create a feature file

o Configure the​ cucumber​ feature

o Execute the feature file

o Create Step Definition​ file

Create a feature file


Right click src/test/java > New > Package​ 

Let us name the package as ‘Features’​ (you can also name the


package as ‘AppFeatures’ or any other name you wish to) 
Finish, the package gets created

Next, right click ‘Features’ package > New > File

The filename should describe some feature (example: login, search,


addtocart etc). So, let us mention a file name with​​ .feature​​ suffix as
seen below


Finish
Notice below that​ feature file gets created​ with​ some default features.
This is due to the cucumber plugin that we had installed in the
previous tutorial


You can also see some of the comments (lines 1-18) that describe
some of the keywords for your ready reference.
Let us remove the sample features (line#19 onwards)

We will create our own feature using the​​ Feature:​ reserved​​ keyword.


So let us write​​ Feature:

Now hit spacebar to create whitespace. Notice that the color changes

Let us give some name to our feature​ 



Let us now write scenario name using​​ Scenario:​​ keyword

Let us now write the ‘Given’ keyword which defines some pre-condition

Carefully see line#22 above. Do NOT​​ add colon after ‘Given’ keyword.


Thus​​  ​ is wrong. You should use​​  ​ 
Next, we will write the ‘When’ step that defines some action. Since the
name of the course​ is a string, it should be in double quotes, the price
is numeric and hence without quotes ​ 


Next​ we will write ‘Then’ statement that​ defines an outcome

So this was our first scenario. Likewise, you can write any number of
scenarios for search feature.
Configure​ the cucumber feature
Let us see how to​ configure​ cucumber​ feature​ 
Right click feature file > Run As > Run Configurations


See below.​ Expand ‘Cucumber Feature’ and select New_configuration.​ 
Click ‘Browse’ button and select the path of the feature file

Execute the feature file


Click Apply, click Run


The below message would be seen in console:

The console has suggested us 3 java methods (code snippets)​​ that


need to be implemented. So basically, we have not yet implemented
the​​ 3 steps in our​​ feature​​ file.​​ The 3 steps are: Given, When, Then.​​ 

The feature​​ file​​ currently​​ is in plain English​​ language.​​ 


You can also execute the feature file by right clicking the feature file >
Run As > Cucumber Feature, see below.​ 


Create Step Definition
Recall the cucumber framework design that we had discussed in
previous tutorial. In a step definition file, we map the Give/When/Then
steps using @ annotations

Now, under src/test/java, create a new package ‘StepDefinitions’ and


under this package, create a class


Next, go to the console and copy the code snippets

Paste these methods in step definition​ file:


Right now there are 3 errors.​ Press Ctrl+Shift+o to import
Given/When/Then

The errors are resolved now.


See below. The ‘Given’ step in feature file is mapped to @Given in step
definition file. Also, the​​ exact​​ step​​ name​ written in feature file​​ "Search
field should be present on the Way2Automation website"​​ is mapped to
the​​ step​​ of @Given method​​ in the step def file​​ 


Next,​​ whatever is the step name "Search field should be present on the
Way2Automation website",​​ cucumber will generate the same method
name​​ 

Note:​ You can change the method name​​ to any other desired name,
example​​ search_field_should_be_present_on_the_website().​​ However,
the step name CANNOT be changed.​​ So, you​​ cannot change the step
name to​​ @Given("Search field should be present on the website").
Next, the string that we mentioned within double quotes in the ‘When’
step, translates to {string}​ in the step definition file. Similalrly, the
integer we mentioned in the ‘When’ step, translates​ to {int}​ in the step
definition file. Also, cucumber automatically generates 2 method
parameters: String, Integer​ 


Next, you can change the variable names as seen below
Similarly, you can change the variable name in @Then method

One thing that you notice in the step definition file is that, cucumber
automatically generates an exception.​ The reason being, we have not
yet written any​ java code for the respective steps.

Next, run the feature file

So, when you now run the feature file, the console shows the​ same​
 Exception


To resolve this, let us remove the​ exception from the step def file, see
below

Next, we will write some simple java code SOPs


Whatever course name and price you have written in the feature file,
the same gets passed to ‘courseName’ and ‘price’ variables
respectively in step def file.
Next, let us again run the feature file

Notice the console output, all 3 steps got passed this time. There are
no exceptions.

Also notice that the respective course name and price​ gets passed to
the SOPs, see below. You can also see the respective Give/When/Then
steps in the console below. The course name mentioned in
When/Then steps matches with the course names in respective SOPs

So, this is how we implement the step definition file. 

Thank you​​ for reading!


Share On

     

Leave a Comment
Your email address will not be published. Required fields are marked *

Type here..

Name*

Email*

Website

Save my name, email, and website in this browser for the next time I comment.

Post Comment »

Search

Recent Posts
Identify Nth element using Playwright Java
Download a File using Playwright Java
Create and upload a new file during runtime using Playwright Java
Post API Request using Playwright Java
Keypress and GET Request API testing in Playwright Java

Archives
November 2022
October 2022
September 2022
August 2022
July 2022
March 2022
February 2022
January 2022
December 2021
November 2021
October 2021
September 2021

Categories
Behave BDD
Cucumber BDD
Cypress
DevOps

Playwright JAVA
Playwright JS
Selenium 4.0
Selenium C# 
Selenium Java
Worked with various CMM level orgranizations. Involved in setting up of manual and
automation testing teams. Implemented various automation projects using Selenium,
Webservices REST APIs, QTP, SOAP UI, Cypress, Robot Framework, Protractor, JMeter etc. 

IMPORTANT LINKS
Home
About Us
Member Login
Lifetime Membership
Buy Now
Careers
Contact Us

COURSES

Selenium Java
Selenium Python
Selenium Python
API Testing
DevOps
Jmeter
Manual Testing
Appium Java
Appium Python
Robot Framework

ABOUT US
 CDR Complex, 3rd Floor, Naya Bans Market, Sector 15, Noida, Near sec-16 Metro Station
 +91 97111-11-558
 +91 97111-91-558
[email protected]
[email protected]

Ⓒ Way2Automation - All Rights Are Reserved

You might also like