Last Updated On
1. What is BDD? Why do we use BDD?
BDD (behavior-driven development ) is an Agile software development process that encourages collaboration among developers, QA, and business participants in a software project.
Why do we use BDD
BDD increases and improves collaboration between various teams involved in the project to easily engage with the product development cycle. We use a simple English language called Gherkins to write test scenarios.
2. What is Cucumber? What are the advantages of Cucumber?
Cucumber is an open-source tool that supports Behavior Driven Development (BDD). It is written in Rugby Language. Cucumber reads executable specification written in plain English language (Gherkins) and validates that the software act as per the executable specifications.
Advantages of Cucumber
- Cucumber supports different languages like Java.net and Ruby.
- It acts as a bridge between the business and technical language. We can accomplish this by creating a test case in plain English text.
- It allows the test script to be written without knowledge of any code; it allows the involvement of non-programmers as well.
- It serves the purpose of an end-to-end test framework, unlike other tools.
- Due to simple test script architecture, Cucumber provides code re-usability.
Please refer to this tutorial for more details – Introduction of Cucumber Testing Tool (BDD Tool)
3. What is the programming language used by Cucumber?
The 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.
Please refer to this tutorial for more details – Introduction of Cucumber Testing Tool (BDD Tool)
4. What is Gherkin?
Gherkin is a set of grammar rules that makes plain text structured enough for Cucumber to understand.
Gherkin serves multiple purposes:-
- Unambiguous executable specification
- Automated testing using Cucumber
- Document how the system actually behaves
Please refer to this tutorial for more details – Cucumber – What is Gherkin.
5. What are the files required to execute a Cucumber test scenario?
Features
Step Definition
Test Runner
6. What is a Feature File?
A Feature File is a file in which we store features, descriptions of the features and scenarios to be tested. 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. Below is an example of the Feature file.
Feature: Login to HRM Application
@ValidCredentials
Scenario: Login with valid credentials
Given User is on Home page
When User enters username as "Admin"
And User enters password as "admin123"
Then User should be able to login sucessfully
Please refer to this tutorial for more details – Cucumber – What is Feature File in Cucumber.
7. What are the various keywords that are used in Cucumber for writing a scenario?
Below mentioned are the keywords used for writing a scenario:
- Given – It is used to describe the initial context of the scenario. When the Given step is executed, it will configure the system to a well-defined state, such as creating & configuring objects or adding data to a test database.
- When – It is used to describe an event or action. This can be a person interacting with the system or it can be an event triggered by another system.
- Then – It is used to describe the expected outcome of the scenario
- And/But – If we have several Given’s, When’s, or Then’s, then we can use And /But.
Please refer to this tutorial for more details – Cucumber – What is Feature File in Cucumber.
8. What is the purpose of the Step Definition file in Cucumber?
It is a Java method with an expression, which is used to link it to Gherkin steps. When Cucumber executes a Gherkin step, it will look for a matching step definition to execute. Step definitions can be written in many programming languages.
Feature: Book flight ticket for one-way trip
Scenario: flight ticket for one-way trip from Dublin
Given I live in Dublin
@Given ("I live in Dublin")
public voidVacation()
{
System.out.println("I live in Dublin");
}
Please refer to this tutorial for more details – Step Definition in Cucumber
9. What is the use of the Background keyword in Cucumber?
Background keyword is used to group multiple given statements into a single group. This is generally used when the same set of given statements is repeated in each scenario in the feature file. For example, you need to pass the bearer token in every test. So, this can be done by defining the Given statement as background.
Please refer to this tutorial for more details – Background in Cucumber
10. What is the purpose of the Cucumber Options tag?
The 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 the Cucumber Options tag:
@CucumberOptions(features="src/test/resources/features/MyHoliday.feature",tags = { "@BookOneWayFlight"})
11. What is TestRunner class in Cucumber?
It is the starting point for JUnit to start executing the tests. TestRunner class is used to provide the link between the feature file and the step definition file.
Below is an example of TestRunner class.
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature", tags = { "@BookOneWayFlight"})
public classTestRunner {
}
Please refer to this tutorial for more details – Cucumber – JUnit Test Runner Class.
12. What is a profile in cucumber?
You can create Cucumber profiles to run a set of features and step definitions. Use the following command to execute a cucumber profile.
Syntax:
cucumber features -p <profile_name>
Example:
cucumber features -p Integration
13. What are hooks in Cucumber?
Hooks are blocks of code that can run at various points in the Cucumber execution cycle
1- Before: executes before the feature file execution.
2- After: executes after the feature file execution.
3- BeforeStep: executes before each step execution.
4- AfterStep: executes after each step execution.
Below is an example of hooks.
public class CucumberHooksExampleDefinitions {
WebDriver driver;
@Before
public void setup() {
System.out.println("---------------------Before Executing----------------------");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@BeforeStep
public void beforeStepTest() {
System.out.println("--------------BeforeStep Executing---------------");
}
@Given("User is on Home page")
public void userOnHomePage() {
System.out.println("Open Website");
driver.get("https://opensource-demo.orangehrmlive.com/");
}
@When("User enters username as {string}")
public void entersUsername(String userName) throws InterruptedException {
System.out.println("Enter userName");
driver.findElement(By.name("txtUsername")).sendKeys(userName);
}
@When("User enters password as {string}")
public void entersPassword(String passWord) throws InterruptedException {
System.out.println("Enter passWord");
driver.findElement(By.name("txtPassword")).sendKeys(passWord);
driver.findElement(By.id("btnLogin")).submit();
}
@Then("User should be able to login sucessfully")
public void sucessfullLogin() throws InterruptedException {
String newPageText = driver.findElement(By.id("welcome")).getText();
System.out.println("newPageText:" + newPageText);
assertThat(newPageText, containsString("Welcome"));
}
@AfterStep
public void afterStepTest() {
System.out.println("--------------------AfterStep Executing---------------------");
}
@After
public void close() {
driver.close();
System.out.println("--------------------After Executing----------------------");
}
}
Please refer to this tutorial for more details – Hooks in Cucumber
14. What are cucumber tags?
Tags are a great way to organize Features and Scenarios. Cucumber tags help in filtering the scenarios. We can tag the scenarios and then run them based on tags.
Below is an example of the tag.
Feature: Sample use of Tags in Cucumber
@ValidCredentials
Scenario: Login with valid credentials
Given User is on Home page
When User enters username as "Admin"
And User enters password as "admin123"
Then User should be able to login sucessfully
Here, @ValidCredentials is the Tag.
Please refer to this tutorial for more details – Tags in Cucumber.
15. What is the use of features property under the Cucumber Options tag?
Feature property under Cucumber option tag lets Cucumber know the location of Feature Files. As in the below example, the location of the feature file is under src/test/resources/features.
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature")
Please refer to this tutorial for more details – Cucumber – What is Feature File in Cucumber.
16. What is the use of glue property under the Cucumber Options tag?
The glue property lets Cucumber know the location of the Step Definition file. As in the below example, the location of the step definition is “com.cucumber.demo.definitions”.
@CucumberOptions(features = { "src/test/resources/features/MyHoliday.feature" }, glue = {"com.cucumber.demo.definitions" }
17. What is the Scenario Outline in the feature file?
Scenario outline is a way of parameterization of scenarios in Cucumber. It is used to repeat the same tests by passing different values or arguments to Step Definition. Scenario Outline must be followed by the keyword ‘Examples’, which specifies the set of values for each parameter.
Scenario Outline: Book Flight for one-way trip
Given I live in Dublin with adults and kids
And I want to book one way flight ticket from Dublin to London on
When I search online
Then TripAdvisor should provide me options of flights on
And Cost of my flight should not be more than Euro per person
And Tickets should be refundable
Examples:
|noOfAdults |noOfKids |travelDate |flightFare |
|2 | 2 |22-Jan-2020 |100 |
|1 | 0 |12-Mar-2020 |50 |
Please refer to this tutorial for more details – Data Driven Testing using Scenario Outline in Cucumber
18. What is the purpose of the Examples keyword in Cucumber?
Examples – All scenario outlines have to be followed by the Examples section. This contains the data that has to be passed on to the scenario in Feature File. Scenario Outline must be followed by the keyword ‘Examples’, which specifies the set of values for each parameter.
The example is shown in the previous example.
Please refer to this tutorial for more details – Data Driven Testing using Scenario Outline in Cucumber
19. 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.
20. What is the purpose of a cucumber dry-run?
We use to compile the cucumber feature files and step definitions. If there occur any compilation errors, then it shows them on the console.
@RunWith(Cucumber.class)
@CucumberOptions(dryRun=true)
public class RunCucumberTest {
}
21. List out some of the main differences between Jbehave and Cucumber?
1- Jbehave is Java-based and Cucumber is Ruby-based.
2- Jbehave is story-driven whereas the Cucumber is feature-driven.
22. What are the steps to generate a report in Cucumber?
We run the following command to produce HTML reports.
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:target/cucumber"})
public class RunCucumberTest {
}
Cucumber Version 6.7.0 and above we can create a Cucumber Report by adding a cucumber.properties file in src/test/resources and mention the below instruction in it.
cucumber.publish.enabled=true
Please refer to this tutorial for more details – Cucumber Report Service
23. Does Cucumber comes with an assertion library?
No, Cucumber does not come with an assertion library. It uses assertion from unit testing tools like JUnit, TestNG, JavaScript, RSpec.
24. How to run a Cucumber test using tags in Command-Line?
mvn test -Dcucumber.filter.tags="@BookOneWayFlight"
mvn test -Dcucumber.options="@BookOneWayFlight"
Please refer to this tutorial for more details – Run Cucumber Test from Command Line.
25. Can we use Selenium with Cucumber?
Yes, we can use Selenium with Cucumber to test a Web Application. Cucumber makes the tests readable for business also. To know more about the Integration of Selenium with Cucumber, refer to this tutorial – Integration of Cucumber with Selenium and JUnit
26. What is DataTable in Cucumber?
Data Tables in Cucumber are used to add multiple parameters in Step Definition in a tabular form rather than putting all the parameters in the Gherkin statement. This is much easier to read and multiple rows of data can be passed in the same step. Data tables from Gherkin can be accessed by using the DataTable object as the last parameter in a Step Definition. Below is an example of DataTable.
Feature: Login to HRM Application
@ValidCredentials
Scenario: Login with valid credentials
Given User is on HRMLogin page
When User enters valid credentials
|Admin|admin123|
Then User should be able to login sucessfully and new page open
Please refer to this tutorial for more details – DataTables in Cucumber
27. What is the difference between Scenario Outline and DataTables in Cucumber?
Scenario Outline
- The Scenario Outline keyword is used to run the same Scenario multiple times, with different combinations of data.
- Cucumber automatically runs the complete test the number of times equal to the number of data in the Test Set.
- Example tables always have a header row, because the compiler needs to match the header columns to the placeholders in the Scenario Outline’s steps.
Scenario Outline: Book Flight for one-way trip
Given I live in Dublin with adults and kids
And I want to book one way flight ticket from Dublin to London on
When I search online
Then TripAdvisor should provide me options of flights on
And Cost of my flight should not be more than Euro per person
And Tickets should be refundable
Examples:
|noOfAdults |noOfKids |travelDate |flightFare |
|2 | 2 |22-Jan-2020 |100 |
|1 | 0 |12-Mar-2020 |50 |
DataTable
- No keyword is used to define the test data
- This works only for the single step, below which it is defined
- Data tables are passed wholesale to the step definitions, and it’s up to the user to interpret them. They don’t necessarily have a header row.
Feature: Login to HRM Application
@ValidCredentials
Scenario: Login with valid credentials
Given User is on HRMLogin page
When User enters valid credentials
|Admin|admin123|
Then User should be able to login sucessfully and new page open
28. Can we run Cucumber tests parallelly?
Yes, we can run Cucumber tests parallelly. Cucumber-JVM allows parallel execution across multiple threads since version 4.0.0.
Cucumber Scenarios can be executed in parallel using the JUnit Platform
Cucumber can be executed in parallel using JUnit and Maven test execution plugins.
Cucumber can be executed in parallel using TestNG and Maven test execution plugins.
Please refer to this tutorial for more details – Parallel Testing in Cucumber with TestNG.
29. Can we use Cucumber with Rest Assured?
Cucumber is not an API automation tool, but it works well with other API automation tools, like Rest Assured.
Please refer to this tutorial for more details – Rest API Test in Cucumber BDD.
This tutorial gives an idea of various questions that can be asked in an interview. Hope this helps you in the preparation of the Interview.