top of page
arrow.png
arrow_edited_edited.jpg

Cucumber Interview Questions

What are the advantages of cucumber testing framework?

  • Cucumber is a powerful and versatile testing framework that can be used to improve the quality of a software.

  • Cucumber tests are written in a human-readable format called Gherkin, which makes them easy to understand for both technical and non-technical stakeholders. It supports behavior-driven development (BDD) approach.

  • Since Cucumber tests are written in a natural language/declarative format i.e., the tests do not specify how to implement the behavior, tell only what the behavior should be. 

  • Also, the tests are written in a shared format, which makes it easy to collaborate with other developers and stakeholders.

  • They can be easily automated using a variety of tools. It helps to improve the development process by making it easier to identify and fix defects in the early stage of development.

What is meant by a feature file?

Cucumber tests are typically organized into features, which are a collection of scenarios.

  • A feature file provides a high-level description of an Application Under Test.

  • Each feature file starts with the keyword 'Feature'. 

  • It is a description of what the software is supposed to do

List the most important keywords used in Cucumber feature file.

  • Feature

  • Scenario

  • Scenario Outline

  • Examples

  • Given

  • When

  • Then

  • And

  • Background

What is TestRunner class in Cucumber?

  • It is an empty class file, used to run tests in Cucumber framework.

  • No code is written under the TestRunner class.

  • However, the tags @RunWith and @CucumberOptions should be included.

  • Under the @CucumberOptions tag, the path of the feature file and step definition file that we want to execute should be provided.

What is Step definition file in Cucumber?

  • A step definition file contains the corresponding code for the steps defined in a Cucumber feature file.

  • Each step definition is nothing but a method.

  • The method body contains the code that links it to one or more Gherkin steps and executes the step.

E.g., Feature file

Scenario: Login into Homepage

Given I am on the home page

Step Definition file

import io.cucumber.java.en.Given;

public class StepDefinitions {

    @Given("^I am on the home page$")

public void loginwithCredentials ( ){

WebDriver driver=new ChromeDriver();

driver.get(“url”);

driver.findElement(By.id(“uname”)).sendKeys(“katty”);

driver.findElement(By.id(“pword”)).sendKeys(“katty123”);

driver.findElement(By.id(“loginbtn”)).click();

}

        When Cucumber executes a feature file, it will first look for a matching step definition for each step in the feature file. If a matching step definition is found, Cucumber will execute the code in the step definition. They also make it easy to reuse code across multiple tests.

How can Cucumber tests be run parallelly?

Cucumber tests can be run parallelly by adding some plugins to the project's dependencies and then configured it in test runner class file.   To run the tests in parallel we can have,

1.cucumber-parallel plugin (using multiple threads)

2.cucumber-junit plugin (using JUnit)

3.cucumber-testng plugin (using TestNG)

@RunWith(Cucumber.class)
@CucumberOptions(features = {"\\MiniProjectCucumberTestNG\\Features"}, 
                   glue = {"stepDefinitions"}, 
                   dryRun = false, 
                   monochrome = true, 
                   plugin = { "pretty", "html:target/Cucumber", "json:target/Cucumber.json" },
                   publish=true
                   )

public class TestRunner extends AbstractTestNGCucumberTests {
                       @DataProvider(parallel = true)
                       public Object[][] scenario() {
                           return super.scenarios();
                       }

    Also, we can use a load balancer to distribute the tests across multiple machines, this can help to improve the performance of the test suite.

Explain grouping in Cucumber.

       Grouping the related tests can help us to organize and manage the tests in an easier manner. It makes the testing more efficient. Cucumber testing framework's grouping facility let us to categorize Cucumber features and scenarios into groups. There are a few different ways to group Cucumber features and scenarios.

1.Tags are a way to identify groups of tests that share a common characteristic.

 Use the `@` symbol to tag the features and scenarios.  e.g., @smoke Test

@SmokeTest

Scenario: Login to the application

Given the user is on the login page

When the user enters their username as "admin" and password as "admin123"

Then the user should be logged in

Using the tags option in the @CucumberOptions class, we can specify which tags we want to run 

@RunWith(Cucumber.class)

@CucumberOptions(

            tags = {"@SmokeTest"})

public class SmokeTestRunner { }

This will run all of the scenarios that are tagged with @SmokeTest.

2. Use the Background keyword.

Background keyword allows us to define steps that are common to all of the scenarios in a feature.

Background: Given the user is on the home page

Scenario: Login to the application

When the user enters their username as "name123" and password as "pword123"

Then the user should be logged in

Scenario: Logout from the application

When the user clicks the logout button

Then the user should be logged out

