How to create Gradle project with Selenium and JUnit4


The previous tutorial explained How to create Java Gradle project in Eclipse. In this tutorial, I will explain how we can set up a Gradle project with Selenium and JUnit4.

This framework consists of:

  1. Java 8 or above
  2. JUnit– 4.13.2
  3. Gradle – 7.5.1 (Build Tool)
  4. Selenium – 4.3.0

Steps to set up Gradle Java Project for Selenium and TestNG

  1. Download and Install Java on the system
  2. Download and setup Eclipse IDE on the system
  3. Setup Gradle on System
  4. Create a new Gradle Project
  5. Add Selenium and JUnit4 dependencies to the Gradle project
  6. Create Pages and Test Code for the pages
  7. Run the tests from JUnit
  8. Run the tests from Command Line
  9. Gradle Report generation

Project Structure

Implementation Steps

Step 1- Download and Install Java

Selenium needs Java to be installed on the system to run the tests. Click here to know How to install Java.

Step 2 – Download and setup Eclipse IDE on the system

The Eclipse IDE (integrated development environment) provides strong support for Java developers. Click here to know How to install Eclipse.

Step 3 – Setup Gradle

To build a test framework, we need to add several dependencies to the project. This can be achieved by any build tool. I have used Gradle Build Tool. Click here to know How to install Gradle.

Step 4 – Create a new Gradle Project

If you want to create the Gradle project from Eclipse IDE, click here to know How to create a Gradle Java project.

Step 5 – Add Selenium and JUnit4 dependencies to the Gradle project

plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'

repositories {
    // Use Maven Central for resolving dependencies.

java {
    sourceCompatibility = 11
    targetCompatibility = 11

dependencies {
    // Use JUnit test framework.
    testImplementation 'junit:junit:4.13.2'

    // This dependency is used by the application.
    implementation ''
    implementation 'org.seleniumhq.selenium:selenium-java:4.4.0'
    implementation 'io.github.bonigarcia:webdrivermanager:5.3.0'

application {
    // Define the main class for the application.
    mainClass = 'com.example.App'

test {
    useJUnit {

    testLogging {
        events "passed", "skipped", "failed"
        showStandardStreams = true


Step 6 – Create Pages and Test Code for the pages

We have used PageFactory model to build the tests. I have created a package named pages and created the page classes in that folder. Page class contains the locators of each web element present on that particular page along with the methods of performing actions using these web elements.

This is the BaseClass that contains the PageFactory.initElements.

import org.openqa.selenium.WebDriver;
public class BasePage { 
      public WebDriver driver;
      public BasePage(WebDriver driver) {
          this.driver = driver;

Below is the code for LoginPage and HomePage


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage extends BasePage{
     public LoginPage(WebDriver driver) {
    @FindBy(name = "username")
    public WebElement userName;
    @FindBy(name = "password")
    public WebElement password;
    @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[1]/div/span")
    public WebElement missingUsernameErrorMessage;
    @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[1]/div/span")
    public WebElement missingPasswordErrorMessage;
    @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button")
    public WebElement login;
    @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/div/div[1]/div[1]/p")
    public  WebElement errorMessage;
   // Get the error message when password is blank
    public String getMissingPasswordText() {
        return missingPasswordErrorMessage.getText();

   // Get the error message when username is blank
   public String getMissingUsernameText() {
        return missingUsernameErrorMessage.getText();
    // Get the Error Message
    public String getErrorMessage() {
        return errorMessage.getText();
    public void login(String strUserName, String strPassword) {


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class HomePage extends BasePage {
    public HomePage(WebDriver driver) {
     @FindBy(xpath = "//*[@id='app']/div[1]/div[2]/div[2]/div/div[1]/div[1]/div[1]/h5")
      public  WebElement homePageUserName;
      // Get the User name from Home Page
        public String getHomePageText() {
           return homePageUserName.getText();

Here, we have BaseTests Class also, which contains the common methods needed by other test pages.

import java.time.Duration;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class BaseTests {
	public WebDriver driver;
	public final static int TIMEOUT = 10;
    public void setup() {
	    driver = new ChromeDriver();

    public void tearDown() {


import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

public class LoginTests extends BaseTests{
    public void invalidCredentials() {
	    LoginPage objLoginPage = new LoginPage(driver);
    	objLoginPage.login("Admin", "admin123$$");
    	// Verify Error Message
    	 Assert.assertEquals("Invalid credentials",objLoginPage.getErrorMessage());
    public void gotoHomePage() {
	    LoginPage objLoginPage = new LoginPage(driver);
    	objLoginPage.login("Admin", "admin123");
    	HomePage objHomePage = new HomePage(driver);
    	// Verify Home Page
    	 Assert.assertEquals("Employee Information",objHomePage.getHomePageText());
    public void missingUsername() {
	    LoginPage objLoginPage = new LoginPage(driver);
    	objLoginPage.login("", "admin123");
    	// Verify Error Message
   	     Assert.assertEquals("Invalid credentials",objLoginPage.getMissingUsernameText());
    @Test @Ignore
    public void missingPassword() {
	    LoginPage objLoginPage = new LoginPage(driver);
    	objLoginPage.login("admin", "");
    	// Verify Error Message
   	     Assert.assertEquals("Invalid credentials",objLoginPage.getMissingPasswordText());

Step 7 – Run the tests from JUnit

Right-click on the Tests and select Run As -> JUnit Test

The output of the above program is shown below.

Step 8 – Run the tests from Command Line

Note:- As you can see, my project has two parts – GradleSeleniumJUnit4Demo and GradleSeleniumJUnit4Demo-app.

Go to the app project and run the tests, using the below command

gradle clean test

The output of the test execution is

Step 9 – Gradle Report generation

Once the test execution is finished, refresh the project. We will see a folder – GradleReports. This report is generated when the tests are executed through the command line.

This folder contains index.html.

Right-click on index.html and select open with Web Browser. This report shows the summary of all the tests executed. As you can see that Failed tests are selected (highlighted in blue), so the name of the test failed along with the class name is displayed here.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s