Skip to main content

Creating Automated Tests Using the Selenium Web Driver

Welcome to the ultimate guide on automating your testing processes. In this tutorial we are going to explain how to use Java Cucumber BDD Framework using Selenium WebDriver. Here is the step-by-step tutorial.

Make sure all the following applications are installed

Make sure all the following applications are installed:

Creating a Maven Project

  1. Open the Eclipse IDE.

  2. Click on the File → New → Maven Project.

Web Driver 1

  1. Click on the checkbox of Create a simple project (skip archetype selection) and then Next button.

Web Driver 2

  1. Add the name of your Group ID and Artifact Id and then click on the Finish button.

Web Driver 3

  1. Your Maven project has been created and will look like this.

Web Driver 4

Adding Dependencies

Adding Cucumber JVM

  1. Copy the latest JVM dependency from MavenRepository.

  2. Go to pom.xml file and add the dependencies tag and paste the Cucumber JVM and click on the save button.

Web Driver 6

Adding JUnit

  1. Copy the latest JUnit dependency from MavenRepository.

  2. Go to pom.xml file and paste the JUnit dependency under Cucumber JVM dependency and click on the save button.

Adding Cucumber JVM JUnit

  1. Copy the latest Cucumber JVM JUnit dependency from MavenRepository.

  2. Go to pom.xml file and paste the Cucumber JVM JUnit dependency under JUnit dependency.

Adding Selenium Java

  1. Copy the latest Selenium Java dependency from MavenRepository.

  2. Go to pom.xml file and paste the Selenium Java dependency under Cucumber JVM dependency.

Web Driver 7

Let’s Define Test Steps

  1. Now, create a new folder with the name “Babakom Login“ under src/test/resources folder.

Web Driver 8

  1. Now, create a Login.feature file under LoginFeature folder.

Web Driver 9

If Cucumber Eclipse plugin isn’t already installed, then go to Help and then Eclipse Marketplace, search for Cucumber and install the plugin.

Web Driver 10

  1. Now, remove all the content and add your steps definition for a feature using Gherkin Syntax.

Web Driver 11

  1. Create a new folder under src/test/java and name it as StepsDefinition.

Web Driver 12

  1. Create a new Class under StepsDefinition folder and name it as LoginSteps.

Web Driver 13

  1. Create functions for each mentioned steps defined in the Login.feature file. Also, add Cucumber annotations against each step. Make sure the steps are exactly similar.

Web Driver 14

  1. import Cucumber annotations in the Java test script.

Web Driver 15

Create a Runner Class

  1. Create a class under StepsDefiniton folder with the name Runner.

Web Driver 16

  1. Now, add Cucumber annotation @RunWith(Cucumber.class) just above the class Runner and import Cucumber annotation.

Web Driver 17

  1. Also add Cucumber annotation @CucumberOptions(features="",glue={""}) and provide features and steps definition's files location.

Web Driver 18

  1. Again add import the Cucumber annotation.

Web Driver 19

Adding Chrome Driver

To add the Chrome Driver, follow the steps below:

  1. Go to src/test/resources folder and create a new folder under it with the name Drivers.

  2. Now add the Chromedriver.exe file here.

Web Driver 20

  1. Now add the following commands in LoginSteps.java file.

Web Driver 21

Make sure the path of ChromeDriver is accurate, otherwise this will not work.

Creating your First Test Case

As we have defined the test steps for our testcase in Login.feature files. Now, we are going to add the code for each steps which are defined as a method in our LoginSteps class.

  1. @Given Test Method:

For @Given Test Method, we will add the following code:

driver.manage().window().maximize()
driver.navigate().to("<https://babakom-develop.babakom.sa/login/login>")

The first line of the code will maximize the browser’s window, and the second line will navigate the user to Babakom login page.

Web Driver 22

  1. @When Test Method:

For @When Test Method, we will add the following code:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.findElement(By.id("69c9a06e-3923-5ae8-5249-5f88d1127e8d")).sendKeys("");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.findElement(By.id("93fee15a-c1c0-4b9d-f8a3-0ca9c3553887")).sendKeys("");

Enter your email in sendkeys in line no 2, and your password in line 4.

Web Driver 23

  1. @And Test Method

For @And Test Method, add the code for clicking on the Login button.

driver.findElement(By.xpath("//*[@id=\"LE_0c118eb6-577a-d619-d687-921ac0484b74\"]/evi-button/button")).click();

Web Driver 24

  1. @Then Test Method

Now, add the implicit wait command for the 4th step to see whether the user is navigated to the babakom homepage or not.

Web Driver 25

Let’s Run the code

Now, to run your code go to Login.feature file and click on the Run and then Run as 1 Cucumber Feature.

Web Driver 26

If all the test steps have been passed, you will see them as Passed in the Console below.

Web Driver 27

You have created and ran your first test!