Google News
logo
Cucumber Interview Questions
Cucumber is a software tool that supports behavior-driven development (BDD) framework. Central to the Cucumber BDD approach is its ordinary language parser called Gherkin.
 
Cucumber reads executable specifications written in plain text and validates that the software does what those specifications say. The specifications consists of multiple examples, or scenarios. For example :
Scenario: Breaker guesses a word
  Given the Maker has chosen a word
  When the Breaker makes a guess
  Then the Maker is asked to score
Each scenario is a list of steps for Cucumber to work through. Cucumber verifies that the software conforms with the specification and generates a report indicating success or failure for each scenario.
Gherkin is a set of grammar rules that makes plain text structured enough for Cucumber to understand. The scenario above is written in Gherkin.
 
Gherkin serves multiple purposes :
 
* Unambiguous executable specification
* Automated testing using Cucumber
* Document how the system actually behaves
Gherkin
The Cucumber grammar exists in different flavours for many spoken languages so that your team can use the keywords in your own language.
 
Gherkin documents are stored in .feature text files and are typically versioned in source control alongside the software.
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.
Following are the two files required to execute a Cucumber test scenario:
 
* Features

* Step Definition
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.
A project’s feature is defined as a stand-alone unit or functionality. A list of scenarios that need to be tested for a feature is often included. This feature file is a file that consists of features, descriptions of features, and situations that need to be evaluated.
Even though both of them are used for the same purpose, the acceptance tests have completely different frameworks :
 
* Cucumber is Ruby-based, while JBehave is purely Java framework.

* Cucumber is based on features, while JBehave is based on stories.
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 .
Which programming languages are supported by Cucumber?
The Cucumber tool supports multiple programming languages such as Java, .Net, Ruby, etc. You can also integrate this tool easily with multiple tools such as Selenium, Capybara, etc.
10 .
What do you understand by regular expressions?
A regular expression is a pattern used to describe a certain amount of text. The most basic regular expression consists of a single literal character.
Following software is used to run a Cucumber Web Test case :
 
* Cucumber
* Ruby and its Development Kit
* IDE like ActiveState
* Watir ( To simulate browser)
* Ansicon and RSpec (if required)
Following is the list of advantages of the Cucumber framework that makes Cucumber an ideal choice for rapidly evolving agile methodology in today's corporate world :
 
* Cucumber is an open-source and free-to-use tool.

* It is easy to use. Even non-technical users can also understand the scenarios because of its plain text representation.

* It bridges the communication gap between various project stakeholders such as Business Analysts, Developers, and Quality Assurance personnel.

* With the Cucumber tool, we can develop automation test cases that are easier to maintain and understand.

* It is easy to integrate with other tools such as Selenium and Capybara.
For example to make visitor visit the site “google” the command we use for given
Given (/^ I am on www.google.com$/) do

Browser.goto “https://www.google.com.”

end – This will visit www.google.com
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.
We need the following minimum requirements to successfully run a Cucumber Web test case :
 
* 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.

* An IDE (Integrated Development Environment) wherein we can write our code. Example : Eclipse.

* Build tools so as to do tasks such as compiling code, packaging code to a jar, creating source code. Example : Maven, Gradle.
BDD is a methodology to understand the functionality of an application in the simple plain text representation.
 
The main aim of the Behavior Driven Development framework is to make various project roles such as Business Analysts, Quality Assurance, Developers, Support Teams understand the application without diving deep into the technical aspects.
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"})
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
19 .
What symbol is used for parameterization in Cucumber?
Pipe symbol (|) is used to specify one or more parameter values in a feature file.
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.
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.

But” It denotes a logical OR relationship between two propositions. OR can be combined with the GIVEN, WHEN, and THEN statements.
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.
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.
24 .
What is the starting point of execution for feature files?
When integrated with Selenium, the starting point of execution must be from the TestRunner class.
25 .
Should any code be written within the TestRunner class?
No code should be written under the TestRunner class. It should include the tags @RunWith and @CucumberOptions.
RSpec and Cucumber both are the example of testing frameworks. RSpec uses traditional Unit Testing. It means it uses testing a class or part of the application in isolation from the rest of the application. So your model does what your model is supposed to do, the controller does what it is supposed to do, etc. RSpec and Cucumber both are used for Acceptance Testing, also called ATDD, BDD, etc.
 
Difference between RSpec and Cucumber
 
* The main difference between RSpec and Cucumber is the business readability factor.

* RSpec is mainly used for Unit Testing. On the other hand, Cucumber is mainly used for Behavior-driven development. We can also use it for System and Integration Testing.

* In Cucumber, the specifications or features are separate from the test code, so the product owners can provide or review the specification without going through the code. These are the .feature files that you make in Cucumber.

* RSpec also has a similar mechanism, but instead of describing a step with a Describe or Context, it uses the business specification to execute that statement. This approach is a little easier for developers to work with but a bit harder for non-technical guys.


