Cucumber interview questions and answers
Here I am listing down cucumber interview questions with answers explaining the concepts as well to understand the real time scenarios.
Before Jumping on to Interview questions Lets see What is Cucumber?
Cucumber is a popular testing framework supporting Behavior Driven Development (BDD). It facilitates the creation and execution of tests in a simple language that describes the behavior of the application.
Key components include: Feature files, Gherkin Syntax, Step definitions, Test runner, Tags, Hooks, Data tables, Scenarios, and Scenario outlines.
1. How do you write tests in cucumber?
Tests are written in feature files as scenarios and scenario outlines.
2. How do you run tests in cucumber?
- Tests can be run from the Maven command line with different options
- Running all tests: mvn clean test
- Running tests with specific tags: mvn clean test -Dcucumber.filter.tags=”@ValidCredentials”
- Running a specific feature file: mvn clean test -Dcucumber.features=”src/test/resources/features/HomePage.feature”
- Generating cucumber report: mvn clean test -Dcucumber.plugin=”html:target/cucumber-reports/cucumberReport.html”
- Tests can be executed via the CucumberTestRunner class, specifying desired features and tags, along with options for generating reports. Simply right-click on the class file and select “Run”.
3. Can you explain CucumberTestRunner file?
The Cucumber test runner file configures and executes test scenarios, specifying the paths of feature and step definition files, what scenarios to run, the report format, and destination.
Here are the available options:
- features: Specify the path of feature files.
- glue: Define the path of the step definitions files.
- tags: Specify which tests to run. Scenarios in feature files are marked with tags. You can use ‘and’, ‘or’, and ‘not’ operators. For example: “@regression and not @smoke”, or “@smoke and @sanity”.
- plugin: Specify the reporting format and destination for reports
4. Can you explain the structure of a feature file?
A feature file consists of a feature description and test scenarios written in Gherkin language, including scenarios and scenario outlines with optional descriptions.
Feature: Title of the Feature
description of the feature (optional)
Background:
Given common precondition
And another common precondition
And another common precondition
…
Scenario: Title of the Scenario
# Description of the scenario (optional)
Given some precondition
When some action is taken
Then some expected result
And additional expected result (optional)
# This is a comment
Scenario Outline: Title of the Scenario Outline
# description of the scenario outline (optional)
Given a <variable>
When <action> is taken
Then <outcome>
Examples:
| variable | action | outcome |
| value1 | action1 | result1 |
| value2 | action2 | result2 |
…
5. What is Gherkin language?
Gherkin is plain text language used to describe the software behavior. In Gherkin each line starts with a keyword. A Gherikin document has a file extension .feature.
6. What are the different keywords used in Feature file
- Feature
- Background
- Scenario Outline
- Scenario
- Given
- When
- Then
- But
- And
- Or
- Example
7. When do you use Background step?
Background steps are used when a set of steps needs to run before every scenario of that feature file. It is intended to provide reusable context for the scenarios, such as setting up preconditions
8. What is the difference between Scenario and Scenario Outline
Scenario is used when it needs to be executed for one time.
Scenario outline is for executing the same scenario with multiple sets of test data given in Examples
9. What is the step definition file in cucumber
Code implementation of steps specified in feature file
10. What are the hooks in Cucumber?
Hooks are code blocks running at various stages during test execution.
Lets say we want to execute a piece of code before and after tests. Hooks are typically used for setup and teardown of the test project
Hooks can be written any where in the test code
There are different hooks available in cucumber
- Scenario hooks
- @Before, there hooks gets executed before 1st step of every scenario
- @After, these hooks gets executed after the last step of very scenario irrespective of the status of the scenario run.
- Step hooks
- @BeforeStep
- @AfterStep
- Conditional hooks – these hooks gets executed conditionally based on the tags. We can configure these hooks to run before or after a particular scenario.
- @Before(“@tag”)
- @After(“@tag”)
- Global hooks
- @BeforeAll – these hooks run before any scenario is run
- @AfterAll – these hooks run after all scenarios have been run
11. Is it possible to have multiple @Before hooks in the project
Yes, we can have multiple code blocks for the same hook. And also we can specify the order of execution of these same hooks be specifying order.
eg., @Before(order=1)/ @After(order=1)
12. What are the tags in cucumber
Tags are used to group, categorize and filter scenarios/ scenario outlines. Tags helps controlled execution of tests. Tags are defined starting with @ and tag name.
Tags can be place in feature file before the following elements
- Feature
- Scenario
- Scenario Outline
- Examples
In the below example tag1 and tag2 represents all the tests in the feature file
Multiple tags can be specified.
@tag1 @tag2
Feature: Title of the Feature
Description of the feature
…
Scenario: Title of the Scenario
Description of the scenario
…
@smoke
Given some precondition
When some action is taken
Then some expected outcome
13. Can I give a tag for Background in a feature?
You cannot apply a tag to the Background section in a Gherkin feature file. Tags are used to categorize and filter scenarios or scenario outlines, but they don’t apply to the Background itself. this helps controlled execution of tests. Purpose of Background steps is designed to contain steps that are common to all scenarios within a feature.
14. Is it possible to run tests with multiple tags?
Yes, you can use AND, OR and NOT key words in Dcucumber.filter.tags in cucumber runner file.
Eg., @smoke and @sanity and not @healthindicator
15. What is the maximum number of scenarios allowed in feature file?
As such there is no limit for the number of scenarios in feature file but it’s a good practice not to have more scenarios in one feature file. Let’s say 10 would be a good number. This makes feature file maintainable and readable
16. What is the number of steps allowed in a scenario?
There is no rule for max limit but its good practice to limit number of steps to make the scenario more readable. Lets say there are 4 steps involved in making one precondition then create one step consisting of all 4 step implementation.
For example: I need to write a precondition that user logged in and is on dashboard page
Instead of writing like below
Given user login with valid credentials
And navigate to homepage
And navigate to dashboard
Better way
Given user logged in and navigate to dashboard
17. What are the Assertions that are supported in cucumber?
As of May 2024, latest version of cucumber 7.17.X
Cucumber doesn’t have direct support for assert, it doesn’t have any libraries for assertion. But you can use testNG, Junit assertions for Java, Node.js for Javascript, RSpec for Ruby and so on.
18. How do you parameterize tests in cucumber, how to pass test data from feature file?
you can parameterize tests by passing test data from the feature file using placeholders in the steps. Cucumber allows you to achieve this using various methods, such as Scenario Outline with Examples, Data Tables, and Doc Strings.
In the below example, “<username>” and “<password>” are placeholders. data passed from Examples table.
Feature: Login Functionality
Scenario Outline: Successful login with valid credentials
Given the user is on the login page
When the user enters username “<username>” and password “<password>”
Then the user should be logged inExamples:
| username | password |
| testuser1 | password123 |
| testuser2 | pass456 |
| adminuser | adminpass |
Data table is the table of data passed to a particular step
When the user provides the following details:
| field | value |
| first name | John |
| last name | Doe |
| email | john@example.com|
| password | secret123 |
Doc Strings are multi-line text or large blocks of text into a step.
Feature: API Testing
Scenario: Create a new user via API
Given I send a POST request to “/api/users” with the following data:
“””
{
“name”: “John Doe”,
“email”: “john.doe@example.com”,
“password”: “password123”
}
“””
Then the user should be created successfully
19. What is Data Table in cucumber?
Cucumber provided DataTable class to capture data from feature file. Data table in feature file is like a a table with multiple columns and rows with header.
DataTable has many methods to convert data into a list or map or list of maps depending on the number of columns and rows.
20. How will you pass data from example table to steps?
Column name of the example table can be used for this. Give column name in angular brace <>
Eg:
Feature: Login Functionality
Scenario Outline: Successful login with valid credentials
Given the user is on the login page
When the user enters username “<username>” and password “<password>”
Then the user should be logged inExamples:
| username | password |
| testuser1 | password123 |
| testuser2 | pass456 |
| adminuser | adminpass |
21. What is profile in cucumber?
profiles are used to define custom configurations for running your tests. Profiles allow you to specify different sets of options, such as which features or tags to run, the format of reports, or environment variables. Profiles in Cucumber are typically defined in a cucumber.yml
file located in the root of your project.
Eg:., cucumber.yml
default: –tags @regression –format pretty –format html –out reports/default.html
smoke: –tags @smoke –format progress –format json –out reports/smoke.json
regression: –tags @regression –format pretty –format json –out reports/regression.json
staging: –tags @staging –format html –out reports/staging.html –plugin rerun –dry-run
You can run tests using
cucumber -p smoke
cucumber