---Advertisement---

How to Create Your First Cucumber Script in Ruby: Step-by-Step with Examples Best 2025

By Manisha

Updated On:

---Advertisement---
 First Cucumber Script in Ruby

✅First Cucumber Script in Ruby :Introduction

Getting Started with Cucumber Scripts

First Cucumber Script in Ruby: Cucumber is a powerful BDD (Behavior Driven Development) testing framework that allows you to write test scenarios in plain English using Gherkin syntax. In this guide, you’ll learn how to create and run two beginner-friendly Cucumber scripts using RubyMine IDE.


First Cucumber Script in Ruby: Before starting, ensure the following are installed:

  • Ruby & DevKit
  • Cucumber gem (gem install cucumber)
  • RubyMine IDE
  • watir-webdriver gem (gem install watir-webdriver)

✅ Cucumber Script 1: Multiply Two Numbers

First Cucumber Script in Ruby: Write a feature to multiply two numbers and verify the result using Cucumber.

Step-by-Step Instructions:

Launch RubyMine from the Windows Start menu.

  • Go to File > New Project
  • Choose the directory where the project will be stored and click Create
  • Inside your project, create a new folder called features
  • Under features, create a new file named multiply.feature

gherkin

Feature: Multiply two numbers

  Scenario: Multiply 5 and 10

    Given I have two numbers 5 and 10

    When I multiply them

    Then the result should be 50

Open “Start Command Prompt with Ruby” and run:

bash

cucumber features/multiply.feature

You’ll see an error because the step definitions are not yet created.


✅ Step Definition for Multiplication

  • Cucumber Script in Ruby: Create a folder inside features named step_definitions
  • Create a Ruby file multiply_steps.rb

ruby

Given(/^I have two numbers (\d+) and (\d+)$/) do |num1, num2|

  @result = num1.to_i * num2.to_i

end

When(/^I multiply them$/) do

  # Multiplication done in Given step

end

Then(/^the result should be (\d+)$/) do |expected_result|

  expect(@result).to eq(expected_result.to_i)

end

bash

cucumber features/multiply.feature

✅ You should now see the test pass successfully.


✅ Cucumber Script 2: Email Input Validation

First Cucumber Script in Ruby: Test the behavior of a form when the email field is empty or filled.


Feature File: email_validation.feature

gherkin

Feature: Email form validation

  Scenario: Submit form without email

    Given I open the browser

    And I navigate to “http://demo.guru99.com/”

    When I click the submit button without entering an email

    Then I should see an error message

  Scenario: Submit form with email

    Given I open the browser

    And I navigate to “http://demo.guru99.com/”

    When I enter “test@example.com” in the email field

    And I click the submit button

    Then I should be redirected or see a success message


Step Definition File: email_steps.rb

ruby

require ‘watir’

require ‘rspec’

browser = Watir::Browser.new :chrome

Given(/^I open the browser$/) do

  @browser = Watir::Browser.new :chrome

end

And(/^I navigate to “(.*)”$/) do |url|

  @browser.goto url

end

When(/^I click the submit button without entering an email$/) do

  @browser.button(name: ‘submit’).click

end

Then(/^I should see an error message$/) do

  expect(@browser.text).to include(“Email ID is required”) # Adjust message as needed

end

When(/^I enter “(.*)” in the email field$/) do |email|

  @browser.text_field(name: ’emailid’).set(email)

end

And(/^I click the submit button$/) do

  @browser.button(name: ‘btnLogin’).click

end

Then(/^I should be redirected or see a success message$/) do

  expect(@browser.url).to include(“access.php”) # Adjust depending on app behavior

end


Run the Email Feature File

bash

cucumber features/email_validation.feature

✅First Cucumber Script in Ruby: The tests will run in the browser and validate the presence or absence of email input.


✅Summary

Cucumber ScriptDescription
Multiply NumbersMultiplies two integers and validates result
Email ValidationTests form behavior with and without email input

Gherkin Language in Cucumber

jetbrains.com/ruby

Leave a Comment

Index