                In this example, the Given the user is on the home page step is defined in the Background section. This step will be run before each of the scenarios in the feature.  It is useful for setting up the same environment that are used in multiple scenarios.

3. Use the Feature keyword. The Feature keyword allows us to define a group of scenarios that are related to a specific feature of our application.

Feature: Login

Scenario: Login to the application

Given the user opens the URL "https://www.amazon.com/"

When the user enters their username as "shopper" and password as "shopper123"

Then the user should be logged in

Scenario: Logout from the application

When the user clicks the logout button

Then the user should be logged out

               In this example, the Login feature contains two scenarios: Login to the application and Logout from the application.

Describe the use of the Options tag in the Cucumber Framework.

        In the Cucumber framework, the Options tag is a part of the TestRunner file and comes in the form of an annotation called @CucumberOptions. By using the `@Options` tag, one can customize the behavior of Cucumber to meet the specific needs.  It is used to specify options that control how Cucumber should run the tests.

        Cucumber can report its results in a variety of formats, such as text, HTML, and JSON, so the format of the output can be specified by using the `format` option.  By default, Cucumber runs all the tests in a single thread. In order to specifying the number of threads, 'threads' option is used.

To run specific tests that are associated with each other, ‘tags’ option can be used.

@CucumberOptions (

                 format = {"html"},

                threads = 2,

            tags = {"@smoke"})

      In the above example, the `@Options` tag is used to specify that the output should be in HTML format, two threads should be used to run the tests, only run the tests that are tagged with `@smoke`

@Runwith(Cucumber.class)

@CucumberOptions (

       features={"src/test/java/login.feature"},         //To specify the path of the feature file.

       glue={"stepDefinitions"},                               //To specify the path of the step definition file.

      tags={"@smokeTest"},

      plugin = {"pretty", "summary"},

      format = {"pretty", "html:target/cucumber"},

       dryRun=false,

       monochrome=true

)

public class TestRunner{

}

What are scenarios in Cucumber?
Scenarios are a collection of steps that describe a specific interaction with the application.

What is the maximum number of scenarios included in a feature file?

  • A single feature file can contain any number of scenarios but focuses only on one feature such as registration, login etc at a time.

  • By using the keyword "Scenario" or "Scenario Outline", One Scenario can be separated from another. it is advisable to keep the number to a minimum.

For e.g., the maximum number of scenarios that can be included in a feature file is 10.

What is the purpose of Scenario outline keyword?

  • The Scenario Outline keyword can be used to run the same Scenario multiple times, with different combinations of values.  i.e., to achieve data parametrization. 

  • Scenario Outline must be followed by the keyword ‘Examples’ which specify the set of values for each parameter. 

  • The multiple data sets are represented in the form of a table separated by (||) symbol under 'Examples' keyword. Each row represents a group of data.

What is the use of glue property under Cucumber Options tag?

It helps Cucumber to locate the step definition file.

Is Cucumber A automation tool?

A cucumber is a tool based on Behavior Driven Development (BDD) framework which is used to write acceptance tests for the web application.  

Comparison Between Selenium and Cucumber in Automated Testing

Slide4_edited.png

Explain the purpose of the Cucumber TestNG test runner class in a Cucumber test suite. Also, Mention the role of the AbstractTestNGCucumberTests class in the context of Cucumber and TestNG integration.

 The specified options in the @CucumberOptions annotation will determine which features, scenarios, and step definitions are executed during the test run.

 

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;

@CucumberOptions(

                       // Path to your feature files

             features = "src/test/resources/features",

                     // Package where your step definitions are located

             glue = "stepdefinitions",

                   // Optional: Specify the tags for scenarios to run

              tags = "@smoke",

                  // Optional: Specify the reporting format and location

              plugin = {"pretty", "html:target/cucumber-reports"}

           )

public class TestRunner extends AbstractTestNGCucumberTests {

}

@CucumberOptions: This annotation is used to specify various options for running Cucumber tests.

features : Specifies the path to the directory containing your feature files.

glue       : Specifies the package where your step definitions are located.

tags       : (Optional). Specifies tags to run specific scenarios or features.

plugin   : (Optional). Specifies the format and location of the output reports.

AbstractTestNGCucumberTests: This class is provided by the Cucumber TestNG integration to facilitate the integration of Cucumber with TestNG. It extends AbstractTestNGCucumberTests to enable the execution of Cucumber scenarios using TestNG.

