0% found this document useful (0 votes)
39 views32 pages

Theory

NA

Uploaded by

AMOL KENE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views32 pages

Theory

NA

Uploaded by

AMOL KENE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Give your users a seamlIntroduction to Cucumber Testing Framework

Cucumber is an open-source software testing tool written in Ruby. Cucumber enables


you to write test cases that anyone can easily understand regardless of their technical
knowledge.
Before understanding cucumber testing, let’s quickly go through the various types of
automation testing frameworks

1. Linear Scripting Framework


2. Modular Testing Framework
3. Data-driven Framework
4. Keyword-driven Testing Framework
5. Behavior-driven Development Testing Framework (BDD)

Cucumber Framework supports BDD


Behaviour-driven Development (BDD) is a software development technique that has
evolved from TDD (Test Driven Development), which is an approach or programming
practice where the developers write new code only when the automated test case fails.
Behavior-driven development’s approach involves the usage of shared language that
enhances communication between various tech and non-tech teams. Tests are more
user-focused and based on the system’s behavior. In BDD, “Given-When-Then” is the
proposed approach for writing test cases.

Consider the below example for better understanding:

 Given the user has entered invalid credentials


 When the user clicks submit button
 Then display the proper validation message

Benefits of using Cucumber Testing Tools


Involving stakeholders becomes easier regardless of their programming knowledge

 Testers can write Test scripts without having in-depth knowledge of programming
 Plugins are faster as compared to Selenium
 Supports various programming languages
 Code can be reused
 Simple and quick setup
 Flexible with different software platforms like Selenium, Ruby on Rails, Watir, Spring
framework, and so forth

Benefits of BDD in Cucumber Framework

 Focuses on defining ‘behavior’ rather than defining ‘tests’


 Enhances communication among the members of a cross-functional product team
 Helps reach a wider audience by the usage of non-technical language
 Enables you to understand how the system should perform from the developer’s and
customer’s perspective
 The improvement in the quality of code results in reduced costs of maintenance and
also minimizes the project’s associated risks
Below image describes a simple BDD operation –

Limitations of Behavior-driven development

 Testers must have prior experience in TDD (Test-driven Development) to work in BDD
 BDD approach may be ineffective if the requirements are not correctly analyzed
 Testers must have sufficient technical skills

What is Cucumber Framework


Cucumber is used to execute automated acceptance tests written in the “Gherkin”
language. Gherkin is a domain-specific language for behavior descriptions. Gherkin is
business readable.
Cucumber test automation makes use of two important files –

1. Feature file – Contains code written in Gherkin (plain English text)


2. Step definition file – Contains the actual code written by the developer
Below images represent sample Feature and Step definition files:
Feature file:

Step Definition File:

Cucumber acts as a bridge between the following teams:

 Business Analysts and Software Engineers


 Manual and Automation Testers
 Manual Testers and Developers

Cucumber with Selenium


Many organizations prefer the Selenium framework for cross browser compatibility
testing. These organizations also prefer integrating Cucumber with Selenium as it
makes it easier to read and understand the flow of applications among the members
from different teams. Gherkin syntax involves simple and plain text, which makes it
easier to understand test cases.
Cloud testing products like BrowserStack support Selenium testing with Cucumber

Difference between Cucumber Testing and Selenium Testing:


Cucumber Testing Selenium Testing

It uses functional and performance


It uses behavior driven development tool. (selenium grid) tool.

Plugin in cucumber works faster. Plugins are slower than cucumber.

Cucumber Framework supports other


language as well beyond Ruby like Java, Selenium supports Java, .Net and many
Scala, Groovy etc. other languages.

Like Cucumber Tool, writing


Writing automation steps are joint effort of automation steps are joint effort of
testers and developer. testers and developer.

Cucumber supports only web environment. Supports only web environment.

Introduction to Cucumber Framework

Test-driven development is the technique of using automated unit tests to implement


test coverage. Behavior driven development (BDD) is a branch of Test-driven
development (TDD) that uses human-readable language as user requirements for
software tests. The Cucumber framework is one of the BDD frameworks in the market.

Cucumber is a testing framework that supports Behavior Driven Development (BDD). It


is written in plain English text called Gherkin. It is defined as a scenario of inputs,
actions and outcomes. Gherkin interprets human input into the software concept of
input/process and actions. The following is how you write a Cucumber feature file in the
Gherkin Language. Each test is written in a feature file and has one or more scenarios
in it. Behavior driven development (BDD) is a good idea according to Agile methodology
and User Acceptance Testing (UAT). In the following example, we show how to test a
Facebook login functionality.
Feature: Each feature file begins with ‘Feature’ keyword. Every feature file should have
a feature extension. It gives a summary of what you will be testing.
Scenario: Each feature file contains one or more multiple scenarios. Each test is called
a scenario followed by three parts.

GIVEN: Defines precondition for the test. For example, if you want to verify that a
company logo is displaying, the precondition to verify the logo is displaying would be
that the user is at the Home Page.

