Gradle with Selenium

This blog is for the automation testers who are aware of Maven build tool. We will not venture into the typical points such as what Gradle, why it is used or how is different from Maven etc but will see how to use Gradle as a dependency management for Selenium and TestNG. 

Installing Gradle

  • Download the binary from Gradle site. Look for the Binary-only
  • Unzip to your local drive
  • Set Environment variables
  • Check whether its installed properly or not

Check Gradle version

Go to cmd and enter command gradle -v

Now let’s see how it used in a test automation project

Tools used: Eclipse IDE (Version: Neon.3 Release (4.6.3))

After installing the Gradle, it’s time to set the Gradle User Home: path. Follow Windows -> Preference -> Gradle and set the home path.

Create a Gradle Project

File -> Project  -> Gradle Project 

Select and click on Next and again Next 

Click Next

Click Next

Check the Home Path and Click on Finish. Once the project is created, it will look like the below

Adding dependencies such as Selenium and TestNG

build.gradle is the file where we will add the dependencies. I have removed all the comments just to give a cleaner look to the content of build.gradle file.

apply plugin: 'java'
 
repositories {
    jcenter()
}
 
// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    testCompile 'junit:junit:4.12'
}

By default junit is added as dependency. Lets replace it with testng and also add selenium dependencies and save it.

apply plugin: 'java'
 
repositories {
    jcenter()
}
 
// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
 compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.14.0'
 // https://mvnrepository.com/artifact/org.testng/testng
 testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
}

Right click on build.gradle -> Gradle -> Refresh Gradle Project to add the dependencies to the project. Once added, the same can be verified by expanding Project and External Dependencies.

Next Steps:

  • Create a Test case
  • Create WebDriverSession file to intialize the driver etc
  • Add src/test/resources folder and add chromedriver.exe file
  • Create a testng.xml file in src/test/resources folder
  • Add it to build.gradle file
  • Run the test case from command line

WebDriverSession.java

package com.test;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class WebDriverSession {
  
 WebDriver webDriver = null;
 
 public void setChromeDriver(){
  System.out.println("####################### SET UP CHROME DRIVER ###########");
  System.setProperty("webdriver.chrome.driver",
    System.getProperty("user.dir") + "\\src\\test\\resources\\chromedriver.exe");
  webDriver = new ChromeDriver();
  webDriver.manage().window().maximize();
 }
  
 public void init() {
  System.out.println("This is init in Webdriver session");
  String browserType="chrome";
   
  switch(browserType){
  case "chrome":
   setChromeDriver();
   break;
  default:
   System.out.println("######### No Browser Type provided #################");
  }
   
  webDriver.get("https://www.hm.com/se");
 }
  
 public void tearDown(){
  webDriver.quit();
 }
}

TestCaseOne.java

package com.test;
 
import org.testng.annotations.Test;
 
public class TestCaseOne {
  @Test (description="My First Test case", groups = {"smoketest"})
  public void FirstTestCase() {   
   WebDriverSession webDriverSession = new WebDriverSession();
   webDriverSession.init();
   System.out.println("Test case one is executed");
   webDriverSession.tearDown();
  }
   
  @Test (description="My First Second case", groups = {"smoketest"})
  public void SecondTestCase() {
   System.out.println("Test case Two is executed");
  }
   
  @Test (description="My First third case", groups = {"smoketest"})
  public void ThirdTestCase() {
   System.out.println("Test case Three is executed");
  }
}

Create testng.xml file

<suite name="Suite">
  <test name="Test" thread-count="5">
    <classes>
      <class name="com.test.TestCaseOne">
    </class></classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Once testng.xml file is created, make sure it runs successfully. Let’s add the testng.xml file to the build.gradle file and run it from command line in a typical build cycle way.

Add to build.gradle file

Note: Check the line 16 to 24 which adds the testng.xml file.

apply plugin: 'java'
 
repositories {
    jcenter()
}
 
// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
 compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.14.0'
 // https://mvnrepository.com/artifact/org.testng/testng
 testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
}
 
    test{
    useTestNG() {
      suites 'src/test/resources/testng.xml'
      testLogging.showStandardStreams = true
  testLogging {
         events "PASSED", "FAILED", "SKIPPED"
      }  
     }
    }
  • test – This runs a collection of test cases using any supported test library — JUnit, JUnit Platform or TestNG — and collates the results. 
  • useTestNG – configure which test groups to include or exclude during the test execution via the Test.useTestNG(org.gradle.api.Action) setting
  • testLogging.showStandardStreams – Printing sysout in the console if running the test cases through commandline.
  • testLogging – To update the test status as Passed or failed etc

Now lets run the test suite from command line: Go to the folder where the project is created. And run the command: gradle clean test

Output is as below which is self explanatory.

About This Site

The main aim of this site is to share knowledge with fellow software test specialists who are keen to grow both technically and professionally.

Categories