MGNK Junction
Cucumber IQs
What are the advantages of cucumber testing framework?
-
Cucumber is a powerful and versatile testing framework that can be used to improve the quality of a software.
-
Cucumber tests are written in a human-readable format called Gherkin, which makes them easy to understand for both technical and non-technical stakeholders. It supports behavior-driven development (BDD) approach.
-
Since Cucumber tests are written in a natural language/declarative format i.e., the tests do not specify how to implement the behavior, tell only what the behavior should be.
-
Also, the tests are written in a shared format, which makes it easy to collaborate with other developers and stakeholders.
-
They can be easily automated using a variety of tools. It helps to improve the development process by making it easier to identify and fix defects in the early stage of development.
What is meant by a feature file?
Cucumber tests are typically organized into features, which are a collection of scenarios.
-
A feature file provides a high-level description of an Application Under Test.
-
Each feature file starts with the keyword 'Feature'.
-
It is a description of what the software is supposed to do
List the most important keywords used in Cucumber feature file.
-
Feature
-
Scenario
-
Scenario Outline
-
Examples
-
Given
-
When
-
Then
-
And
-
Background
What is TestRunner class in Cucumber?
-
It is an empty class file, used to run tests in Cucumber framework.
-
No code is written under the TestRunner class.
-
However, the tags @RunWith and @CucumberOptions should be included.
-
Under the @CucumberOptions tag, the path of the feature file and step definition file that we want to execute should be provided.
What is Step definition file in Cucumber?
-
A step definition file contains the corresponding code for the steps defined in a Cucumber feature file.
-
Each step definition is nothing but a method.
-
The method body contains the code that links it to one or more Gherkin steps and executes the step.
E.g., Feature file
Scenario: Login into Homepage
Given I am on the home page
Step Definition file
import io.cucumber.java.en.Given;
public class StepDefinitions {
@Given("^I am on the home page$")
public void loginwithCredentials ( ){
WebDriver driver=new ChromeDriver();
driver.get(“url”);
driver.findElement(By.id(“uname”)).sendKeys(“katty”);
driver.findElement(By.id(“pword”)).sendKeys(“katty123”);
driver.findElement(By.id(“loginbtn”)).click();
}
}
When Cucumber executes a feature file, it will first look for a matching step definition for each step in the feature file. If a matching step definition is found, Cucumber will execute the code in the step definition. They also make it easy to reuse code across multiple tests.
How can Cucumber tests be run parallelly?
Cucumber tests can be run parallelly by adding some plugins to the project's dependencies and then configured it in test runner class file. To run the tests in parallel we can have,
1.cucumber-parallel plugin (using multiple threads)
2.cucumber-junit plugin (using JUnit)
3.cucumber-testng plugin (using TestNG)
Also, we can use a load balancer to distribute the tests across multiple machines, this can help to improve the performance of the test suite.
Explain grouping in Cucumber.
Grouping the related tests can help us to organize and manage the tests in an easier manner. It makes the testing more efficient. Cucumber testing framework's grouping facility let us to categorize Cucumber features and scenarios into groups. There are a few different ways to group Cucumber features and scenarios.
1.Tags are a way to identify groups of tests that share a common characteristic.
Use the `@` symbol to tag the features and scenarios. e.g., @smoke Test
@SmokeTest
Scenario: Login to the application
Given the user is on the login page
When the user enters their username and password
Then the user should be logged in
Using the tags option in the @CucumberOptions class, we can specify which tags we want to run
@RunWith(Cucumber.class)
@CucumberOptions(
tags = {"@SmokeTest"})
public class SmokeTestRunner { }
This will run all of the scenarios that are tagged with @SmokeTest.
2. Use the Background keyword.
Background keyword allows us to define steps that are common to all of the scenarios in a feature.
Background: Given the user is on the home page
Scenario: Login to the application
When the user enters their username and password
Then the user should be logged in
Scenario: Logout from the application
When the user clicks the logout button
Then the user should be logged out
In this example, the Given the user is on the home page step is defined in the Background section. This step will be run before each of the scenarios in the feature. It is useful for setting up the same environment that are used in multiple scenarios.
3. Use the Feature keyword. The Feature keyword allows us to define a group of scenarios that are related to a specific feature of our application.
Feature: Login
Scenario: Login to the application
When the user enters their username and password
Then the user should be logged in
Scenario: Logout from the application
When the user clicks the logout button
Then the user should be logged out
In this example, the Login feature contains two scenarios: Login to the application and Logout from the application.
Describe the use of the Options tag in the Cucumber Framework.
In the Cucumber framework, the Options tag is a part of the TestRunner file and comes in the form of an annotation called @CucumberOptions. By using the `@Options` tag, one can customize the behavior of Cucumber to meet the specific needs. It is used to specify options that control how Cucumber should run the tests.
Cucumber can report its results in a variety of formats, such as text, HTML, and JSON, so the format of the output can be specified by using the `format` option. By default, Cucumber runs all the tests in a single thread. In order to specifying the number of threads, 'threads' option is used.
To run specific tests that are associated with each other, ‘tags’ option can be used.
@CucumberOptions (
format = {"html"},
threads = 2,
tags = {"@smoke"})
In the above example, the `@Options` tag is used to specify that the output should be in HTML format, two threads should be used to run the tests, only run the tests that are tagged with `@smoke`
@Runwith(Cucumber.class)
@CucumberOptions (
features={"src/test/java/login.feature"}, //To specify the path of the feature file.
glue={"stepDefinitions"}, //To specify the path of the step definition file.
tags={"@smokeTest"},
plugin = {"pretty", "summary"},
format = {"pretty", "html:target/cucumber"},
dryRun=true,
monochrome=true
)
public class TestRunner{
}
What are scenarios in Cucumber?
Scenarios are a collection of steps that describe a specific interaction with the application.
What is the maximum number of scenarios included in a feature file?
-
A single feature file can contain any number of scenarios but focuses only on one feature such as registration, login etc at a time.
-
By using the keyword "Scenario" or "Scenario Outline", One Scenario can be separated from another. it is advisable to keep the number to a minimum.
For e.g., the maximum number of scenarios that can be included in a feature file is 10.
What is the purpose of Scenario outline keyword?
-
The Scenario Outline keyword can be used to run the same Scenario multiple times, with different combinations of values. i.e., to achieve data parametrization.
-
Scenario Outline must be followed by the keyword ‘Examples’ which specify the set of values for each parameter.
-
The multiple data sets are represented in the form of a table separated by (||) symbol under 'Examples' keyword. Each row represents a group of data.
What is the use of glue property under Cucumber Options tag?
It helps Cucumber to locate the step definition file.
Is Cucumber A automation tool?
A cucumber is a tool based on Behavior Driven Development (BDD) framework which is used to write acceptance tests for the web application.
Selenium vs Cucumber
.