Pages

Gradle Project

Creating Selenium Gradle Project in Eclipse



Creating Selenium Gradle Project in Eclipse
What is Gradle? 

Gradle is open-source Build management and automation system based on Groovy-based Domain-specific language instead of XML files, unlike maven or ant. It is been created and disturbed by Apache.


Advantages of Gradle

ü Dependency and distribution management.
ü Integration with Source code management tool like GIT.
ü We can build scripts with a functional programming language.
ü Single Build tool to support multiple programming languages.
ü It has DSL for creating and writing build tasks.

Differences between Gradle and Maven:
S.No
Gradle

Maven
1
An open source build automation tool that is built on concepts of Apache Ant and Apache Maven.

It is compressive software project management tool used primarily for project build automation using Java.
2
Written in Java, Kotlin and Gradle.

It is written in Java.
3
Gradle Build time is short and fast.

Maven performance is slow as compared to Gradle.
4
Gradle Scripts are much short and cleaner.

Maven scripts are a bit lengthy as compared to Gradle.
5
It uses Domain-specific language (DSL)

It uses XML
6
It is based on the task using which work is performed.

In maven goals are defined which is linked to the project.
7
It supports incremental compilations of java class.

It does not support incremental compilations.
Installing Gradle on Windows/Linux/MAC:

You can install the Gradle build tool on Linux, macOS, or Windows.
You can find all releases and their checksums on the releases page.

Prerequisites

Gradle runs on all major operating systems and requires only a Java Development Kit version 8 or higher to run. To check, run java -version. You should see something like this:

❯ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

Gradle ships with its own Groovy library, therefore Groovy does not need to be installed. Any existing Groovy installation is ignored by Gradle.

Gradle uses whatever JDK it finds in your path. Alternatively, you can set the JAVA_HOME environment variable to point to the installation directory of the desired JDK.

SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix-like systems (macOS, Linux, Cygwin, Solaris and FreeBSD). We deploy and maintain the versions available from SDKMAN!.

❯ sdk install gradle

Homebrew is "the missing package manager for macOS".

❯ brew install gradle

Installing manually

Step 1. Download the latest Gradle distribution

The distribution ZIP file comes in two flavors:

  • Binary-only (bin)

  • Complete (all) with docs and sources

Need to work with an older version? See the releases page.

Step 2. Unpack the distribution

Linux & MacOS users

Unzip the distribution zip file in the directory of your choosing, e.g.:

❯ mkdir /opt/gradle
❯ unzip -d /opt/gradle gradle-6.5-bin.zip
❯ ls /opt/gradle/gradle-6.5
LICENSE  NOTICE  bin  README  init.d  lib  media

Microsoft Windows users

Create a new directory C:\Gradle with File Explorer.

Open a second File Explorer window and go to the directory where the Gradle distribution was downloaded. Double-click the ZIP archive to expose the content. Drag the content folder gradle-6.5 to your newly created C:\Gradle folder.

Alternatively, you can unpack the Gradle distribution ZIP into C:\Gradle using an archiver tool of your choice.

Step 3. Configure your system environment

To run Gradle, the path to the unpacked files from the Gradle website need to be on your terminal’s path. The steps to do this are different for each operating system.

Linux & MacOS users

Configure your PATH environment variable to include the bin directory of the unzipped distribution, e.g.:

❯ export PATH=$PATH:/opt/gradle/gradle-6.5/bin

Alternatively, you could also add the environment variable GRADLE_HOME and point this to the unzipped distribution. Instead of adding a specific version of Gradle to your PATH, you can add $GRADLE_HOME/bin to your PATH. When upgrading to a different version of Gradle, just change the GRADLE_HOME environment variable.

Microsoft Windows users

In File Explorer right-click on the This PC (or Computer) icon, then click Properties → Advanced System Settings → Environmental Variables.

Under System Variables select Path, then click Edit. Add an entry for C:\Gradle\gradle-6.5\bin. Click OK to save.

Alternatively, you could also add the environment variable GRADLE_HOME and point this to the unzipped distribution. Instead of adding a specific version of Gradle to your Path, you can add %GRADLE_HOME%/bin to your Path. When upgrading to a different version of Gradle, just change the GRADLE_HOME environment variable.

Verifying installation

Open a console (or a Windows command prompt) and run gradle -v to run gradle and display the version, e.g.:

❯ gradle -v

------------------------------------------------------------
Gradle 6.5
------------------------------------------------------------

(environment specific information)
===============
In this section we will be creating our first Java Selenium program along with TestNG and Gradle as a build automation tool.

To Start working with Gradle we need to install Gradle plugin into our eclipse for that we need to follow below steps:

1)     In Eclipse Click on HelpàEclipse Market Place.

2)     Search for Gradle Plugin

3)     Install the plugin if already not installed. Please make sure to restart your eclipse after installation.


Now to Create our first Gradle project in eclipse we need to perform the below steps:

1)     FileàNewàProject type Gradle in wizards.

2)     We will get option of Gradle project. Please select that

3)     Click on Next 

4)Click On Next and provide name of the project 
5)Click on Next then Review the configuration before starting the creation and import of the Gradle project.


6) Click on Finish. Then Project will be created
 Expand the project in package explorer section different folders will be shown as below


7)Now we would be able to see that along with our basic project structure there are two more new files named “build.gradle” and “settings.gradle” are generated.

settings.gradle file would be used to specify which files we need to include in our build.

build.gradle is the heart of our project in which we need to specify all our dependencies, plugins and repository information the same way we do it in our maven project.

We can also delete the by default package created in src/java and src/test folder.

Now to start working with the Gradle project we need to put the Gradle dependencies in our build.gradle file.

In this section for learning purpose I would be creating simple selenium program along with TestNG.

For this purpose we need to go to maven repository and copy the Gradle dependencies of selenium and testing.


This dependencies we need to copy into the dependencies section of our build.gradle file.

Once adding all the dependencies we need to do right click on project àGradleàRefresh Gradle project this will download all the dependencies in our local.

Now we can create any selenium program for e.g to enter any value in google home page along with its corresponding testing.xml file as well.

package com.qa.test;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class GoogleDatadrivenTest {
private WebDriver driver=null;
private WebDriverWait wait=null;
  @Test(dataProvider = "searchData")
  public void searchTest(String s) {
Reporter.log("Started executing the searchTest(String s): "+s);
Reporter.log("type the " +s+ " keyword in search editbox");
driver.findElement(By.name("q")).sendKeys(s);
Reporter.log("submit on the search editbox");
driver.findElement(By.name("q")).submit();
Reporter.log("verify the selenium - Google Search title");
wait.until(ExpectedConditions.titleContains(s+" - Google Search"));
Reporter.log("wait for the search results text");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("result-stats")));

Reporter.log("fetch the search resultstext");
String txt = driver.findElement(By.cssSelector("div#result-stats")).getText();
Reporter.log("search results text for "+ s + " is--> " + txt);

//Reporter.log("String txt = About 3,82,00,000 results (0.72 seconds)");
String[] str = txt.split(" ");
// str[] = ["About","3,82,00,000","results","(0.72","seconds)"]
// 0 1 2 3 4
Reporter.log("search results count for "+s+"  is-->" + str[1]);
  
  
  
  }
  @BeforeMethod
  public void beforeMethod() {
Reporter.log(" i am inside @BeforeMethod block");
Reporter.log("open the url");
driver.get("http://google.com");
Reporter.log("wait for the home page title");
wait.until(ExpectedConditions.titleContains("Google"));
Assert.assertEquals(driver.getTitle(), "Google");
  }

  @AfterMethod
  public void afterMethod() {
Reporter.log("I am inside @AfterMethod ...");
Reporter.log("clear the cookies");
driver.manage().deleteAllCookies();
  }


  @DataProvider
  public Object[][] searchData() {
   Object[][] data = new Object[3][1];
     //1st row
   data[0][0]="selenium";
   //2nd row
   data[1][0]="UFT";
   //3rd row
   data[2][0] ="cucumber";
   
     return data;
    
  }
  @BeforeClass
  public void launchBrowser() {
Reporter.log("Started executing the @BeforeClass -launchBrowser() ");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
Reporter.log("set the chromedriver.exe path");
System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chromedriver_win32 (2)\\chromedriver.exe");

// interface refobj = new implementedclass()
driver = new ChromeDriver();
Reporter.log("maximize the code");
driver.manage().window().maximize();
Reporter.log("add implicit wait");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Reporter.log("create object for WebDriverWait class");
wait = new WebDriverWait(driver, 30);
  }

  @AfterClass
  public void afterClass() {
  Reporter.log("started executing the afterClass() of @AfterClass block ...");
if (driver != null) {
driver.close();
}
  
  }

  @BeforeTest
  public void beforeTest() {
  Reporter.log("i am inside @BeforeTest block");
  Reporter.log("This annotated block will execute before any test tag related classes");
  }

  @AfterTest
  public void afterTest() {
  Reporter.log("i am inside @AfterTest block");
  Reporter.log("This annotated block will execute after test tag related classes have been ran");
  }

  @BeforeSuite
  public void beforeSuite() {
  Reporter.log("i am inside @BeforeSuite block");
  Reporter.log("This annotated block will execute before any test in the suite have ran");
  }

  @AfterSuite
  public void afterSuite() {
  Reporter.log("i am inside @AfterTest block");
  Reporter.log("This annotated block will execute after all tests have completed");
  }

}
======================================================
Now to execute this we have two options.

1)     We can directly execute this with the help of testing.xml as we usually do.

2)     In Our build.gradle we can create one new task lets name it as a test in this we need to give values like.




To execute this right-click on build.gradle, Select Run as àRun configurations.

Here we need to select our gradle project and in arguments Tab, we need to enter the command gradle test as shown in below screen





Now click on run to execute our first Gradle test.
=========================================================

How to Generate Gradle Report in Selenium TestNG:

How to Generate Gradle Report in Selenium TestNG
There are two basic types of reports that we can generate with Gradle.
1.     TestNG report
2.     Gradle Report

To Configure TestNG Report we need to provide the below configuration in our build.gradle file.

}
Once we execute our test we would be able to view one new folder named AutomationReport which would contain a report like this.



Similarly to configure the Gradle report we need to provide the below configuration in our build.gradle file.




















No comments:

Post a Comment

Note: Only a member of this blog may post a comment.