Create your First Cucumber Script (2 Examples)

โšก Smart Summary

Cucumber Script execution helps you turn plain-language Gherkin scenarios into runnable automated tests using Step Definition files, allowing teams to validate behavior, share intent across stakeholders, and ship reliable BDD-driven projects on Cucumber or Cucumber-JVM.

  • ๐Ÿฅ’ Gherkin Basics: Feature files describe scenarios in Given-When-Then syntax that both testers and business teams can read.
  • ๐Ÿ“ Step Definitions: Each Gherkin step maps to a code block written in Ruby, Java, or another supported language.
  • โš™๏ธ Project Setup: RubyMine or any IDE can host the features folder, step_definitions folder, and runner configuration.
  • โœ… Two Examples: A multiplication scenario and a Guru99 demo login scenario demonstrate end-to-end BDD execution.
  • ๐Ÿงช Modern Versions: Cucumber-JVM 7.x and Cucumber Ruby 9.x remain the current stable releases used across CI pipelines.

Cucumber Script

In this tutorial, you will create Cucumber Scripts to test two scenarios using Gherkin syntax and Step Definition files. Cucumber is a popular BDD framework that connects plain-language Feature files with executable test code so business analysts and developers can collaborate on the same specifications.

Cucumber Script 1: Multiply 2 Numbers

In this first Cucumber example, you will write a simple Feature file that multiplies two numbers and then create a matching Step Definition file in Ruby. The same workflow applies if you use Cucumber-JVM 7.x with Java or Kotlin in IntelliJ IDEA.

Step 1) Open RubyMine Editor via the Windows start menu.

Cucumber Script:Multiply 2 Numbers

Step 2) In RubyMine Editor, click on Create New Project.

Cucumber Script:Multiply 2 Numbers

Step 3) Select the Project location and click “Create.”

Cucumber Script:Multiply 2 Numbers

Step 4) Create a file directory inside the project root for your Feature files.

Cucumber Script:Multiply 2 Numbers

Step 5) Name the directory as “features”. Cucumber will scan this folder automatically.

Cucumber Script:Multiply 2 Numbers

Step 6) Create and Save File in “yourfolder/features/” with name “yourfilename.feature”.

Click here if the video is not accessible

Cucumber Script:Multiply 2 Numbers

Cucumber Script:Multiply 2 Numbers

Step 7) To execute the scenario, save the following program inside the Feature file. Note how Gherkin keywords such as Feature, Scenario, Given, When, and Then describe the test behavior.

Cucumber Script:Multiply 2 Numbers

Code:

Feature: Multiplication 
  I multiply two numbers 
	Scenario: multiply a and b 
	  Given I have variable a 
	  And I have variable b 
      When I multiplication a and b 
      Then I display the Result

Step 8) Now run your first Feature file.

Click on “Start Command Prompt With Ruby”.

Cucumber Script:Multiply 2 Numbers

The output you receive is shown below.

Cucumber Script:Multiply 2 Numbers

You will see an error because you must write a Step Definition file that pairs with the Feature file.

Step 9) Create a Step Definition file for your Feature file.

Create a new folder in the RubyMine editor named “step_definition”.

Cucumber Script:Multiply 2 Numbers

Cucumber Script:Multiply 2 Numbers

Step 10) Save the file in “yourfolder/features/step_definitions” with name test_step.rb.

Cucumber Script:Multiply 2 Numbers

Cucumber Script:Multiply 2 Numbers

Step 11) Write the following code into the Step Definition file. Each block maps to a Gherkin step from the Feature file.

Cucumber Script:Multiply 2 Numbers

Code :

Given(/^I have variable a$/) do 
  @a = 50
end

And(/^I have variable b$/) do 
  @b = 70 
end

When(/^I multiplication a and b$/) do 
  @mul = @a * @b
end

Then(/^I display the Result$/) do 
  puts "Multiplication of #{@a} and #{@b} is #{@mul}"
end

Step 12) Run the Feature file again.

Cucumber Script:Multiply 2 Numbers

The result is shown below.

Cucumber Script:Multiply 2 Numbers

Cucumber Script 2: Verify output when Email id is entered or not entered

In this second example, you will use Ruby with Watir to drive a browser and validate two registration scenarios on the Guru99 demo site. The same patterns apply if you adopt Cucumber-JVM 7.x with Selenium WebDriver and Java.

In this example we use Ruby.

Test Scenario: Verify output when Email id is NOT entered.

Test Steps:

  1. Open Browser
  2. Go To https://demo.guru99.com/
  3. Do not enter Email id
  4. Click Submit

Test Scenario: Verify output when Email id is entered.

Test Steps:

  1. Open Browser
  2. Go To https://demo.guru99.com/
  3. Enter Email Id
  4. Click Submit

Code in Feature file:

Feature: guru99 Demopage Login
To Login in Demopage we have to enter login details
Scenario: Register On Guru99 Demopage without email
Given I am on the Guru99 homepage
When enter blank details for Register
Then error email shown
Scenario: Register On Guru99 Demopage with valid email
Given I am on the Guru99 homepage
When enter details for Register
Then login details shown

Code in Step Definition file:

require 'watir-webdriver'

require 'colorize'

browser = Watir::Browser.new

Given (/^I am on the Guru99 homepage$/)do

browser.goto "https://demo.guru99.com"

end

When (/^enter blank details for Register$/)do

browser.text_field(:name,"emailid").set(" ")

browser.button(:name,"btnLogin").click

end

Then (/^error email shown$/)do

puts " Email is Required".red

browser.close

end

When (/^enter details for Register$/)do

browser = Watir::Browser.new

browser.goto "https://demo.guru99.com"

browser.text_field(:name,"emailid").set("<a href="mailto:guru99@gmail.com">guru99@gmail.com</a>")

browser.button(:name,"btnLogin").click

end

Then (/^login details shown$/)do

puts " Sucessfully register"

browser.close

end

Run the code in command prompt and you will see the following output.

Verify Output When Email id is Entered or Not Entered

FAQs

A Feature file is a plain-text file with a .feature extension that uses Gherkin syntax to describe one application feature. It groups related Scenarios written with Given, When, and Then steps so non-technical stakeholders can read and validate behavior alongside developers.

Each Gherkin step is text only. Cucumber needs a Step Definition that maps the step to executable code in Ruby, Java, or another language. Without that mapping, the runner reports undefined steps and the Scenario fails to execute the intended logic.

Cucumber-JVM 7.x is the current stable line and is recommended for new Java and Kotlin projects. Cucumber Ruby 9.x remains the modern choice for Ruby teams. Both releases support JUnit 5, parallel execution, and tag-based filtering for CI pipelines.

AI assistants analyze user stories, acceptance criteria, and historical defects to draft Gherkin Scenarios in Given-When-Then format. They suggest boundary cases, alternative paths, and tag suggestions so BDD teams cover more behavior without spending hours hand-writing each Feature file.

Yes. AI tools read Feature files, match each Gherkin step to existing code, and scaffold new Step Definition methods in Ruby or Java. They generate boilerplate, parameter regex, and assertion stubs so developers only refine the business logic instead of writing every glue function by hand.

Summarize this post with: