Java Selenium web automation example in gradle

Selenium WebDriver became a de-facto standard for automating the web based UI testing. It can also be very useful in screen scraping or crawling kind of applications. Here we have a basic example of web UI automation using Selenuium framework. We will use gradle as the build tool.

We can simply call it as Hello World of Selenium.

Folder structure of source files

1java-selenium-web-automation-gradle-example
2├──build.gradle
3├──settings.gradle
4└──src
5   └──main
6      └──java
7         └──com
8            └──firstfewlines
9               └──SeleniumSample.java

1. build.gradle
 1group 'com.firstfewlines'
 2version '1.0-SNAPSHOT'
 3
 4apply plugin: 'java'
 5apply plugin: 'application'
 6
 7sourceCompatibility = 1.8
 8
 9mainClassName = 'com.firstfewlines.SeleniumSample'
10
11repositories {
12    mavenCentral()
13}
14
15dependencies {
16    compile('org.seleniumhq.selenium:selenium-java:3.4.0') {
17        exclude group: 'org.seleniumhq.selenium', module: 'selenium-android-driver'
18        exclude group: 'org.seleniumhq.selenium', module: 'selenium-iphone-driver'
19        exclude group: 'org.seleniumhq.selenium', module: 'selenium-safari-driver'
20        exclude group: 'org.webbitserver', module: 'webbit'
21        exclude group: 'commons-codec', module: 'commons-codec'
22        exclude group: 'cglib', module: 'cglib-nodep'
23    }
24}

2. settings.gradle
1rootProject.name = 'selenium-example'
2

3. SeleniumSample.java

Here in the main method, we are initializing the ChromeDriver, opening the Google website and search for a text. Once the works done we should close the web browser using webDriver.quit() that is commented here so that we can see the result.

 1package com.firstfewlines;
 2
 3import org.openqa.selenium.By;
 4import org.openqa.selenium.Keys;
 5import org.openqa.selenium.WebDriver;
 6import org.openqa.selenium.WebElement;
 7import org.openqa.selenium.chrome.ChromeDriver;
 8import org.openqa.selenium.support.ui.ExpectedConditions;
 9import org.openqa.selenium.support.ui.WebDriverWait;
10
11import java.net.URL;
12
13public class SeleniumSample {
14
15    public static void main(String [] argv) throws Exception {
16
17        WebDriver webDriver = new ChromeDriver();
18
19        webDriver.navigate().to(new URL("https://www.google.com"));
20
21        WebDriverWait wait = new WebDriverWait(webDriver, 10000);
22        wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.tagName("input"), 0));
23
24        WebElement input = webDriver.findElement(By.cssSelector("#lst-ib"));
25
26        input.sendKeys("Spark Java file upload example - First Few Lines");
27        input.sendKeys(Keys.ENTER);
28
29//        webDriver.quit();
30    }
31}

Download complete source code:


How to run the application?

  • Make sure you have the latest version of Google Chrome installed.
  • Please download Selenium Webdriver for Google Chrome from http://chromedriver.storage.googleapis.com/index.html
  • Place the chromedriver executable in a folder and add that folder to system path
  • Make sure you have the latest version of Gradle installed and PATH is configured.
  • Download the attached source code zip file and unzip it to your local folder.
  • Go to the directory where you unzipped the file i.e. where the build.gradle is there
  • Run gradle clean run command
  • The application should start a Google Chrome and do the search automatically as stated in the code

Alternatively, you can use IntelliJ IDEa or Eclipse to import the Gradle project and run from there.


References:


comments powered by Disqus