WHEN: Defines that an action is performed by the user.

THEN: Defines the outcome of the previous steps.

AND: If you have multiple ‘WHEN,’ you can use ‘AND.’

Here is an example:

Feature: Home Page Test

Scenario: Facebook Login Functionality Test

Given I have a Firefox browser running

When I go to the Facebook.com

And I enter valid Email as “[email protected]

And I enter valid password as “JohnD123”

And I should click login button

Then I should see the Facebook logo on the top leftStep definitions are Java methods
with a set of Java code that links to one or more Gherkin feature files. When a feature
file is executed, it looks for a matching “step definitions” step.
A Cucumber runner class is one of the many mechanisms used to run a Cucumber
feature file. It uses the Junit runner class to run the files.

 Runner class acts as a link between the step definition class and the feature files. It is a
class where you provide the path for both feature files and step definitions.
 With the Runner class you have the option to run either one single feature or multiple
feature files.

 @RunWith annotations: This is a Junit annotation that specifies which runner has to
run execute this class. Cucumber.class is a parameter. Junit knows that this is a
Cucumber test.
 @CucumberOptions: This is an important annotation to run a Cucumber feature
file. features is a parameter that provides the path of the Feature file. Glue, another
parameter, provides the path of the step definitions class. plugin, generates an HTML
report on the provided location.

When we execute Cucumber scenarios, it automatically generates a report for the


passed and failed scenarios. It generates two types of reports: an HTML and a Json
report. Also, by adding a feature called extent reports, Cucumber can generate good-
looking HTML reports.

Cucumber is a very good BDD tool that provides testers a lot of flexibility and has large
community support. It’s not helpful to use the Cucumber framework when step
definitions grow a lot because it gets challenging to manage them.

Step-by-Step Implementation Process of BDD Framework Using Cucumber

Behavior Driven Development (BDD) Framework enables software testers to complete test
scripting in plain English. BDD mainly focuses on the behavior of the product and user
acceptance criteria. Cucumber is one of the best tools used to develop in the BDD
Framework. The Cucumber tool is used for development in various languages like Ruby,
Python, and JavaScript. It uses Gherkin, an ordinary language parser, that permits writing
scripts in English that can be easily understood. It is used to run the acceptance tests
written in BDD. Here we will walk you through what BDD, and why and how you
should be using Cucumber in BDD.

Advantages of the BDD Framework

 BDD scenarios are written in plain English, which is easy to understand by everyone
involved in the project, from stakeholders to project team members.
 Scenarios are written specifically to each functionality which gives clarity for product
development and testing.
 Be it manual or automated, testing becomes very easy as the test cases or the test scripts
can be easily identified from the feature files.
 The maintenance of the framework is simple.
 BDD also acts as project documentation for all modules and functionalities.

Why You Should Choose Cucumber for BDD Framework

 It serves as the automated test for test scripts and documentation for the project itself.
 Test scenarios are clearly organized in the feature files.
 Tests are written based on user stories and system behavior corresponding to the user
story in English using annotations like Given, When, Then.
 Code can be written in any language like Java, Python, Perl, etc.

Features of Cucumber
Gherkin: In BDD, the scenarios are written in a language named Gherkin. Gherkin uses a
simple syntax and keywords are easily understood by anyone like business users and
stakeholders. The feature files which store the scenarios under test are written in Gherkin.
Gherkin files should have the .feature file extension. Every feature file contains an
individual scenario under test.

Features: Feature is a use case that describes the module under test. The feature is
composed of the following details.

 The keyword “Feature:” is appended before the name of the feature.


 The feature name is written beside the feature keyword.
 An optional description is also written below the feature to describe the crux of the
feature.
Scenario: A scenario is a series of steps involved in the test execution.

Scenario Outline: Used where test data is replaced with multiple sets of data for each run
of a test script.

Implementation of BDD Framework in Selenium Webdriver


As an example of an implementation of the test script using Cucumber, we are going to
use a maven project in Selenium Webdriver. Before we start with the implementation, we
need to add the dependency for Cucumber in pom.xml as we are using a maven project
in this scenario. Here we are covering two scenarios.

Scenario 1

In this scenario we check the login functionality with a single user:

1. Launch the Swag labs page


2. Verify the Swag labs login page
3. Enter the username
4. Enter the Password
5. Click on the Login button
6. Verify the Sauce demo home page is displayed
7. Logout of the application.
Below is the Loginmethods.java class for multiple sets of data to check the login
functionality for a single user.
1.Loginmethods.java

Functionality Methods are defined in this


class:
2. LoginPage.feature.java

Feature file: Steps for the scenario is written in this


class:

3. runTest.java

Runner class: Class to run the feature


file:
4. Results in Junit

Below are the Feature file run results in the


Junit:

5. Test results in Console Window

Below are the test results in the Console


window:

Let us see the same implementation using multiple sets of data in the below scenario:

Scenario 2

In this scenario, we check the login functionality with multiple users:

1. Launch the Swag labs page


