How to Build a Robust Automation Framework with Cucumber and Selenium

theAutoBot
4 min readMar 17, 2023

--

Automation testing has become an essential part of software development as it helps to find bugs and errors in the application quickly. When it comes to automation testing, Cucumber and Selenium are two of the most popular tools that developers use to test their applications. Cucumber is a Behavior Driven Development (BDD) framework that uses Gherkin language to write test cases, and Selenium is a web testing framework that allows developers to automate web browsers. In this article, we will guide you through the process of building a robust automation framework with Cucumber and Selenium.

Step 1: Setup
To build an automation framework with Cucumber and Selenium, you need to first install the following software:

  • Java Development Kit (JDK): Install JDK on your computer as it is required to run Java-based applications.
  • Eclipse: Download and install the latest version of Eclipse, which is an Integrated Development Environment (IDE) used to write and execute Java programs.
  • Selenium WebDriver: Install the Selenium WebDriver Java client library.
  • Cucumber: Add the Cucumber dependency in your pom.xml file if you are using Maven as your build tool.

Step 2: Project Structure
After installing the required software, create a new Maven project in Eclipse. In the project, create the following directories:

  • src/main/java — This folder contains all the Java classes.
  • src/main/resources — This folder contains all the configuration files.
  • src/test/java — This folder contains all the Cucumber test cases.

Step 3: Create Feature File
In Cucumber, you need to write the test cases in a feature file. Create a new feature file under src/test/resources/features with the extension “.feature”. In the feature file, write the test case scenarios in Gherkin language. For example:

Feature: Login Functionality
Scenario: Login with valid credentials
Given User is on the login page
When User enters valid username and password
Then User should be logged in successfully

Step 4: Code for Initialization
Now that we have set up the environment for our automation framework, we need to write the code for initialization. This code will help in launching the browser and navigating to the desired URL. Below is the sample code for initialization:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.Before;

public class Hooks {
public static WebDriver driver;

@Before
public void initializeTest() {
String browserName = System.getProperty("browser.name");
if(browserName.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
driver = new ChromeDriver();
}
else if(browserName.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.get("https://www.google.com/");
}
}

Step 5: Create Step Definitions
After creating the feature file, create a new Java class under src/test/java to write the step definitions for the scenarios. Step definitions are used to map the Gherkin language to actual code that will be executed. For example:

public class LoginSteps {
@Given("^User is on the login page$")
public void user_is_on_the_login_page() {
//code to navigate to the login page
}

@When("^User enters valid username and password$")
public void user_enters_valid_username_and_password() {
//code to enter valid username and password
}

@Then("^User should be logged in successfully$")
public void user_should_be_logged_in_successfully() {
//code to verify successful login
}
}

Step 6: Create Test Runner
To run the Cucumber test cases, create a new Java class under src/test/java and annotate it with @RunWith(Cucumber.class). This class will act as the test runner. For example:

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features")
public class TestRunner {
}

Step 7: Execute Test Cases
To execute the test cases, right-click on the TestRunner class and select Run As -> JUnit Test. The test cases will run and the results will be displayed in the console.

Conclusion:
In this article, we have shown you how to build a robust automation framework with Cucumber and Selenium. With the right setup and organization, you can create an efficient and effective testing framework that will help you to catch bugs and errors early on in the development process. So, get started with building your own automation framework today and take your automation testing to the next level. Remember to keep your test cases organized, maintain a clear directory structure, and follow best practices for writing reusable and maintainable code.

By using the power of Cucumber and Selenium, you can achieve better test coverage, faster test execution, and more reliable test results. With Cucumber, you can write tests in a clear, human-readable format that makes it easy to communicate test cases to non-technical stakeholders. And with Selenium, you can automate your web application tests with ease, ensuring that your application is functioning as expected across a variety of browsers and operating systems.

So, if you are looking to streamline your testing process, consider building an automation framework with Cucumber and Selenium. With these powerful tools at your disposal, you can create a testing process that is efficient, effective, and scalable, helping you to deliver high-quality software that meets the needs of your users.

If you are looking for Cucumber Tutorial.

If you are looking for Cucumber Framework follow me here:

--

--

theAutoBot

In this digital era, where technology is growing within seconds. I'm just utilizing the power of Artificial Intelligence.