 Maven dependency for Cucumber TestNG:

<dependency>

    <groupId>io.cucumber</groupId>

    <artifactId>cucumber-testng</artifactId>

    <version>7.3.0</version>

    <scope>test</scope>

</dependency>

 BDD, or Behavior-Driven Development, is a software development methodology that aims to enhance collaboration between different stakeholders in a software project, such as developers, testers, and non-technical individuals (like business analysts or product owners). BDD emphasizes communication and shared understanding of the behavior of a system through examples expressed in natural language. Hence by using BDD practices with Cucumber, teams can create a shared understanding of system behavior, ensure alignment between business requirements and development efforts, and produce living documentation that is easily readable by both technical and non-technical stakeholders. The natural language specifications in Gherkin make it possible to have conversations about system behavior without diving into technical details, fostering collaboration across the entire development team.

 Gherkin Language

Gherkin is a business-readable language used to write specifications in a natural language format.

It is used to describe the behavior of a system using scenarios composed of steps in the Given-When-Then format

Feature: User Authentication

Scenario: Valid user login

  Given the user is on the login page

  When the user enters valid credentials

  Then the user should be logged in successfully

 Feature Files

Feature files contain Gherkin scenarios and are used to describe the expected behavior of a system.

Each feature file typically corresponds to a specific feature or functionality of the software.

 Step Definitions

Step definitions are written in a programming language (e.g., Java, Ruby) and provide the automation logic to execute the steps defined in Gherkin scenarios.  They bridge the gap between the natural language of Gherkin and the programming language used for automation.

     @Given("^the user is on the login page$")

             public void navigateToLoginPage() {

                // Code to navigate to the login page

              }

 Test Execution:

Cucumber executes the Gherkin scenarios by matching them with corresponding step definitions.

Test results are presented in a human-readable format, indicating which steps passed or failed.

Explain the concepts and different types of hooks available in Cucumber. How do hooks enhance the flexibility and maintainability of Cucumber scenarios?

 In Cucumber, hooks are blocks of code that allow you to run pre- and post-scenario or feature-specific actions. Hooks provide a way to set up and tear down resources, manage state, or perform actions before or after scenarios and features.

 Before Hooks

@Before annotation in Java.

Executed before each scenario.

Useful for setting up initial conditions or preparing resources before scenario execution.

 

import io.cucumber.java.Before;

public class BeforeHooks {

    @Before

    public void beforeScenario() {

        // Code to be executed before each scenario

    }

}

 After Hooks

@After annotation in Java.

Executed after each scenario.

Useful for cleaning up resources, taking screenshots, or logging results after scenario execution.

 

import io.cucumber.java.After;

public class AfterHooks {

    @After

    public void afterScenario() {

        // Code to be executed after each scenario

    }

}

 BeforeStep Hooks

@BeforeStep annotation in Java.

 Executed before each step within a scenario.

Useful for actions needed before each step, such as logging or taking specific actions.

 

import io.cucumber.java.BeforeStep;

public class BeforeStepHooks {

    @BeforeStep

    public void beforeStep() {

        // Code to be executed before each step

    }

}

 AfterStep Hooks

@AfterStep annotation in Java.

Executed after each step within a scenario.

Useful for actions needed after each step, such as logging or capturing additional information.

import io.cucumber.java.AfterStep;

public class AfterStepHooks {

    @AfterStep

    public void afterStep() {

        // Code to be executed after each step

    }

}

 Before and After Hooks for Tags

 @Before("@tagName") and @After("@tagName") annotations in Java.

Executed before or after scenarios with a specific tag.

Allows you to conditionally run hooks based on scenario tags.

Java

 

import io.cucumber.java.Before;

import io.cucumber.java.After;

 

public class BeforeAndAfterHooksForTags {