2. Verify the Swag labs login page
3. Enter the username
4. Enter the Password
5. Click on the Login button
6. Verify the Sauce demo home page is displayed
7. Logout of the application

Below is the Loginmethods.java class for multiple data sets to check the login
functionality for multiple users.

1. Loginmethods.java

Methods for the functionality are defined in this class:

Here we define the step as:

@When ("^User enters the username as \"([^\”] *)\"$")

public void enter_the_username(String ar1) {

driver.findElement(By.id("user-name")).isDisplayed();

driver.findElement(By.id("user-name")).sendKeys(ar1); }

Here \"([^\"]*)\"$" denotes the user expression for multiple sets of data

The method “enter_the_username” takes String \"([^\"]*)\"$" as an argument, which is


passed from the examples during runtime.

2. LoginPage.feature.java
Feature file: Steps for the scenario is written in this
class:

Scenario Outline is the tag name used for testing multiple sets of data.

Examples section: To define the multiple data sets.

Here the “<username>” and <”password”> are replaced with the username and
password combinations mentioned in the Examples section

Examples:

| username | password |

| standard_user | secret_sauce |

| performance_glitch_user | secret_sauce|

3. runTest.java

Runner class: Class to run the Feature file.


4. Results in Junit

Below are the Feature file run results in the


Junit:

5.Test Results in the Console window

Below are the test results in the Console window:


Cucumber Interview Questions for Freshers

1. What is Cucumber? Explain the need of using Cucumber.

Cucumber is a behavior-driven development (BDD) testing tool. The BDD framework's


major goal is to bring together a variety of project responsibilities, such as quality
assurance, developers, and business analysts, to understand the application without
diving too deeply into the technical components.
Testers use Cucumber to create test cases for evaluating program behaviour. It is an
important tool to automate acceptance tests written in logical language that
customers can understand. It's primarily used to develop acceptance tests for web
apps based on their features' behaviour.
2. What is Gherkin Language?

Gherkin is a readable business language that allows you to define business activity
without getting bogged down in implementation specifics. It's a domain-specific
language for defining specs tests in Cucumber format. It describes use cases in plain
English and helps users to remove logic elements from behaviour testing.

3. What is the principle of Behaviour Driven Development?

Behaviour Driven Development (BDD) is a synthesis and refinement of practices


stemming from Test Driven Development (TDD) and Acceptance Test-Driven
Development (ATDD). BDD augments TDD and ATDD by
applying the “Five Why’s” principle to each proposed user story so that its purpose is
clearly related to business outcomes. Five Why's is an iterative interrogative approach
for uncovering the cause-and-effect links at the root of a problem. The main purpose
of this technique is to uncover the core cause of a flaw or problem by asking "Why?"
repeatedly. Each response serves as the foundation for the next question.

4. What are the primary keywords in Cucumber?

Following are the primary keywords in Cucumber:-

 Feature: The Feature keyword's aim is to collect relevant scenarios and provide a high-
level description of a software feature.
 Rule: The Rule keyword is used to express a single business rule that should be
followed. It adds to the information about a feature.
 Example: This is a practical illustration of a business rule. It comprises a series of steps.
 Given: The given steps are used to describe the system's initial context - the scenario's
scene. It usually refers to an event that occurred in the past.
 When: When describing an occurrence or an action, When is employed. It could be a
user interacting with the system or an event generated by another system.
 Then: Then steps are employed to indicate an anticipated outcome, or result.
 Background: A background helps you to give the situations that follow it some
context. It can have one or more Given steps, which are executed prior to each
scenario but after any Before hooks.

5. Which language is used in Cucumber?


Cucumber understands Gherkin. It's a straightforward English representation of the
app's functionality. It is used for defining test cases. It is intended to be non-technical
and human-readable, and it describes use cases for a software system as a whole. It's a
domain-specific (DSL), business-friendly language.

6. What do you mean by scenario in Cucumber Testing?

Scenario is a fundamental Gherkin structure. Every scenario begins with the keyword
"Scenario:" (or a localized version of it) and ends with a scenario title. Every feature can
have one or more scenarios, each of which has one or more steps.

As an example of a scenario, consider the following:

Scenario − Verify My Orders Functionality.


Explanation: When a user clicks on the My Orders option he/ she should be taken to
the My Orders page.

7. What do you mean by Scenario Outline?

Consider the situation when we need to run a test scenario multiple times. Assume we
need to ensure that the login feature is functional for all types of subscribers. This
necessitates repeating the login functionality scenario. Copying and pasting the
identical instructions to just re-run the code does not appear to be a good approach.
Gherkin adds another framework, the scenario outline, to help with this. The scenario
outline is similar to scenario, with the exception that several inputs are provided.
Example:-

Scenario Outline - Sign In Feature for a website.


Explanation: The website can have multiple users and so we need to consider all the
users while implementing the sign-in functionality.

9. What are the basic requirements to run Cucumber Web test cases?

We need the following minimum requirements to successfully run a Cucumber Web


test case:-

