How to Use Cucumber Automation Testing Tool | Devstringx
Cucumber Automation Testing Tool
Cucumber is a tool that supports BDD (Behaviour Data-Driven). By using cucumber, we can write test scripts in a way that anybody can understand even if the person is non-technical, and can understand what’s going on in the test script. In BDD, business analysts, and product owners first write scenarios that describe the behavior of the product from the customer’s perspective,
In Cucumber, We use Given-When-Then steps. Let’s consider a scenario
Feature: Machine Calculator As a user I want to use it to Multiply the numbers So that I don’t need to multiply my own
- Given − It describes the pre-requisite for the tests to be executed.
- For Ex — Given: I have a calculator
- When − It defines the trigger points for any test scenario execution
- For Ex — When: I multiply 2 and 3
- Then- Then holds the expected result for the test to execute
- For Ex — Then: the result should be 6
Configuration
- 1st Step- Install Eclipse, & Java.
- 2nd Step- Install Maven.
- 3rd Step- Create Maven Project on Eclipse & on Pom.xml, Add the below dependencies.
Cucumber-Java
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.0.2</version> <scope>test</scope> </dependency>
Cucumber-Junit
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.0.2</version> <scope>test</scope> </dependency>
Cucumber Gherkin
Gherkin is a language that developers and testers use to define tests in Cucumber
It automatically downloaded along with the other Cucumber jars.
Cucumber Features
Cucumber tests are written, in a file known as a feature file. There should be a separate feature file, for each feature under test. The extension of the feature file should be “.feature”.
For Example
- Feature- User Signup
- Feature Filename- user sign up. feature
For Example
- Feature − The user should be able to login into twitter when the username and the password are correct.
The user should be shown an error message when the username and password are incorrect.
The user should navigate to the home page if the username and password are correct.
- Outline − Login functionality for a Twitter site.
The given user navigates to Twitter. When I enter my Username as “[email protected]” and the Password as “invalidPass”. Then, login should be unsuccessful.
Cucumber -Tags: Tag use to connect a test like smoke, sanity, regression, etc. with a particular scenario.
@SmokeTest Scenario: Search Employee Name
Given: the desired employee will display
@RegressionTest scenario: Search Company Name
Given: Desired Company will display
Read Also:- Complete Guide On BDD Test Automation Frameworks
Cucumber– Hooks: hooks, is a block of code that is run before or after each scenario of the script.
We use methods @Before and @After.
Below is an example, of how we can use hooks in our test script
package utilities;
import cucumber.api.java.After; import cucumber.api.java.Before; public class HookExample { @Before public void beforeTheScenario(){ System.out.println("hit the URL"); } @After public void afterTheScenario(){ System.out.println("Login to the application"); } }
Tagged Hooks: The hook can also use with the tag. You can use @before and @after hooks with a specific test.
For Ex:
@Before (‘@RegressionTest) @After (‘@RegressionTest)
You can also use the same concept of the hook with logic and/or operator.
@Before (‘@RegressionTest, @SmokeTest) @After (‘@RegressionTest, @SmokeTest)
Adding Cucumber Extent Reporter library to Maven Project
Below is the dependencies defined
- Step 1
<dependency> <groupId>net.masterthought</groupId> <artifactId>cucumber-reporting</artifactId> <version>5.5.3</version> </dependency> <dependency> <groupId>tech.grasshopper</groupId> <artifactId>extentreports-cucumber6-adapter</artifactId> <version>2.8.1</version> </dependency>
- Step 2: Make one file called extent. properties in the resource folder
- Step 3: Now, we Define the path in extent. properties, of the folder in which the report will be generated
And the path will be like this:
extent.reporter.spark.start=true extent.reporter.spark.out=Extent Report/PrjctExtentReport.html
- Step 4: Make a test runner class and put the plugin in the test runner class
@RunWith(Cucumber.class) @CucumberOptions( features = "Feature", glue={"stepDefinition"}, tags= “@Regression” or “@Smoke” //whatever the test you want to execute , just put that tag name here plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"}, ) public class TestRunner { }