- 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 written without knowledge of any code; it allows the involvement of non-programmers as well.
- It serves the purpose of end-to-end test framework unlike other tools.
- Due to simple test script architecture, Cucumber provides code re-usability.
For more details, click here
3. What is the programming language used by Cucumber?
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.
For more details, click here
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
For more details, click here
5. What are the files required to execute a Cucumber test scenario?
Features
Step Definition
Test Runner
For more details, click here
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
For more details, click here
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 use to describe the initial context of the scenario. When 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 use 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.
For more details, click here
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.
For more details, click here
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");
}
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 are 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.
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 the 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 {
}
For more details, click here
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 the example of hooks
public class CucumberHooksExampleDefinitions {
WebDriver driver;
@Before
public void setup() {
System.out.println("---------------------Before Executing----------------------");
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vibha\\Desktop\\SeleniumKT\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, 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----------------------");
}
}
For more details, click here
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.
For more details, click here
15. What is the use of features property under the Cucumber Options tag?
Feature property under Cucumber option tag let 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")
For more details, click here
16. What is the use of glue property under the Cucumber Options tag?
Glue property lets Cucumber know the location of the Step Definition file. As in the below example, the location of 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 specify 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 |
For more details, click here
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 specify the set of values for each parameter.
The example is shown in the previous example.
For more details, click here
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 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 {
}
For more details, click here
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"
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 link.
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
For more details, click here
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 run 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
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.