1. The compiler and the development kit for the programming language we will
be using. Example: JDK and JRE for using Java as our programming language.
2. An IDE (Integrated Development Environment) wherein we can write our code.
Example: Eclipse.
3. Build tools so as to do tasks such as compiling code, packaging code to a jar,
creating source code. Example: Maven, Gradle.

10. What are the advantages of using Cucumber?

Following are the advantages of using Cucumber:-

 Cucumber supports a variety of programming languages, including Java.net and Ruby.


 It serves as a link between commercial and technical language. This can be done by
writing a test case in plain English text.
 It enables the test script to be developed without any prior knowledge of
programming, as well as the participation of non-programmers.
 Unlike other tools, it functions as an end-to-end test framework.
 Cucumber allows for code reuse thanks to its simple test script architecture.

11. What are Step Definitions in the context of Cucumber?

Step definitions connect Gherkin steps to programming code. The mapping between
each step of the scenario defined in the feature file and a code of the function to be
executed is stored in the steps definition file. A step definition carries out the action
that should be performed by the step. So step definitions hard-wire the specification
to the implementation.

or

Step definition maps the Test Case Steps in the feature files to code. It executes the
steps on Application Under Test and checks the outcomes against expected results. In
order to execute step definition it must match the given component in a feature

12. What are annotations in Cucumber?

An annotation is a type of text that has been pre-defined and has a specified meaning.
It tells the compiler/interpreter what to do when the program runs. The annotations on
Cucumber are as follows:

 Given: It specifies the requirements for running the test.


Example: Given I have an account on Interviewbit.
 When: It establishes the starting point for any test scenario.
Example: When I log in to Interviewbit.
 Then: It contains the expected result of the test which is to be executed.
Example: Then registration should be successful.
 And: Between any two statements, it gives the logical AND condition. AND can be
combined with the GIVEN, WHEN, and THEN statements.
Example: When I enter my account number AND CVV.
 But: It denotes a logical OR relationship between two propositions. OR can be
combined with the GIVEN, WHEN, and THEN statements.
Example: Then I should be logged in BUT I must enter the OTP.

13. Enlist the files needed in the Cucumber framework.

The following are the files required for a Cucumber framework:

 Feature File: It has plain text descriptions of single or numerous test situations.
Keywords like Then, When, Background, Scenario Outline, Feature, And, But, and so on
are used in the tests. As a result, it's a file that keeps track of features and their
descriptions.
 Step Definition File: It has the extension .java. It essentially acts as a translator
between the test scenario steps provided in the feature file and the automation code.
Cucumber searches the step definition file and executes the relevant functions that are
assigned to that step when it runs a step described in the feature file.
 TestRunner: .java is the file extension for this file. It connects the feature file and the
step definition file. It allows the user to run one or more feature files at the same time.
It contains the locations of the step definition and feature files.

14. How do you comment the code in Cucumber? What is the importance of
comments?

A comment is a chunk of code that is intended for documentation rather than


execution. To make it more legible and clear, whether it's a step definition file or a
feature file. As a result, it's critical to use/insert comments in the right places
throughout the file. This is also beneficial for troubleshooting the code. Comments can
be added to Cucumber feature files at any time. To add comments, simply begin the
statement with the “#” sign.
Different programming languages have different standards for commenting. Let's see
how Cucumber handles the situation:

 For Step Definition File, if you're using Java as a platform, start your comments with
"/."
 In the case of a feature file, we only need to type # before starting our comment.

15. What are hooks in Cucumber?

Hooks are code blocks that execute before or after each Cucumber scenario in the
execution cycle. This enables us to better control the development workflow and
decrease code redundancy. Setting up the web driver and terminating the web driver
session resembles a test setup. When dealing with different scenarios, it's best to do
the setup and clean up only once. Hooks are used to bringing optimization.

Certain preconditions, such as executing the program, creating a database connection,


preparing the test data, and so on, may be required in some cases. There are also
several postconditions to be fulfilled, such as ending the database connection, closing
the browser, refreshing test data, and logging out of the program. Cucumber handles
all of these situations with the use of hooks.

The methods @Before and @After can be used to define hooks anywhere in the
project or step definition layers. Before hook is executed before any other test
situations, and after the hook is executed after all test scenarios have been completed.

16. What are tags in Cucumber and why are they important?

When we only have one, two, or maybe five situations in a feature file, it appears to be
simple. In reality, however, this does not occur. In a single feature file, we may have 10,
20, or even more scenarios for each feature under test. They could reflect various
purposes (smoke test/regression test), perspectives (developer/QA/BA), and statuses
(ready for execution/work in progress).
Tags in cucumber provide a way to run scenarios in a specific sequence from a runner
file. Each situation can be labeled with a useful tag. Later, in the runner file, we may
specify which tag (and hence which scenario(s)) Cucumber should run. “@” is the first
character in a tag. Any relevant content after "@" can be used to define your tag.
Example - ‘@InitialTest’

17. What is Cucumber Dry Run?


