First test using selenium and cucumber with java

Introduction 

According to Behavior Driven Development (BDD) framework, we have user stories in the form of Requirement Documentation or Business Requirement Documentation. Based on this multiple features and scenarios are created. The feature file is created with the help of Gherkin keyword. This feature file contains multiple features and in the same feature, we can have multiple scenarios or scenarios outlined, for each and every scenario different scenario steps are written. 

In this blog, we will discuss- 

  • Project setup for our first test 
  • How to add maven dependencies 
  • How to download browser driver files 
  • How to create a Feature file and add Scenario and Steps 
  • How to create Step Definition/Glue Code for the steps 
  • How to add Selenium WebDriver code 
  • How to run and check the execution of the Feature file  

Project setup for our first test 

Firstly, we must install all prerequisites in our project (for more info about prerequisites you can visit- Web Automation using Selenium and Cucumber with Java

Add the maven dependencies: 

After verifying the prerequisites, add the required dependencies as shown below 

To get the maven dependencies first we need to visit the below link: 

https://mvnrepository.com/repos/central  and then we need to search for the required dependency  

Project setup- Cucumber

Select Cucumber JUnit>click on the latest version>copy the dependency

JUnit

> add it to the pom.xml file 

Similarly, we need to search for Cucumber JVM:JUnit(io.cucumber)> Select Cucumber JVM:JUnit(io. cucumber) >click on the latest version>copy the dependency 

Cucumber JVM

> add it to the pom.xml file 

Next, we need to search for Selenium Java(org.seleniumhq.selenium)>Select Java(org.seleniumhq.selenium)> click on the latest version>copy the dependency 

Selenium Java

> add it to the pom.xml file 

Below is an image of the pom.xml file after all the dependencies have been added 

pom.xml

Download browser driver files 

In order to run our tests on the browsers I.e., Chrome, Firefox, etc. we need to download the browser driver files. 

To get the browser drivers first we need to visit the link below: https://www.selenium.dev/   

Go to the Downloads section> click on Browsers> select documentation for desired browser, e.g., chrome> download the current release of chrome driver as per the version of chrome in your OS 

Downloads
Index

After downloading the driver, we need to extract its folder and provide the exe file from it in our project. 

To add the driver to our project we need to create a folder under ‘src/test/resources’ then copy the driver exe and paste it into that folder. 

Project explorer- cucumber

Another way to use web drivers is to add its dependency to pom.xml file 

To get web driver dependency we need to visit the link below: 

https://mvnrepository.com/repos/central  and then we need to search for the required dependency as shown in the above steps 

Select WebDriverManager(io.github.bonigarcia)>click on the latest version>copy the dependency 

WebDriver Manager

> add it to the pom.xml file 

Create a Feature file, add Scenario and Steps 

To create a feature file, we need to right-click on the features folder> click on New> go to File>provider File name with ‘.feature’ extension> click on Finish 

Create a feature file

Next, we need to create the features, scenarios for the features, and the steps to follow for the specific feature as per the scenario. 

login.feature

In the above example, a feature file has been created, and it contains a Feature to launch the browser and test the search functionality, also the Scenario for this feature is to validate the search functionality followed by the Steps given by using Given, When, Then, And which will instruct the process for testing the particular feature. 

Create Step Definition/Glue Code for the steps 

Step definitions basically provide a backend code for the steps that we have written in the feature file so that the processor will know the exact code to be executed through the step definition for each step that we have created in the feature file. 

First, we need to add Step Definitions/Glue Code under ‘src/test/java package’ 

To add the Step Definitions/Glue Code> right click on src/test/ java package> go to New> select Folder>Provide the name of the folder>click on Finish 

After creating the Steps folder right click on it> go to New> select class> provide the name of the class>click on Finish 

Next, we need to run the feature file and then copy the provided functions for Given, When, And, Then under the message stating to implement the missing steps from the console and paste it into the class created in the steps folder  

Launch Browser java

Next, we again need to run the feature file and get the results as shown in the image below in the console. 

Add Selenium WebDriver code 

To run the tests using the selenium web driver we need to do the following steps 

Go to the step definition class and write the code that will execute the test using selenium on the browser as shown in the below image:

Code explanation: 

In this code, we have added the selenium web driver codes for every function as per the steps that we need to follow. In the first function, we have a code to open the browser, where the System.setProperty() provides the path where the exe file for the driver is located, and then with driver=new ChromeDriver(); it will launch the chrome browser, and will open the https://www.google.com/ URL through driver.get(). 

In the second function, the driver.findElement(By.xpath(“”)) will locate the web element I.e., the search box by its xpath locator and will input the text stating ‘Neova Solutions’ through sendKeys() into the search box 

In the third function driver.findElement(By.xpath(“”)) will again locate the web element I.e., the search box by its xpath locator and will perform a keyboard action on the search box by simulating the ENTER key press through  sendKeys(Keys.ENTER) 

The fourth function uses driver.close() to close the browser after navigating to the search results page. 

Execution and results: 

After writing the code into the class file of the step definitions folder, we need to run the feature file and check the execution results. The results in the console should be passed as shown in the image below 

Another approach to execute the feature file is by creating a runner class  

To create a runner class we need to right-click on the steps folder>go to new> select class> provide the name of the class>click on Finish 

After Creating the test runner class file, we need to add the cucumber annotation and options. Then provide the location of the feature files and step definitions as shown in the below image: 

To run the test runner class file, we need to right-click on the class file> select Run As> select JUnit Test. 

After executing the test runner class, we should get JUnit results as passed as shown in below image: