Cucumber Framework with Selenium WebDriver

The Cucumber framework can be integrated with any Java-based test automation framework. In this tutorial, we’ll see how to use Selenium WebDriver with Cucumber for a simple test scenario

Required Libraries:

     1. Cucumber (core, java, junit, picocontainer)
     2. Selenium WebDriver
     3. WebDriverManager
     4. JUnit
     5. Maven Surefire Plugin

Setting up the Project

     1. Create a Java project in your IDE.
     2. Use Maven as the build tool.
     3. Find pom.xml and add the dependencies. 

Example pom.xml

<dependencies>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.13.0</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>7.13.0</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.4.1</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.13.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>7.13.0</version>
</dependency>

</dependencies>


Selenium Cucumber BDD Project Structure

Browser Invocation in Hooks

Set up and close the browser in a Hooks class using Cucumber annotations:
@Before – launch browser
@After – close browser
This ensures each scenario starts and ends cleanly.

Use the Runner class for Execution:

The Runner file is placed in src/test/runners. We can specify the feature files to be executed, tags to filter scenarios, and HTML report configurations. Using the Maven Surefire plugin, the scripts can be executed through the command line.

Reporting

After execution, reports will be generated and stored inside the reports folder. These can be enhanced using plugins like pretty, html, or extent-reports.

GitHub Source Code: You can find a demo Selenium Cucumber BDD project in ths github report: https://github.com/qafeast/CucumberJavaSelenium/blob/main
 

Related Tutorials