The purpose of the Cucumber dry run is to verify compilation faults and compile the
Step Definition and Feature files. Dry run's value might be either true or false. Dry run
has the value false by default and it is present in the Test Runner Class file.

If the dry run value is true, Cucumber will check all steps in the Feature file. Within the
Step Definition file, it will also check the implementation code of steps in the Feature
file.

If any of the steps in the Feature file is missing its implementation in the Step
Definition file, a message is thrown. The @CucumberOptions has a dry run parameter
that is used to configure the test parameters.

18. What do you mean by profile in Cucumber?

When testing a feature, cucumber profiles make it simple to define groupings of tests
in a feature file so that we can choose to execute only a subset of them rather than all
of them. It was created to help people save time. In a cucumber.yml file, the user can
reuse commonly used cucumber flags.

19. What programming languages are used by Cucumber?

Cucumber supports a variety of programming languages, including Java,.NET, Ruby,


and others. It can also be used with other tools like Capybara and Selenium.

Cucumber Interview Questions for Experienced

21. What is a test harness in the context of Cucumber?

The test harness in Cucumber helps in separating the task of establishing the context
and interacting with the browser from cleaning up the step definition files. It gathers
the stubs, drivers, and other tools needed to enable test execution automation in
testing.

The following is the purpose of the test harness:-

 To run a set of tests either within the framework or with the help of the test harness
 To enter data into the program being tested.
 Debugging becomes more flexible and supported.
 To record the outputs of the software under test
 To keep track of the test results (pass/fail) for each test.
 Aids developers in determining code coverage at the code level.

The advantages of the test harness are as follows:-

 As a result of automation, productivity increases.


 Improved software quality as a result of automation allows us to be more productive.
 Tests can be scheduled.
 Can handle complex conditions that testers have a hard time simulating.

22. What is the difference between RSpec and Cucumber?

RSpec and Cucumber are two examples of testing frameworks. Traditional Unit Testing
is used by RSpec. It refers to the practice of testing a section of an application
separately from the remaining part of the application. As a result, your model performs
what it's expected to do, the controller does what it's expected to do, and so on. Both
RSpec and Cucumber are used for Acceptance Testing, also known as ATDD, BDD, and
other terms.
The following are the major differences between RSpec and Cucumber:-

 The fundamental distinction between RSpec and Cucumber is the element of business
readability.
 Unit testing is the primary purpose of RSpec. Cucumber, on the other hand, is primarily
utilized in behavior-driven development. It can be used for System and Integration
Testing as well.
 Cucumber separates the specs or features from the test code, allowing product owners
to provide or review the specification without having to walk through the code.
 RSpec includes a similar method, but instead of elaborating a step with a Describe, it
executes the statement using the business specification. This method is easier for
developers to use, but a little more difficult for non-technical people.

23. Difference between Selenium and Cucumber.

Open-source testing tools, Selenium and Cucumber are both used for functional
testing. However, there are some distinctions between them.

Here are some key distinctions between Selenium and Cucumber:

 Cucumber is a behavior-driven development automation tool that may be used with


Selenium. Selenium is a web browser automation tool for web projects (or Appium).
 Cucumber is used for acceptance testing, while Selenium is used for automated UI
testing.
 Technical teams (SDETs/programmers) favour Selenium, while non-technical teams
often choose Cucumber (business stakeholders and testers).
 Cucumber isn't required for Selenium to work. Cucumber's step-definition
implementation is based on Selenium or Appium.
 The script creation with Selenium is complicated, whereas Cucumber is
straightforward.

24. Why do we need to use Cucumber with Selenium?

Cucumber and Selenium are two widely used testing frameworks and
technologies. Selenium is widely used for functional testing in many organizations.
These companies use Cucumber in conjunction with Selenium because Cucumber
makes the application flow easier to read and comprehend. The most important
advantage of combining Cucumber and Selenium is that it allows developers to build
test cases in simple feature files that managers, non-technical stakeholders, and
business analysts can understand. It allows you to develop tests in Gherkin, a human-
readable programming language. Java,.NET, PHP, Python, Perl, and other
programming languages are supported by the Selenium-Cucumber framework.

25. In a feature file, what is the maximum number of scenarios?

A feature file in Cucumber can include a maximum of 10 scenarios. This quantity can
differ from one project to the next and from one organization to the next. It's
advisable to keep the number of scenarios in the feature file to a minimum.

26. What do you mean by Test Driven Development (TDD)?

TDD is an abbreviation that stands for Test-Driven Development. This is a development


practice in which the test cases are created first, followed by the code that underpins
the test cases. TDD may also be used to construct automation testing. TDD takes
longer to develop due to the fact that it finds fewer flaws. The TDD development
practice has increased the quality of code, which is more reusable and flexible as a
result. TDD also aids developers in achieving high test coverage, ranging from 90% to
100%. The sole disadvantage of TDD for developers is that they must build test cases
before producing code.
The following is a list of the TDD methodology's basic six-step process:

 First, all the test cases are written. Based on your requirements, you must create an
automated test case.
 Carry out all of the tests: Carry out these automated test cases on the code that has
