1) What are the files needed in a Cucumber framework?

Answer: Cucumber is a tool used to run automated acceptance tests created in a BDD format. One of its most outstanding features of the tool is the ability to carry out plain-text functional descriptions (written in the language called Gherkin) as automated tests.

Advantages of Cucumber are

  • Its focus on end-user experience
  • Cucumber tests are written in plain-text English so people with no or less technical skills can also write scenarios
  • It allows us to involve business stakeholders who can’t easily read a code
  • Allows quick and easy setup and execution
  • High reusability of code in the tests
  • It can be integrated with Selenium and other testing frameworks like JUnit & TestNG

2) What are the files needed in a Cucumber framework?

Answer: The files needed in a Cucumber framework are listed below:

Feature file – The feature file has an extension of .feature. It has single or multiple test scenarios described in plain text. The tests are written with keywords like Then, When, Background, Scenario Outline, Feature, And, But, and so on. Thus it is a file that maintains the features and their descriptions.

Step Definition file – This file has an extension of .java. It basically provides the mapping of the test scenario steps described in the feature file to the automation code. Thus when Cucumber runs a step described in the feature file, it searches the step definition file and executes the relevant functions that are mapped to that step.

TestRunner file – This file has an extension of .java. It links the step definition file and the feature file. It gives the user the option to execute one or more than one feature file. It has the path of the step definition file and the feature file.

3) What are the different keywords used in feature file?

Answer: The different keywords used in the feature file are

  • Feature
  • Background
  • Scenario
  • Scenario Outline
  • Given
  • When
  • Then
  • And
  • But

4) Explain Scenario Outline in the Cucumber framework.

Answer: A Scenario Outline is used to run a particular scenario with more than one data set in multiple combinations. A feature file can have fewer lines if we take the help of a Scenario Outline. The data to be passed at runtime need not be hardcoded in the feature file if we use Scenario Outline.

With Scenario Outline the runtime values are passed as parameters enclosed in <> in the feature file. The data set is expressed in a tabular format separated with pipe delimiter starting with the Examples keyword.

Feature file with Scenario Outline implementation.

1
2
3
4
5
6
7
8
9
Feature: Admin Login Verification
Scenario Outline: Multiple Users Login Test
Given User navigates to Admin Login Page
When Page Title is Welcome to Admin
Then User inputs "<Name>" and "<EmployeeId>"
Examples:
         | Name         | EmployeeId |
         | Adam | 3678 |
         | Sam          | 2589       |

5) Explain keyword Background in the Cucumber framework.

Answer: The keyword Background is used to describe a test step or a group of test steps that are common to every test inside the feature file. A test having a Background executes before all the test scenarios in the feature file.

Thus a keyword Background is used to run a scenario as a precondition for other scenarios in the feature file.

Feature file with Background implementation

In the above example, we are testing the Admin Login Verification feature. We have two scenarios – New user addition by admin user and User deletion by an admin user. The precondition to both these scenarios is defined with Background scenario – User Login Test and it shall execute two times before each of the scenarios.

6) Explain keyword Examples in the Cucumber framework.

Answer: We can achieve a data-driven approach in Cucumber with the help of the Examples keyword. The Scenario Outline in a feature file should be accompanied by the Examples part which consists of the multiple data set to be passed at the 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.

Feature file with Examples implementation

7) Explain BDD.

Answer: BDD means Behavior Driven Development. In Behavior Driven Development, the tests are designed in plain English and are created from a more end-user perspective and describe the application characteristics.

Behavior Driven Development framework increases collaboration and coordination among members in an agile team of developers, testers, business analysts, product owners, customers, and other stakeholders. It does not require any technical knowledge and is written in plain text and requires no technical clarity.

Behavior Driven Development framework like Cucumber helps to bridge any understanding gap between business stakeholders and the developers. Some of the benefits of using Behavior Driven Development are listed below:

  • Since all the team members and stakeholders contribute to the project development, it ensures maximum testing coverage.
  • Since the tests are designed in plain text, there is proper understanding and knowledge over the business requirements.
  • Test tools like Cucumber and SpecFlow in Behavior Driven Development help to automate scenarios.
  • Behavior Driven Development helps to achieve reusability of code to a large extent.
  • Data parameterization can be easily implemented in Behavior Driven Development.
  • Behavior Driven Development allows easy integration with continuous integration tools like Jenkins.

