Selenium (webdriver) ‘Hello world’ Example

There are various tools where web testing can be achieved. One of the better tool is selenium. This blog will demonstrate basic web testing with selenium webdriver and Java. This blog assumes that you already have basic Java programming knowledge.

Prerequisites:

    • IntelliJ
    • Java 8 SDK
    • Maven
    • TestNG
    • Chrome browser (using chrome webdriver)
    • Webdriver manager

Setting up

You can skip this section if you already have Java IDE setup with maven. If not, please follow the instructions below.

It’s handy to use a good Java IDE like Intellij, please download it from the website, a free community edition, will do perfectly fine, please install it.

Also make sure you install Java SDK, following the download url here.

After that we will create a new Maven project, by first clicking “Create New Project”

Screen Shot 2019-07-15 at 19.57.52

Make sure you select the Project SDK by navigating to the folder Java SDK installed.  Choose the “Maven” and click on next.

Screen Shot 2019-07-15 at 20.02.13

Provide a proper groupid and artifactId and click on next.

Screen Shot 2019-07-15 at 20.07.03

We have reached the final step for maven within Intellij. Provide a proper project name and location and click on Finish.

Screen Shot 2019-07-15 at 20.08.20

Configuring Maven

Setting the right Java libraries dependencies to enable the Selenium webdriver testing; a quick description of each library:

  1. TestNG; is a great Java Testing framework and this will be used to write selenium tests
  2. ChromeDriver; as part of Selenium webdriver for the ChromeBrowser

Screen Shot 2019-07-15 at 20.18.54

Open the pom.xml and have the following inserted after the tag <version>,

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.141.59</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
    </dependency>
</dependencies>

Chrome Webdriver Installation

In this example chrome webdriver will be used. In this case you need to have chrome browser driver installed on your machine. Follow this url to download the Chrome browser driver. There are multiple versions, please use the version  of the chrome browser on your machine. To check this open Chrome, go to help, and click “About Google Chrome” to get the version.

Important
It’s a zip file, please extract and put the executable in the OS path. For mac, copy the file to the path: /usr/local/bin/. For the Windows Operation System, you need to add it to the path as well. 

As alternative, you have to add this piece of code in the class to refer to the executable.

System.setProperty("webdriver.chrome.driver","C:/Your/Path/to/chromedriver.exe");

Development

We are all settled, let’s create a Java class where the power of Selenium webdriver is being demonstrated. A new class can be created by right-click on the Java directory and click new “Java Class”

Screen Shot 2019-07-15 at 20.26.39

Let’s call it “DemoSelenium”

Screen Shot 2019-07-15 at 20.28.47

Just to get you off the ground, this is the most basic test you can get.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * Created by TripleQA
 */

public class DemoSelenium {

    private WebDriver driver;

    @BeforeClass
    public void init(){
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test(testName = "helloWorld")
    public void tstHelloWorldGoogle() throws InterruptedException {
        driver.get("http://www.google.com/xhtml");
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("TripleQA hello world");
        searchBox.submit();
        //Deliberately adding pause
        Thread.sleep(15000);
    }

    @AfterClass
    public void cleanup(){
        if(driver !=null)
            driver.quit();
    }
}

Let’s have a deeper look into code. For the ones who are unfamiliar with TestNG. It has some powerful java annotations, the @BeforeClass will load the ChromeDriver class, before the test class is being executed.

    @BeforeClass
    public void init(){
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

The java annotation @Test will define a new test case by using java method.

    @Test(testName = "helloWorld")
    public void tstHelloWorldGoogle() throws InterruptedException {
        driver.get("http://www.google.com/xhtml");
        Thread.sleep(5000);
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("TripleQA hello world selenium");
        searchBox.submit();
        //Deliberately adding pause
        Thread.sleep(15000);
    }

This is the actual test method where we invoke the ChromeDriver. Firstly we create a new ChromeDriver(). After that we tell the chrome/web driver  to go to the url http://www.google.com (get statement). Thread sleeps for 5 seconds to give you the opportunity to see the browser. This is the interesting part:

  WebElement searchBox = driver.findElement(By.name("q"));

Websites are full of web elements, such as input box, button, text box and so on. These web elements can be identified/found by  using several cool webdriver API functionalities. In this particular example, the method By.name(“q”) is being used, which means that it is looking for an element with the attribute name ‘q’. You have to use browser developer tool to see the attributes (see screenshot below)
webelement

Well that’s it,  hopefully this give you a start by exploring the world of web automation with Selenium webdriver. See more samples.

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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