been developed so far.
 Modify the code for that test case: You must develop the code to make that test
casework as intended if it fails throughout this step.
 Rerun the test cases: Now you must rerun the test cases to ensure that all of the
previously developed test cases have been implemented.
 Modularize your code as follows: This is a step that can be skipped. However,
refactoring your code to make it more readable and reusable is recommended. That is
why it is necessary.
 For new test scenarios, repeat steps 1–5: This is the final phase in the process. You
must now repeat the process for the remaining test cases till all of them have been
implemented.

27. Difference between TDD and BDD.

TDD BDD
Test-Driven Development (TDD) is a
method of developing software that is BDD is an acronym for behavior-driven
driven by tests. This means that the development. It's a behavior-based
developers must first write the test development approach.
cases before writing the code.
Given-When-Then steps are used to write BDD
TDD tests are developed in a variety of
tests in a human-readable fashion. Non-
programming languages, including
technical people may read and comprehend
Java,.NET, Python, Ruby, and others.
these tests as well.
The scope is the key distinction
BDD, on the other hand, is a collaborative
between TDD and BDD. TDD is a
methodology.
development methodology.
When a test fails because the specified Creating an executable specification that fails
function does not exist, TDD because the feature isn't available, then
recommends writing the simplest code writing the simplest code possible to make the
possible to pass the test, then spec pass in BDD. This process is repeated
TDD BDD
reworking to remove duplication, and until a release candidate is ready to be
so on. delivered.
Users or testers write automated specifications
The test cases are written by the
in BDD, which are then wired to the code
developers in TDD.
under test by developers.
Because TDD tests are written in specific
programming languages, they are Non-programmers can read BDD tests since
difficult to interpret by non- they are written in a human-readable format.
programmers.

28. What is the use of the Options tag in the Cucumber Framework?

The Options tag is a part of the TestRunner file in the Cucumber framework, and it
takes the form of an annotation named @CucumberOptions.

It has two parameters: glue and feature:

1. Feature: The path to the feature file is specified by the feature option.
2. Glue: The glue argument is used to provide the step definition file's location.

Example:

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith (Cucumber.class)
@CucumberOptions (
features = "src/test/Sample/features ",
glue = {"StepDefinitionFile"}
)
public class SampleTestRunner {
}

29. How does the execution start in Cucumber?

Cucumber execution will begin at the support level. In support, it will first load the
env.rb file, then hooks.rb, and last start executing feature file scenario steps.

30. What is grouping in the context of Cucumber?


Cucumber is unconcerned about the names of your step definition files or the order in
which you place them. Instead of maintaining all steps in a single file, we can create
steps.rb file for each major action/feature. This is referred to as grouping.

31. How can you run Cucumber tests parallelly?

The Cucumber JVM Parallel Plugin, which may be used with Serenity BDD, can be used
to conduct parallel tests in Cucumber. The plugin will look in the src/test/resources
directory for feature files. After that, it will create runners for each file.

32. What are some of the prerequisites that you should consider while building a
Selenium Cucumber automation application?

We consider the following before building a Selenium Cucumber automation


application:-

 Determine the type of application you'll be testing. Is it a Web app, a mobile app, or a
desktop application?
 Is there a need for backend testing? Databases or SDKs, for example.
 Is it necessary to run the app through an internationalization test?
 It must include a report that allows you to track down a problem with minimal effort.
 It must be able to generate parametrization tests automatically.
 Any setup-related settings or global attributes should be defined in a config file.
 To segregate the functionality, use abstraction at every level.

33. Difference between JBehave and Cucumber.

Despite the fact that Cucumber and JBehave have the same goal in mind, acceptance
tests are two quite distinct frameworks:

 Cucumber is built on Ruby, while JBehave is a pure Java Framework.


 Cucumber is built on features, whereas JBehave is based on stories.

34. How can you run a selected test from a group of tests in Cucumber?

We may execute a single test from a set of tests in the Cucumber framework using the
tags idea. This is found in the TestRunner file's @CucumberOptions section. With the
use of the @t<agname> keyword, we may tag a scenario in the feature file. A scenario
can have one or more tags within the feature file. We can separate test scenarios with
the assistance of tagging. We must pass the <tagname> value within the tags
argument to execute a selected test in Cucumber, and we must pass the <~tagname>
value within the tags parameter to exclude a test from running.

How to generate reports with Cucumber?


We can generate the output/report of the cucumber using different cucumber
commands.

>cucumber adding.feature –format HTML


>cucumber adding.feature –out report.html
>cucumber adding.feature –format pretty

The report file will be stored in the project folder itself.

How to achieve parameterization in the Cucumber framework?


Answer: We can achieve parameterization in Cucumber. This helps to pass multiple data
sets at runtime in multiple combinations. We can perform data parameterization in the
following ways:

 With the help of keyword Examples.


 Without the help of keyword Examples.

The Scenario Outline in a feature file should be accompanied by the Examples part. This
consists of the multiple data sets to be passed at runtime.

