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

Step 2) In Rubymine Editor, click on Create New Project

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

Step 4) Create a file directory

Step 5) Name the directoryas “features”

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

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

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”

And the output you get is

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”

Step 10) Save File As below in “yourfolder/features/step_ definitions” with name test_step.rb

Step 11) Write the following code into the step file

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:

The result is

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 http://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 http://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 "http://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 "http://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