8) Describe a feature file with an example.

Answer: A feature file is the starting of tests in Cucumber. All the test scenarios are elaborated here in plain simple text. It may contain single or multiple test scenarios at a time. It is recommended to create a separate feature file for each standalone feature.

If we are using an Eclipse IDE, a feature file can be created by selecting our project, then right-click and select the New option. Next click on File. Then we have to provide the name of the file with .feature as an extension.

A feature file normally contains some keywords described in Gherkin language. Let us discuss some of them:

  • Feature – Defines a particular feature that we will test.
  • Description – This is not mandatory and it describes what the feature deals in.
  • Scenario – Describes a particular scenario.
  • Given – Describes the prerequisites of a test scenario.
  • Then – Describes the expected outcome of the test.
  • And – Describes some of the further conditions to be tested.

Feature file implementation.

9) Describe a step definition file with an example.

Answer: First of all we have created a feature file with the test scenarios. But the Cucumber is still unaware of which code to be run for a particular scenario. This is determined by the step definition file which gives one to one mapping of each scenario in the feature file to the function to be executed.

Thus a step definition file is a file with .java extension and contains all the methods in a class having annotations that map them to the scenarios in the feature file. All the keywords used in the Feature file is added to the step definition file in the form of annotations and then importing cucumber.api.java.en package.

Now let us create a step definition file for the Feature Payment Verification in the feature file discussed above.

Step Definition file implementation.

10) 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.

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.

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.

11) How is the Options tag used in the Cucumber framework?

Answer: The Cucumber Options tag is a part of the TestRunner file and comes in the form of an annotation called @CucumberOptions. It contains parameters like feature and glue. The feature parameter has the path of the feature file and the glue parameter has the path of the step definition file.

Code Implementation of TestRunner file with Option tag.

We have to import org.junit.runner.RunWith for the @RunWith annotation and cucumber.api.CucumberOptions for the @CucumberOptions annotation.

12) What are some of the keywords used to build a scenario in Cucumber?

Answer: various keywords that are used in Cucumber for writing a scenario

  • Given
  • When
  • Then
  • And

13) 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.

14) Name any two build management tools that can be integrated with Cucumber?

  • Gradle
  • Maven

15) Describe a TestRunner file with an example.

Answer: A TestRunner file contains a JUnit class. It contains the @RunWith and @CucumberOptions annotations. We need to import org.junit.runner.RunWith and cucumber.api.CucumberOptions for the annotations @RunWith and @CucumberOptions respectively.

The @CucumberOptions has parameters like features, glue, tags, plugin, and so on. The features parameter contains the location of the feature file. The tags parameter is used to include or exclude a tagged scenario in a feature file in the execution. If we provide the (~) before the tag name, we are simply excluding that scenario from the run.

The parameter plugin is used for Cucumber reports in various formats like HTML, JSON, and so on. The pretty, html: test-output values in the plugin is used to generate an HTML report inside the test-output folder within the framework. The parameter strict is used to verify whether any step is not included in the step definition file and the dryRun parameter is used to validate if there is a proper mapping between the step definition file and the feature file.

Inside the JUnit class, we can add annotations like @BeforeClass and @AfterClass, to run a precondition or a postcondition for a test. We have to import org.junit.BeforeClass and org.junit.AfterClass for the @BeforeClass and @AfterClass annotations respectively.

Code Implementation of TestRunner file.

16) Can Cucumber be integrated with Selenium?

Answer: Yes we can integrate Cucumber with Selenium. First of all we need to download the Selenium-server-standalone jar from the below link and add this to our project:

https://www.selenium.dev/downloads/

Next, we should navigate to the Maven repository with the below link:

https://mvnrepository.com/search?q=Cucumber

Then download the following Cucumber Jars listed below and add them to the Project:

  • JUnit
  • Gherkin
  • Cucumber-JUnit
  • Cucumber-Java
  • Hemcrest-core
  • Cucumber – reporting
  • Cucumber – html
  • Cucumber – core
  • Cobertura – code coverage
  • Cucumber – jvm –deps