The Examples section in the feature file should have headings that match with the
variables in the Scenario Outline followed by (|) symbol. Each row below the heading
represents a set of data. So if there are two rows after heading, the test scenario shall
run two times with the two different sets of data.

Let us consider the feature file having the Feature New User Registration described
previously. Next let us now create a step definition file corresponding to that feature file.

Step Definition file implementation with Examples.

1 import cucumber.api.java.en.Given;
2 import cucumber.api.java.en.Then;
3 public class ParameterizationWithExample {
4 @Given ("^User navigates to Registration Page$")
5 public void navigate_to_registration () {
6 System.out.println ("Navigation to registration is done");
7 }
8
9 @Then ("^User inputs \"(.*)\" and \"(.*)\" and \"(.*)\"$")
10 public void user_input (String fname, String lname, String email){
11 System.out.println ("First name is: " + fname);
12 System.out.println ("Last name is: " + lname);
13 System.out.println ("Email is: " + email); }
14 }
In the above example, we have passed three values Firstname, Lastname and Email at
the run time without requiring to hardcode test data inside the step definition file. With
the help of User inputs \”(.*)\” and \”(.*)\” and \”(.*)\ statement, Cucumber
understands that there are three runtime parameters. Also, three parameters are passed
as arguments to the user_input method.

Now let us see how to do data parameterization without Examples.

Feature file implementation.

Feature: Launch Software Testing Material application


Scenario: Software Testing Material launching URL
Given Navigate to “https://www.softwaretestingmaterial.com/”

In the above example, we have passed the URL directly within the Given statement in
the feature file.

Step Definition code Implementation.

1 import cucumber.api.java.en.Given;
2 import cucumber.api.java.en.Then;
3 public class ParameterizationWithoutExample {
4 @Given ("^Navigate to \"([^\"]*)\"$")
5 public void navigate (String url) {
6 System.out.println ("URL is: " + url); }
7 }
In the above example, we have passed one
value https://www.softwaretestingmaterial.com/ that executes at run time without
requiring to hardcode test data inside the step definition file. With the help of Navigate
to \”([^\”]*)\ statement, Cucumber understands that there is one runtime parameter.
Also, this parameter is passed as an argument to the navigate method.

Interview Questions
Q #1) Explain Cucumber shortly.
Answer: Cucumber is a tool that is based on Behavior Driven Development (BDD)
methodology.
The main aim of the Behavior Driven Development framework is to make various project
roles such as Business Analysts, Quality Assurance, Developers, etc., understand the
application without diving deep into the technical aspects.
Q #2) What language is used by Cucumber?
Answer: Gherkin is the language that is used by the Cucumber tool. It is a simple
English representation of the application behavior. Gherkin language uses several
keywords to describe the behavior of applications such as Feature, Scenario, Scenario
Outline, Given, When, Then, etc.
Q #3) What is meant by a feature file?
Answer: A feature file must provide a high-level description of an Application Under
Test (AUT). The first line of the feature file must start with the keyword ‘Feature’
followed by the description of the application under test.
A feature file may include multiple scenarios within the same file. A feature file has the
extension .feature.

Q #4) What are the various keywords that are used in Cucumber for writing a
scenario?
Answer: Mentioned below are the keywords that are used for writing a scenario:
 Given
 When
 Then
 And
Q #5) What is the purpose of a Scenario Outline in Cucumber?
Answer: Scenario outline is a way of parameterization of scenarios. This is ideally used
when the same scenario needs to be executed for multiple sets of data, however, the
test steps remain the same. Scenario Outline must be followed by the keyword
‘Examples’, which specify the set of values for each parameter.
Q #6) What programming language is used by Cucumber?
Answer: Cucumber tool provides support for multiple programming languages such as
Java, .Net, Ruby etc. It can also be integrated with multiple tools such as Selenium,
Capybara, etc.
Q #7) What is the purpose of the Step Definition file in Cucumber?
Answer: A step definition file in Cucumber is used to segregate the feature files from
the underlying code. Each step of the feature file can be mapped to a corresponding
method on the Step Definition file.
While feature files are written in an easily understandable language like, Gherkin, Step
Definition files are written in programming languages such as Java, .Net, Ruby, etc.

Q #8) What are the major advantages of the Cucumber framework?


Answer: Given below are the advantages of the Cucumber Gherkin framework that
make Cucumber an ideal choice for rapidly evolving Agile methodology in today’s
corporate world.
 Cucumber is an open-source tool.
 Plain Text representation makes it easier for non-technical users to understand
the scenarios.
 It bridges the communication gap between various project stakeholders such as
Business Analysts, Developers, and Quality Assurance personnel.
 Automation test cases developed using the Cucumber tool are easier to maintain
and understand as well.
 Easy to integrate with other tools such as Selenium and Capybara.