    @Before("@tagName")

    public void beforeTaggedScenario() {

        // Code to be executed before scenarios with @tagName

    }

 

    @After("@tagName")

    public void afterTaggedScenario() {

        // Code to be executed after scenarios with @tagName

    }

}

 Thus, Hooks in Cucumber help in managing the test environment, setting up and cleaning up resources, and performing actions at different stages of scenario execution. They provide a flexible way to customize the test execution flow based on your requirements.

Why use Cucumber with Selenium?

Slide6_edited.jpg

What is a scenario outline in Cucumber?

A Scenario Outline in Cucumber is a way to run the same scenario multiple times with different sets of data. It allows to parameterize the scenarios and run them with different inputs without duplicating the entire scenario. Scenario Outlines are particularly useful for scenarios that follow a similar structure but vary in terms of input values or data. The basic structure of a Scenario Outline involves the use of a template scenario and a data table (or examples table) that provides different sets of values to be substituted into the template. Each row in the examples table represents a set of input values for a single execution of the scenario.


Feature: Login Functionality
Scenario Outline: User logs in with valid credentials
Given the user is on the login page

 

When the user enters &quot;&lt;username&gt;&quot; and &quot;&lt;password&gt;&quot;
Then the user should be logged in successfully

 

Examples:
| username | password |
| user1 | pass123 |
| user2 | pass456 |
| user3 | pass789 |

 

In this example,
Scenario Outline: indicates the start of the Scenario Outline.
<username> and <password> are placeholders for the parameters.

 

The Examples: section provides different sets of values for the placeholders.
Each row in the examples table represents a unique set of inputs for the scenario.
During execution, Cucumber replaces the placeholders with the values from each row in the examples table and runs the scenario multiple times, once for each set of data. This helps in creating more concise and maintainable feature files, especially when dealing with repetitive scenarios.  When Cucumber executes the Scenario Outline, it generates individual scenarios for each row in the examples table, resulting in separate reports and test results for each set of input values. This makes it easy to identify which specific set of inputs caused a scenario to pass or fail.

Explain the components of a typical Cucumber report and discuss the types of reports generated byCucumber. How Cucumber reports can be integrated into continuous integration pipelines.

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

Slide6_edited.jpg

What is Gherkin in Cucumber?
Gherkin is a domain-specific language used in Cucumber for writing human-readable descriptions of software behaviors without detailing how that functionality is implemented. It serves as a syntax for defining executable specifications in a format that is easy for both technical and non-technical stakeholders to understand. Gherkin is primarily associated with Behavior-Driven Development (BDD) practices, and it is designed to facilitate communication and collaboration among team members.

Key features and characteristics of Gherkin
Natural Language Syntax
Gherkin uses a natural language syntax that is designed to be human-readable and easy to understand.
It uses keywords like Feature, Scenario, Given, When, Then, And, and But to structure scenarios and
steps.

Structured Format
Gherkin scenarios are organized in a structured format using specific keywords to define the different
elements of a behavior.
Commonly used keywords
Feature: Describes a high-level feature or functionality.
Scenario: Represents a specific test scenario.
Given: Describes the initial context or preconditions.
When: Describes the action or event that triggers the behavior.
Then: Describes the expected outcome or result.And &amp; But: Used for additional steps within a scenario to improve readability.
Scenario Outlines:
Gherkin supports Scenario Outlines, allowing the same scenario to be executed with different sets of
data.
It helps in parameterizing scenarios and reducing duplication.
Comments:
Gherkin allows the use of comments, denoted by the # symbol, to provide additional context or
explanations within the feature files.
Tags:
Gherkin supports tagging scenarios with labels (tags) using the @ symbol.
Tags can be used for categorization, filtering, and organizing scenarios.
Example login feature file
Feature: Login Functionality
Scenario: Successful Login
    Given the user is on the login page
    When the user enters valid credentials
    Then the user should be logged in successfully

In this example, Feature introduces the high-level feature, Scenario describes a specific scenario, and
Given, When, and Then describe the steps of the scenario. Gherkin scenarios are typically stored in feature files with a .feature extension and are used as input by Cucumber for test automation. The structured and human-readable nature of Gherkin makes it an 
effective tool for promoting collaboration and communication between team members involved in software development.

bottom of page