Basic Test Automation with Cucumber and Selenium

The Inquisitive Dev
6 min readNov 2, 2020

--

This post will guide you through basic test automation with cucumber and selenium. After initial setup and installation, we will write a simple automated test. The test will open Google Chrome, browse to the Selenium homepage and checks that the UI displays correctly.

Please bare in mind that this article was published on 02/11/2020. As time goes on some of the code examples and setup may become outdated.

Test Automation and BDD

We’re going to write out our tests in BDD format (Behaviour Driven Development). In BDD, the business requirements, defined by the stakeholders, are used to construct test scenarios before any lines of code are written. This helps to ensure that tests are not written as an afterthought, as is often the case. It also helps the developers to write code that is easily testable.

Once the tests are complete we start writing the code, which by default will fail, as the test assertions are not met. As we complete parts of the functionality, tests will begin to pass and we can confirm that we have met the business requirements. If we then change something and the tests start failing, we know straight away that we are violating those requirements.

These test cases are effectively the application’s specification. They form a “living document” that clearly describes the applications behaviour. This ‘document’ is run regularly against the application as part of the test automation suite. As new functionality is added to the application we also add new test scenarios to this document. To achieve this we use a tool called Cucumber.

What is cucumber?

https://cucumber.io/tools/cucumber-open/

Cucumber is an open-source tool to help write test scripts in a BDD format. Because Cucumber is widely adopted in the developer community it is easy to get questions answered in a variety of web forums.

Cucumber uses a syntax called Gerkin and breaks test scenarios down into three parts:

  • Given (something is true)
  • When (an event happens)
  • Then (the expected outcome)

Defining test scenarios in terms of these three steps helps to standardise the testing process for developers. We can use Cucumber to run tests against both the backend and frontend of an application. However, if we want to run realistic test automations on an application’s UI we will need another tool. Selenium.

What is Selenium?

https://www.selenium.dev/

Selenium is an open-source, test automation testing framework that works with multiple browsers. It simulates a user interacting with the application and can ‘click through’ pre-defined journeys in the app.

The Selenium suite consists of:

Selenium IDE — Record and playback user actions within your browser. The IDE can export the source code created for use in projects.

Selenium Grid — Allows you to run tests simultaneously on different machines running different browsers and operating systems.

Selenium WebDriver — A web automation framework that allows you to execute tests directly against different browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. This is what we will be using in this tutorial.

Installation:

GitHub

All the code required is on my github:

https://github.com/bnaylor1989/seleniumTutorial

Use the following command to download to a directory of your choice.

git clone https://github.com/bnaylor1989/seleniumTutorial.git

Selenium dependencies

As we are using a Maven project, all the necessary dependencies are listed in the POM file. To use Selenium for Java we need to add the following dependency:

  • selenium-java

Once this is added you can check the Maven Dependencies folder in your project. You will see that Maven has not only downloaded the jar file for us but also any other transitive dependencies required.

You can find the latest dependencies by browsing to the link below and searching for the required artefact, e.g. selenium-java.

https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java

Cucumber dependencies

To use Cucumber into our project the following dependencies are included in the POM file:

  • cucumber-core
  • cucumber-java
  • cucumber-junit
  • cucumber-jvm-deps

The POM should look like this:

ChromeDriver

Because WebDriver directly calls methods of different browsers, each browser has a corresponding driver. ChromeDriver simply helps WebDriver do this job on Google Chrome.

Make sure you download the version of ChromeDriver that matches your version of Chrome otherwise you will get an error when you try to run your tests. Once downloaded place it in the resources folder of your project then reference in BasePage.java as shown in the source further down this article.

https://chromedriver.chromium.org/downloads

Project structure Overview:

Overview of Cucumbers component parts

  • The config class
  • The test runner class
  • The feature file
  • The Step definitions class
  • The Selenium code

The config class — BasePage.java

This class sets a system property that tells the application where it can find the WebDriver tool that will interact with the browser. The WebDriver object is also instantiated in this class meaning any other classes that need to use it can reference this instance.

The Test Runner Class — CucumberTests.java

This is the java class that will kick the tests off

@RunWith — This is a JUnit annotation that specifies which runner it has to use to execute this class. We have provided Cucumber.class as a parameter with this annotation. With this, JUnit will know that it has to execute this test case as a Cucumber test.

@CucumberOptions: This annotation provides the location of the feature file as well as the step definition class in your project.

The Feature File — SeleniumHomepage.feature

Represents test cases in plain English. Written in Cucumbers language, Gherkin. Each feature file can contain one or more scenarios. A scenario represents a traditional test case. The steps define the actual test steps (in the form Given, When, Then)

The Step Definitions Class — SeleniumHomepageSteps.java

This class contains the execution behind each step in the feature file.

The text following each of the annotations (@Given, @When, @Then) is what links back to the feature file.

Each step defined in the feature file should have a corresponding logic in the step definitions file. We make assertion on test data, such as whether the WebElement returned by seleniumHomepage.getHeaderNavbar() is displayed on the page.

In true object oriented fashion the methods being called in the stepdef file are defined in the SeleniumHomePage class. This helps with maintainability.

The Selenium Code — SeleniumHomePage.java

This class actually contains the Selenium code that is being called in the stepdefs class.

Here we instantiate a new ChromeDriver object to interact with our browser then tell it to go to the Selenium website.

In getHeaderNavbar() we ask selenium to locate an element on the webpage with an id attribute of “navbar” and return it as a WebElement.

Running the tests

Now you can run the CucumberTests class as JUnit test.

You will see the Google Chrome browser open and the steps outlined in the feature file scenarios performed.

The Junit panel will show the results from running the scenarios.

Note: If you are working on a mac and get an error along the lines of:

“The driver is not executable”

Locate the driver in your project and locate it with terminal

Enter the following command to convert it to an executable.

chmod +x chromedriver

Sounding Off

I hope this article on test automation with cucumber and selenium has been useful to people. Thank you for taking the time to read and it’d be awesome if you could subscribe to my blog!

--

--