Create your First Cucumber Script (2 Examples)

In this tutorial, we will create Cucumber Scripts to test two scenarios

Cucumber Script 1: Multiply 2 Numbers

Step 1) Open RubyMine Editor via 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

Cucumber Script:Multiply 2 Numbers

Step 5) Name the directoryas “features”

Cucumber Script:Multiply 2 Numbers

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

Cucumber Script:Multiply 2 Numbers

Cucumber Script:Multiply 2 Numbers

Step 7) To execute our scenario, save the following program in the Feature File

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 let’s Run our First feature file!

Click on “Start Command Prompt With Ruby”

Cucumber Script:Multiply 2 Numbers

And the output you get is

Cucumber Script:Multiply 2 Numbers

You see the error because you have to write step definitions file for feature file

Step 9) Let’s create step definition file for our Feature File!

Create a new folder in Rubymine editor with name “step_definition”

Cucumber Script:Multiply 2 Numbers

Cucumber Script:Multiply 2 Numbers

Step 10) Save File As below 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 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) Now, again run our feature file:

Cucumber Script:Multiply 2 Numbers

The result is

Cucumber Script:Multiply 2 Numbers

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

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("guru99@gmail.com")

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 get

Verify Output When Email id is Entered or Not Entered