Next we have to install the Cucumber Eclipse plugin for which we have to navigate to Install New Software from the Help menu in Eclipse and then add the link: http://cucumber.github.io/cucumber-eclipse/update-site/ for installation.

Then we have to create a feature file with the test scenarios in a step by step approach in Gherkin language, a step definition file for actual Selenium code implementation of the scenarios, and the test runner file to run the Cucumber tests.

17) Which language is used in Cucumber?

Answer: The language that Cucumber understands is called Gherkin. It is a simple English representation of the application behavior. It is used to define test cases. It is designed to be non-technical and human-readable, and collectively describes use cases relating to a software system. It is a Business Readable, Domain Specific Language (DSL).

18) Define programming language that is used by Cucumber?

Answer: Cucumber tool provides support for numerous programming languages such as Java, .Net, Ruby, etc. It can also be included with several tools such as Capybara, Selenium, etc.

19) What are hooks in the Cucumber framework?

Answer: A block of code tagged with hooks in Cucumber can run prior or post a scenario with the help of @Before and @After annotations. Thus hooks help to control the flow of the program and optimize lines of code.

There may be scenarios that require certain preconditions to be executed like launching the application, establishing database connection, configuring the test data, and so on. Also, there are certain postconditions to be executed like terminating database connection, closing the browser, refreshing test data, application log out, and so on.

All these conditions are handled in Cucumber with the help of the hooks. The @Before hook executes before the actual scenario and the @After hook executes after the actual scenario even if the test corresponding to the actual scenario fails.

We have to import cucumber.api.java.en.Before for the @Before annotation and import cucumber.api.java.en.After for the @After annotation.

Code Implementation with hooks.

Feature file.

  • Feature: Invoice Generation
  • Scenario: Verify the invoice generates in pdf format
  • Given User navigates to Invoice Page
  • Then User should be able to open the invoice in pdf format
  • Scenario: Verify the invoice fields
  • Given User opens the invoice
  • Then User should be able to see the payment amount in invoice

In the above feature file, we have included two scenarios.

Step definition file.

In the above example, since there are two scenarios, the test method precondition with @Before annotation and the test method postcondition with @After annotation shall run twice.

20) How to comment on the Cucumber framework?

Answer: We can add comments in the Cucumber code. The comments are incorporated in code mostly for documentation and not for including program logic. It makes the code easier to apprehend and to debug the errors. Also, comments can be put at any point in the code.

<

Inside the feature file, the comments are added after the (#) symbol and the step definition contains comments after the (//) symbol.

Feature file with comments.

  • Feature: Customer verification
  • # Scenario with customer
  • Scenario: Verify customer details
  • Given User navigates to Customer Information Page

Step definition file with comments

21) How to use regular expressions in the Cucumber framework?

Answer: We can use regular expressions in the Cucumber framework inside the step definition file. The regular expression can be used to combine two or more similar steps in the feature file within a test method in the step definition file. Also, the regular expression is used for data parameterization in Cucumber as the Feature New User Registration in the feature file described previously.

Feature file implementation.

  • Feature: Student Study Schedule
  • Description: Verify student study schedule
  • Scenario: Verify hours spent on science subjects
  • Given Student studies Chemistry on Tuesday
  • Given Student studies Mathematics on Tuesday
  • Given Student has thirty minutes recess

Step definition file with regular expression.

In the above example, we have computed the subject’s Chemistry and Mathematics inside one method student_Tuesday_schedule without requiring to hardcode test data inside the step definition file. With the help of Student studies ([^\”]*) statement we have combined two Given (Student studies Chemistry on Tuesday and Student studies Mathematics on Tuesday) statements inside one method.

22) How to skip tests in the Cucumber framework?

Answer: We can skip tests in the Cucumber framework with the help of the tags concept which comes under CucumberOptions in the TestRunner file. We can tag a scenario in the feature file with help @<<tagname>> keyword.

A scenario inside the feature file can have one or multiple tags. With the help of the tagging, we can segregate test scenarios. We can mention these tagged scenarios inside the tags parameter under @CucumberOptions in the TestRunner file. To skip tests in Cucumber we have to pass the ~<<tagname>> expression within the tags parameter.

Feature file implementation with tags.

  • Feature: Insurance Details
  • @Information
  • Scenario: To verify Insurance information
  • Given User is on the Insurance Information Page
  • @Premium
  • Scenario: To verify Insurance premium information
  • Given User is on the Insurance Premium Page