Q #9) Provide an example of a feature file using the Cucumber framework.
Answer: Following is an example of a feature file for the scenario ‘Login into the
application’:
Feature: Login to the application under test.
Scenario: Login to the application.
 Open the Chrome browser and launch the application.
 When the user enters the username onto the UserName field.
 And User enters the password into the Password field.
 When the user clicks on the Login button.
 Then validate if the user login is successful.
Q #10) Provide an example of a Scenario Outline using the Cucumber framework.
Answer: The following is an example of a Scenario Outline keyword for the scenario
‘Upload a file’. The number of parameter values to be included in the feature file is
based on the tester’s choice.
Scenario Outline: Upload a file
Given that the user is on upload file screen.
When a user clicks on the Browse button.
And user enters <filename> onto the upload textbox.
And user clicks on the enter button.
Then verify that the file upload is successful.

Example:
|filename|
|file1|
|file2|

Q #12) What is the limit for the maximum number of scenarios that can be
included in the feature file?
Answer: A feature file can contain a maximum of 10 scenarios, but the number can
vary from project to project and from one organization to another. But it is generally
advisable to limit the number of scenarios included in the feature file.
Q #13) What is the use of Background keyword in Cucumber?
Answer: Background keyword is used to group multiple given statements into a single
group. This is generally used when the same set of given statements are repeated in
each scenario of the feature file.
Q #14) What symbol is used for parameterization in Cucumber?
Answer: Pipe symbol (|) is used to specify one or more parameter values in a feature
file.
Q #15) What is the purpose of Examples keyword in Cucumber?
Ans: Examples keyword is used to specify values for each parameter used in the
scenario. Scenario Outline keyword must always be followed by the keyword Examples.
Q #16) What is the file extension for a feature file?
Answer: File Extension for a feature file is .feature. A feature file is ideally written in a
notepad file and is saved with the extension feature.
Q #17) Provide an example of a step definition file in Cucumber.
Answer: Step definition corresponding to the step “Open Chrome browser and launch
the application” may look like the code mentioned below:
@Given("^Open Chrome browser and launch the application$")

public void openBrowser()

driver = new ChromeDriver();

driver.manage().window().maximize();

driver.get("www.facebook.com");

Q #18) What is the purpose of the Cucumber Options tag?


Answer: Cucumber Options tag is used to provide a link between the feature files and
step definition files. Each step of the feature file is mapped to a corresponding method
on the step definition file.
Below is the syntax of Cucumber Options tag:
@CucumberOptions(features="Features",glue={"StepDefinition"})

Q #19) How can Cucumber be integrated with Selenium WebDriver?


Answer: Cucumber can be integrated with the Selenium Webdriver by downloading the
necessary JAR files.
Given below are the list of JAR files that are to be downloaded for using
Cucumber with Selenium web driver:
 cucumber-core-1.2.2.jar
 cucumber-java-1.2.2.jar
 cucumber-junit-1.2.2.jar
 cucumber-jvm-deps-1.0.3.jar
 cucumber-reporting-0.1.0.jar
 gherkin-2.12.2.jar
Q #20) When is Cucumber used in real-time?
Answer: Cucumber tool is generally used in real-time to write acceptance tests for an
application. It is generally used by non-technical people such as Business Analysts,
Functional Testers, etc.
Q #21) Provide an example of Background keyword in Cucumber.
Answer:
Background: Given the user is on the application login page.
Q #22) What is the use of Behavior Driven Development in Agile methodology?
Answer: The advantages of Behavior Driven Development are best realized when non-
technical users such as Business Analysts use BDD to draft requirements and provide
the same to the developers for implementation.
In Agile methodology, user stories can be written in the format of feature file and the
same can be taken up for implementation by the developers.

Q #23) Explain the purpose of keywords that are used for writing a scenario in
Cucumber.
Answer:
 “Given” keyword is used to specify a precondition for the scenario.
 “When” keyword is used to specify an operation to be performed.
 “Then” keyword is used to specify the expected result of a performed action.
 “And” keyword is used to join one or more statements together into a single
statement.
Q #24) What is the name of the plugin that is used to integrate Eclipse with
Cucumber?
Answer: Cucumber Natural Plugin is the plugin that is used to integrate Eclipse with
Cucumber.
Q #25) What is the meaning of the TestRunner class in Cucumber?
Answer: TestRunner class is used to provide the link between the feature file and the
step definition file. The next question provides a sample representation of how the
TestRunner class will look like. A TestRunner class is generally an empty class with no
class definition.
Q #26) What is the use of glue property under the Cucumber Options tag?
Answer: Glue property is used to let the Cucumber framework identify the location of
step definition files.

Q #27) What is the starting point of execution for feature files?


Answer: When integrated with Selenium, the starting point of execution must be from
the TestRunner class.
Q #28) Should any code be written within the TestRunner class?
Answer: No code should be written under the TestRunner class. It should include the
tags @RunWith and @CucumberOptions.
Q #29) What is the use of features property under the Cucumber Options tag?
Answer: Features property is used to let the Cucumber framework identify the location
of the feature files.
Q #30) What is the maximum number of steps that are to be written within a
scenario?
Answer: 3-4 steps.

You might also like