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:
Download and install Eclipse.
Download Selenium WebDriver for your Browser. (In this tutorial, we are using Chrome.)
Creating a Maven Project
Open the Eclipse IDE.
Click on the File → New → Maven Project.
- Click on the checkbox of Create a simple project (skip archetype selection) and then Next button.
- Add the name of your Group ID and Artifact Id and then click on the Finish button.
- Your Maven project has been created and will look like this.
Adding Dependencies
Adding Cucumber JVM
Copy the latest JVM dependency from MavenRepository.
Go to pom.xml file and add the dependencies tag and paste the Cucumber JVM and click on the save button.
Adding JUnit
Copy the latest JUnit dependency from MavenRepository.
Go to pom.xml file and paste the JUnit dependency under Cucumber JVM dependency and click on the save button.
Adding Cucumber JVM JUnit
Copy the latest Cucumber JVM JUnit dependency from MavenRepository.
Go to pom.xml file and paste the Cucumber JVM JUnit dependency under JUnit dependency.
Adding Selenium Java
Copy the latest Selenium Java dependency from MavenRepository.
Go to pom.xml file and paste the Selenium Java dependency under Cucumber JVM dependency.
Let’s Define Test Steps
- Now, create a new folder with the name “Babakom Login“ under src/test/resources folder.
- Now, create a Login.feature file under LoginFeature folder.
If Cucumber Eclipse plugin isn’t already installed, then go to Help and then Eclipse Marketplace, search for Cucumber and install the plugin.
- Now, remove all the content and add your steps definition for a feature using Gherkin Syntax.
- Create a new folder under src/test/java and name it as StepsDefinition.
- Create a new Class under StepsDefinition folder and name it as LoginSteps.
- 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.
- import Cucumber annotations in the Java test script.
Create a Runner Class
- Create a class under StepsDefiniton folder with the name Runner.
- Now, add Cucumber annotation @RunWith(Cucumber.class) just above the class Runner and import Cucumber annotation.
- Also add Cucumber annotation @CucumberOptions(features="",glue={""}) and provide features and steps definition's files location.
- Again add import the Cucumber annotation.
Adding Chrome Driver
To add the Chrome Driver, follow the steps below:
Go to src/test/resources folder and create a new folder under it with the name Drivers.
Now add the Chromedriver.exe file here.
- Now add the following commands in LoginSteps.java file.
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.
- @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.
- @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.
- @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();
- @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.
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.
If all the test steps have been passed, you will see them as Passed in the Console below.
You have created and ran your first test!