Which should we use?
 
* For a core developer, it is the best choice to use RSpec. It is easier to understand for a technical person and offers a few advantages in keeping things scoped and under control because you don't have to mess up with RegExs for test steps.

* If you are building this for a client, you should choose Cucumber for Acceptance Testing and use RSpec for Unit Testing.
The advantages of the test harness are :
 
* Because of automation, productivity levels increase.

* Better software quality owing to the improved productivity because of automation.

* Tests can be scheduled.

* Complicated conditions that testers have a hard time simulating are easily handled.
Selenium and Cucumber are both open-source testing tools, and both are used for functional testing. But there are some differences between them.
 
Following are some critical differences between Selenium and Cucumber :
 
* Selenium is a web browser automation tool for web apps, while Cucumber is an automation tool for behavior-driven development that can be used with Selenium (or Appium).

* Selenium is used for automated UI testing, while Cucumber is used for acceptance testing.

* Selenium is preferred by technical teams (SDETs/programmers), while Cucumber is typically preferred by non-technical teams (business stakeholders and testers).

* Selenium can work independently of Cucumber. Cucumber depends on Selenium or Appium for step-definition implementation.

* In Selenium, the script creation is complex, while Cucumber is simpler than Selenium.
Cucumber and Selenium are both testing frameworks and prevalent technologies. Many organizations use Selenium for functional testing. Along with Selenium, these organizations integrate Cucumber with Selenium as Cucumber makes it easy to read and understand the application flow.

The most significant benefit of using Cucumber with Selenium is that it facilitates developers to write test cases in simple feature files easily understood by managers, non-technical stakeholders, and business analysts. It provides the facility to write tests in a human-readable language called Gherkin. The Selenium-Cucumber framework supports programming languages such as Java, .NET, PHP, Python, Perl, etc.
Cucumber will not take care how you name your step definition files and what step definitions you put in which file.
 
You can create steps.rb file for each Major operation / Features instead of keeping all steps in a single file. This process is called a grouping.
 
Example : 
 
Under features folder
Adding_steps.rb
Multiplying_steps.rb
You 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.
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
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.
34 .
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
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.
TDD BDD
Test-Driven Development (TDD) is a method of developing software that is driven by tests. This means that the developers must first write the test cases before writing the code. BDD is an acronym for behavior-driven development. It's a behavior-based development approach.
TDD tests are developed in a variety of programming languages, including Java,.NET, Python, Ruby, and others. Given-When-Then steps are used to write BDD tests in a human-readable fashion. Non-technical people may read and comprehend these tests as well.
The scope is the key distinction between TDD and BDD. TDD is a development methodology. BDD, on the other hand, is a collaborative methodology.
When a test fails because the specified function does not exist, TDD recommends writing the simplest code possible to pass the test, then reworking to remove duplication, and so on. Creating an executable specification that fails because the feature isn't available, then writing the simplest code possible to make the spec pass in BDD. This process is repeated until a release candidate is ready to be delivered.
The test cases are written by the developers in TDD. Users or testers write automated specifications in BDD, which are then wired to the code under test by developers.
Because TDD tests are written in specific programming languages, they are difficult to interpret by non-programmers. Non-programmers can read BDD tests since they are written in a human-readable format.
In the Cucumber testing approach, the TestRunner class provides the link between the feature file and the step definition file. The TestRunner class is generally an empty class with no class definition.
 
Example of a TestRunner class in Cucumber :
Package com.sample.TestRunner  
importorg.junit.runner.RunWith;   
importcucumber.api.CucumberOptions;   
importcucumber.api.junit.Cucumber;  
@RunWith(Cucumber.class)   
@CucumberOptions(features="Features",glue={"StepDefinition"})   
public class Runner   
{  
}  
38 .
What is the use of glue property under the Cucumber Options tag?
The Glue property is used to facilitate the Cucumber framework to identify the location of step definition files.
39 .
What is the use of features property under the Cucumber Options tag?
In the Cucumber framework, the features property is used to identify the location of the feature files.
In the Cucumber framework, the Options tag is a part of the TestRunner file and comes in the form of an annotation called @CucumberOptions. It contains two parameters feature and glue.
 
Feature parameter : The feature parameter is used to specify the path of the feature file.
Glue parameter : The glue parameter is used to specify the path of the step definition file.

See the code implementation of TestRunner file with Option tag :
import org.junit.runner.RunWith;  
import cucumber.api.CucumberOptions;  
import cucumber.api.junit.Cucumber;   
@RunWith (Cucumber.class)  
@CucumberOptions (  
features = "src/test/java/features ",  
glue = {"stepDefinitions"}  
)   
public class TestRunner {  
} ​
 
We have to import org.junit.runner.RunWith for the @RunWith annotation and cucumber.api.CucumberOptions for the @CucumberOptions annotation.