Code Implementation of TestRunner file to skip test.

In the above example, since there are two scenarios with tags @Information and @Premium, the test scenario with @Information shall run and the test scenario @Premium shall skip since the parameter tags = {“~@Premium”} has been added in the TestRunner file under @CucumberOptions.

23) How to set priority to tests in the Cucumber framework?

Answer: We can set priority to tests in Cucumber to determine the order of execution. We use Cucumber hooks to control the flow of execution. But this can be modified with the help of the order.

Let us take a step definition file having two test methods with @Before annotations. In order to control the sequence of their execution, we can use @Before (order = int) statement. This ensures that the test methods are executed in an incremental manner. This means the test method having order = 1 shall execute before the method having order = 2.

Thus we can set priority to a test method such that the method having lower order shall be executed first followed by the test method having higher order.

Step definition file implementation.

In the above example, the test method precondition2 [having order = 1] shall be executed first, followed by the test method precondition1 [having order = 2]. Finally the test method navigate_reorder will be executed.

24) How to run a precondition test for a scenario in the Cucumber framework?

Answer: We can run a precondition test for a scenario in Cucumber. A scenario inside the feature file can have one or multiple tags. With the help of the tagging we can segregate test scenarios.

A block of code tagged with hooks in Cucumber can run prior to a scenario with the help of @Before annotation. Thus tagged hooks help to control the flow of the program. There may be a scenario that requires a certain precondition to be executed.

This kind of situation is handled in Cucumber with the help of the tagged hooks. The @Before (“Scenario Tagname”) expression is used to execute a certain precondition for a scenario. We have to import cucumber.api.java.en.Before for the @Before annotation.

Feature file implementation with hooked tags on scenarios.

  • Feature: Buy Insurance
  • @Dental
  • Scenario: To verify buying of Dental Insurance
  • Given User is on the Dental Insurance Page
  • @Car
  • Scenario: To verify buying of Car Insurance
  • Given User is on the Car Insurance Page
  • @Accidental
  • Scenario: To verify buying of Accidental Insurance
  • Given User is on the Accidental Insurance Page

In the above feature file, we have included three scenarios.

Step definition file implementation.

In the above example, the test method preconditionCar [having @Before (“@Car”)] shall be executed before the navigate_car_Insurance method since it is tagged with @Car in the feature file. We can say that the preconditionCar method is just like a precondition to the navigate_car_Insurance method.

The test method preconditionDental [having @Before (“@Dental”)] shall be executed before the navigate_dental_Insurance method since it is tagged with @Dental in the feature file. We can say that the preconditionDental method is just like a precondition to the navigate_dental_Insurance method.

The test method preconditionAccidental [having @Before (“@Accidental”)] shall be executed before the navigate_accidental_Insurance method since it is tagged with @Accidental in the feature file. We can say that the preconditionAccidental method is just like a precondition to the navigate_accidental_Insurance method.

25) How to run a selected test from a group of tests in Cucumber framework?

Answer: We can run a selected test from a group of tests in the Cucumber framework with the help of tags concept. This comes under the @CucumberOptions in the TestRunner file. We can tag a scenario in the feature file with help @<<tagname>> keyword.

A scenario inside the feature file can have one or multiple tags. With the help of the tagging, we can segregate test scenarios. We can mention these tagged scenarios inside the tags parameter under @CucumberOptions in the TestRunner file. To run a selected test in Cucumber we have to pass the <<tagname>> value and to exclude from running we have to pass the <<~tagname>> value within the tags parameter.

Let us consider the Feature: Buy Insurance in the feature file described previously.

Code Implementation of TestRunner file to run selected test.

In the above example, there are three scenarios, but only the test scenarios with @Dental, @Car tags shall run and the test scenario with @Accidental tag shall not be included in the execution.

26) Why is the property glue used in the Cucumber Options tag?

Answer: The property glue is used with the Cucumber Options tag which is a part of the TestRunner file. It takes the location or path of the step definition file.

Code Implementation of TestRunner file with glue.

27) What is a test harness?

Answer: A test harness is a framework that contains test cases, test data, certain parameters and configurations, collects test results and records them.

28) 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.