Getting Started With Cucumber | Devstringx Technologies

Devstringx Technologies
3 min readAug 18, 2021

--

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 can understand that what’s going in the test script. In BDD, business analysts, 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 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 be executed

For Ex — Then: the result should be 6

Configuration

Step1- Install Eclipse, & Java.

Step 2- Install Maven.

Step3- Create Maven Project on Eclipse & on Pom.xml, Add 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-Feature — cucumber- Features- Cucumber tests are written, in a file is known as feature files. 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 File name

userSignup.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 the password are incorrect.

The user should navigate to the home page if the username and the password are correct.

Outline − Login functionality for a Twitter site.

The given user navigates to Twitter. When I enter Username as “johndoe@gmail.com” and Password as “invalidPass”. Then, login should be unsuccessful.

Cucumber -Tags: Tag is used to connect a test like smoke, sanity, regression, etc. with a particular scenario.

@SmokeTest Scenario: Search Employee Name

Given: the desired employee will be displayed

@RegressionTest scenario: Search Company Name

Given: Desired Company will be displayed

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, how we can use hooks in our test script

package utilities;import cucumber.api.java.After;import cucumber.api.java.Before;
public class HookExample {@Beforepublic void beforeTheScenario(){System.out.println("hit the url");}@Afterpublic void afterTheScenario(){System.out.println("Login to the application");}}

Tagged Hooks: The hook can also be used 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 logical 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 {}

.

.

Originally published at https://www.devstringx.com on Jul 26, 2021

--

--

Devstringx Technologies
Devstringx Technologies

Written by Devstringx Technologies

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services

No responses yet