Top 40 Ruby Cucumber Interview Questions You Must Prepare 19.Mar.2024

Cucumber is written in the Ruby programming language. It was originally used exclusively for Ruby testing as a complement to the RSpec BDD framework. Cucumber now supports a variety of different programming languages through various implementations.

All Gherkin files have the .feature file extension. They contain a single Feature definition for the system under test and are an executable test script.

Steps in Gherkin's .feature files can be considered a method invocation. Before Cucumber can execute a step it must be told, via a step definition, how that step should be performed.

Definitions are written in Ruby and conventionally filed under features/step_definitions/*_steps.rb. Definitions start with the same keywords as their invocation (including Gherkin's full language support). Each definition takes two arguments.

Either a regular expression or string with $variables

A block containing ruby code to execute

We can use the following command to generate html reports.

–format html –out report.html –format pretty

To run functional tests written in a plain text Cucumber tool is used. It is written in a Ruby programming language.

Advantages of Cucumber

  • You can inolve business stakeholders who can not code
  • End user experience is priority
  • High code reuse

P141, P142

Class can do: inheritance, having instance, while module CAN NOT. Can be required.

Module can do: make namespace to avoid name clash, can be included.

#instantiate from class within module

Module A

Class B

End

b= A::B.new

Hooks are Cucumber's way of allowing for setup to be performed prior to tests being run and teardown to be run afterward. They are defined as executable Ruby blocks, similar to JUnit methods marked with @Before, @After annotations. Conventionally they are placed under support/, and are applied globally. Three basic types of hooks exist.

Before - Runs before a scenario

After - Runs after a scenario

Around - Assumes control and runs around a scenario

Additional hooks include

  • BeforeStep
  • AfterStep
  • AfterConfiguration - Runs after Cucumber configuration and is passed an instance of the configuration

cucumber tags used to filter the scenarios. We can tag the scenarios and we can execute the scenarios based on tags,

We can add tags to scenarios with @

We can use following command to execute a cucumber tagged scenarios

cucumber features -t @<tag_name>

Cucumber execution will starts from support. In support first it will load the env.rb file then it will load hooks.rb and then it will start execute feature file scenario steps.

We can create Cucumber profiles to run specific features and step definitions

We can use following command to execute a cucumber profile

cucumber features -p <profile_name>

Ex: cucumber features -p regression

We can run particular scenario from a feature file by giving the scenario line number

Ex: cucumber features/test.feature:21

  • Support is a foder where we can setup cucumber related support.
  • rb file will be used to load the required libraries for cucumber scenario execution
  • rb we will add hooks like before, after, beforeStep and afterStep hooks

A feature is a Use Case that describes a specific function of the software being tested. There are three parts to a Feature [18]

  1. The Feature: keyword
  2. The Feature name (on the same line as the keyword)
  3. An optional description on the following lines

Features/ support file contains supporting ruby code. Files in support load before those in step_definitions, which can be useful for environment configuration.

Gherkin is the language that Cucumber uses to define test cases. It is designed to be non-technical and human readable, and collectively describes use cases relating to a software system.

The purpose behind Gherkin's syntax is to promote Behavior Driven Development practices across an entire development team, including business analysts and managers.

Cucumber generates its own html format. Define However better reporting can be done using Jenkins or bamboo tool. Details of reporting are covered in next topic of cucumber.

Scenario outline is used to execute the same scenario with different test data.

we will add test data in examples section

Cucumber does not provide built in browser automation. However, it does work well with existing gems such as Selenium and WATiR-WebDriver.[24] It does support running tests with transactions through leveraging other gems such as ActiveRecord.

We use Given,when,Then,And and But keywords in cucumber scenario steps .

Scenario Outline: Same scenario can be executed for multiple sets of data using scenario outline. The data is provided by a tabular structure separated by (I I).

Each Feature is made of a collection of scenarios. A single scenario is a flow of events through the Feature being described and maps 1:1 with an executable test case for the system.[18] Keeping with the example ATM withdrawal feature, a scenario might describe how a user requests money and what happens to their account.

Cucumber is a software tool used by computer programmers for testing other software. It runs automated acceptance tests written in a behavior-driven development style. Central to the Cucumber BDD approach is its plain language parser called Gherkin.

A test harness for cucumber and rspec allows for separating responsibility between setting up the context and interacting with the browser and cleaning up the step definition files

Gherkin's Feature structure forces organization. However, in cases where this default organization is inconvenient or insufficient, Gherkin provides Tags. Tags are @-prefixed strings and can be placed before.

  • Feature
  • Scenario
  • Scenario Outline

Examples: An element can have multiple tags and inherits from parent elements.

Cucumber dry run is used to compile cucumber feature files and step Definitions. Of there is any compilations errors it will Define How when we use dry run 

Ex: Cucumber features –dry-run

A step definition is the actual code implementation of the feature mentioned in feature file.

Cucumber tests are divided into individual Features. These Features are subdivided into Scenarios, which are sequences of Steps.

  • Before:execute before the feature file execution
  • After:executes after the feature file execution
  • BeforeStep:executes before the each step execution
  • AfterStep:executes after the each step execution

  • Rspec is used for Unit Testing
  • Cucumber is used behaviour driven development.
  • Cucumber can be used for System and Integration Tests

Cucumber execution will starts from support. In support first it will load the env.rb file then it will load hooks.rb and then it will start execute feature file scenario steps.

Gherkin language is used to express scenario in feature files and ruby files containing unobtrusive automation for the steps in scenarios

A regular expression is a pattern describing a certain amount of text. The most basic regular expression consists of a single literal character.

  • It is helpful to involve business stakeholders who can’t easily read code
  • Cucumber focuses on end-user experience
  • Style of writing tests allow for easier reuse of code in the tests
  • Quick and easy set up and execution
  • Efficient tool for testing

Although Cucumber and Jbehave are meant for the same purpose, acceptance tests are completely different frameworks

  • Jbehave is Java based and Cucumber is Ruby based
  • Jbehave are based on stories while Cucumber is based on features

We can create Cucumber profiles to run specific features and step definitions

We can use following command to execute a cucumber profile

cucumber features -p <profile_name>

Ex: cucumber features -p regression

  • Ruby and its Development Kit
  • Cucumber
  • IDE like ActiveState
  • Watir ( To simulate browser)
  • Ansicon and rspec (if required)

Cucumber uses Formatter Plugins to provide output. Several common formats are provided by default, including

  • JSON
  • HTML
  • JUnit

BDD is becoming widely accepted practice in agile software development, and Cucumber-JVM is a mainstream tool used to implement this practice in Java. Cucumber-JVM is based on Cucumber framework, widely used in Ruby on Rails world as well as in Java and .Net.

Cucumber-JVM allows developers, QA, and non-technical or business participants to write features and scenarios in a plain text file using Gherkin language with minimal restrictions about grammar in a typical Given, When, and Then structure.

The feature file is then supported by a step definition file, which implements automated steps to execute the scenarios written in a feature file. Apart from testing APIs with Cucumber-JVM, we can also test UI level tests by combining Selenium WebDriver.

Feature:Visit XYZ page in abc.com

Scenario:Visit abc.com

Given:I am on abc.com

When:I click on XYZ page

Then:I should see ABC page