RelationDigest

Monday, 31 October 2022

[New post] Gradle – Integration of Selenium and JUnit5

Site logo image vibssingh posted: " HOME The previous tutorial explained How to create Gradle project with Selenium and JUnit4 in a Gradle project. In this tutorial, I will explain how we can set up a Gradle project with Selenium and JUnit5. Pre Requisite: Java 8 o" QA Automation Expert

Gradle – Integration of Selenium and JUnit5

vibssingh

Oct 31

HOME

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

Pre Requisite:

  1. Java 8 or above installed
  2. Eclipse or IntelliJ IDE installed
  3. Gradle Installed
  4. Environment variables JAVA_HOME and GRADLE_HOME are correctly configured

This framework consists of:

  1. Java 11
  2. JUnit Jupiter – 5.8.2
  3. JUnit Jupiter Engine - 5.8.2
  4. Gradle – 7.3.3 (Build Tool)
  5. Selenium – 4.3.0

Steps to set up Gradle Java Project for Selenium and JUnit5

  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 JUnit5 dependencies to the Gradle project
  6. Create Pages and Test Code for the pages
  7. Run the tests from Command Line
  8. 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 JUnit5 dependencies to the Gradle project
 /*  * This file was generated by the Gradle 'init' task.  *  */  plugins {     // Apply the application plugin to add support for building a CLI application in Java.     id 'application' }  repositories {     // Use Maven Central for resolving dependencies.     mavenCentral() }  java {     sourceCompatibility = 11     targetCompatibility = 11 }  dependencies {     // Use JUnit Jupiter for testing.     testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'     testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'      implementation 'com.google.guava:guava:30.1.1-jre'     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' }  tasks.named('test') {     // Use JUnit Platform for unit tests.     useJUnitPlatform()  {     }       testLogging {         events "passed", "skipped", "failed"         showStandardStreams = true     }       systemProperties System.properties     reports.html.setDestination(file("$projectDir/GradleReports")) }     
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 BasePage that contains the PageFactory.initElements.

 import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.PageFactory;  public class BasePage { 	 	  public WebDriver driver;  	  public BasePage(WebDriver driver) { 		  this.driver = driver; 		  PageFactory.initElements(driver,this); 	}  } 

Below is the code for LoginPage and HomePage

LoginPage

 import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy;  public class LoginPage extends BasePage{ 	 	 public LoginPage(WebDriver driver) { 		 super(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;                public String getMissingUsernameText() {         return missingUsernameErrorMessage.getText();     }          public String getMissingPasswordText() {         return missingPasswordErrorMessage.getText();     }          public String getErrorMessage() {         return errorMessage.getText();     }         public void login(String strUserName, String strPassword) {       	userName.sendKeys(strUserName);     	password.sendKeys(strPassword);     	login.click();       }  } 

HomePage

 import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy;  public class HomePage extends BasePage {  	public HomePage(WebDriver driver) { 		super(driver); 	}  	  @FindBy(xpath = "//*[@id='app']/div[1]/div[2]/div[2]/div/div[1]/div[1]/div[1]/h5") 	  public  WebElement homePageUserName;  	    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.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager;  public class BaseTests { 	 	public WebDriver driver; 	public final static int TIMEOUT = 10;      	@BeforeEach     public void setup() {     	WebDriverManager.chromedriver().setup(); 	    driver = new ChromeDriver(); 	    driver.manage().window().maximize(); 	    driver.get("https://opensource-demo.orangehrmlive.com/");	     	    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(TIMEOUT));      }       @AfterEach     public void tearDown() {         driver.quit();     }      } 

LoginPageTests

 import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Disabled;  public class LoginPageTests extends BaseTests{ 	      @ParameterizedTest     @CsvSource({             "admin$$,admin123",             "Admin,admin123!!",             "admin123,Admin",             "%%%%%,$$$$$$"})     public void invalidCredentials(String username, String password) {     	    LoginPage objLoginPage = new LoginPage(driver);     	objLoginPage.login(username, password);     	      	// Verify Error Message     	 assertEquals("Invalid credentials",objLoginPage.getErrorMessage());          }          @Test     public void validLogin() {     	    LoginPage objLoginPage = new LoginPage(driver);     	objLoginPage.login("Admin", "admin123");     	      	HomePage objHomePage = new HomePage(driver);     	     	// Verify Home Page     	 assertEquals("Employee Information",objHomePage.getHomePageText());          }          @Test      public void missingUsername() {     	    LoginPage objLoginPage = new LoginPage(driver);     	objLoginPage.login("", "admin123");     	     	     	// Verify Error Message    	     assertEquals("Invalid credentials",objLoginPage.getMissingUsernameText());    	             } 	     @Test @Disabled     public void missingPassword() {     	    LoginPage objLoginPage = new LoginPage(driver);     	objLoginPage.login("admin", "");     	    	     	// Verify Error Message    	     assertEquals("Invalid credentials",objLoginPage.getMissingPasswordText());          }         } 
Step 7 – Run the tests from Command Line

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

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

 gradle clean test 

The output of the above program is

Step 8 – 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.

This report contains detailed information about the failed test, which is shown below.

This shows the list of all the tests - passed, failed, or ignored.

Comment
Like
Tip icon image You can also reply to this email to leave a comment.

Unsubscribe to no longer receive posts from QA Automation Expert.
Change your email settings at manage subscriptions.

Trouble clicking? Copy and paste this URL into your browser:
http://qaautomation.expert/2022/10/31/gradle-integration-of-selenium-and-junit5/

Powered by WordPress.com
Download on the App Store Get it on Google Play
at October 31, 2022
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

RTF Lecture invitation: Strategic Thinking for the Arctic in the 21st Century and Beyond (A Lesson in Nation Build…

Sunday Aug. 10 at 2pm ET ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏  ...

  • [New post] Wiggle Kingdom: April Earnings on Spring Savings!
    Betsi...
  • [New post] Balancing the ‘E’ and ‘S’ in Environment, Social and Governance (ESG) crucial to sustaining liquidity and resilience in the African loan market (By Miranda Abraham)
    APO p...
  • Something plus something else
    Read on bl...

Search This Blog

  • Home

About Me

RelationDigest
View my complete profile

Report Abuse

Blog Archive

  • August 2025 (13)
  • July 2025 (59)
  • June 2025 (53)
  • May 2025 (47)
  • April 2025 (42)
  • March 2025 (30)
  • February 2025 (27)
  • January 2025 (30)
  • December 2024 (37)
  • November 2024 (31)
  • October 2024 (28)
  • September 2024 (28)
  • August 2024 (2729)
  • July 2024 (3249)
  • June 2024 (3152)
  • May 2024 (3259)
  • April 2024 (3151)
  • March 2024 (3258)
  • February 2024 (3046)
  • January 2024 (3258)
  • December 2023 (3270)
  • November 2023 (3183)
  • October 2023 (3243)
  • September 2023 (3151)
  • August 2023 (3241)
  • July 2023 (3237)
  • June 2023 (3135)
  • May 2023 (3212)
  • April 2023 (3093)
  • March 2023 (3187)
  • February 2023 (2865)
  • January 2023 (3209)
  • December 2022 (3229)
  • November 2022 (3079)
  • October 2022 (3086)
  • September 2022 (2791)
  • August 2022 (2964)
  • July 2022 (3157)
  • June 2022 (2925)
  • May 2022 (2893)
  • April 2022 (3049)
  • March 2022 (2919)
  • February 2022 (2104)
  • January 2022 (2284)
  • December 2021 (2481)
  • November 2021 (3146)
  • October 2021 (1048)
Powered by Blogger.