Selenium Java Tutorial

In this tutorial, we will be creating a simple selenium project in java to learn how we can automate web testing through UI. It is like simulating how the user interacts in the web browser, thus testing how the application works whenever the user performs an action such as click in an element in the browser.

Selenium Java Tutorial

We will be using Maven as our build tools and Java 8. If you are not familiar with Maven, you can read more how you can start a Maven project using the link below:
How to install Maven in Windows
How to create Web Application using Maven

Adding Selenium Dependency in Maven

Add the selenium dependency from maven repository. It should be similar like this:



        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>

Download the appropriate Web Driver

In order for us to automated the browser, we need the web driver of the browser that we will be using. Below are the links where you can download the web driver for Chrome, Firefox, and Internet Explorer. You may want to take note the version of your current browser and only download the driver that supports it.

  1. Google Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
  2. Firefox: https://github.com/mozilla/geckodriver/releases
  3. Internet Explorer: http://selenium-release.storage.googleapis.com/index.html?path=3.4/

Copy the exe file to your project resources folder.

Setting System Properties

Selenium uses system property to get the paths of the drivers. Therefore, we need to set our driver absolute paths in system property.

private void setProperties() throws URISyntaxException {
    URL chromeURL = getClass().getClassLoader().getResource("driver" + File.separator + "chromedriver.exe");
    System.setProperty("webdriver.chrome.driver", Paths.get(chromeURL.toURI()).toFile().getAbsolutePath());

    URL firefoxURL = getClass().getClassLoader().getResource("driver" + File.separator + "geckodriver.exe");
    System.setProperty("webdriver.gecko.driver", Paths.get(firefoxURL.toURI()).toFile().getAbsolutePath());

    URL ieURL = getClass().getClassLoader().getResource("driver" + File.separator + "IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", Paths.get(ieURL.toURI()).toFile().getAbsolutePath());
}

Creating a WebDriver Instance

Next, we’ll create an instance of WebDriver. WebDriver.java is an interface and the implementation depends on what browser you will be using. For example, the following codes show how you can create a WebDriver for different browser:

switch(browser) {
    case CHROME:
        webDriver = new ChromeDriver();
        break;
    case FIREFOX:
        webDriver = new FirefoxDriver();
        break;
    case IE:
        webDriver = new InternetExplorerDriver();
        break;
}

After this, you will have a web driver that you can use to automate the browser.

Setting the browser URL

Using the web driver, that we have just created, we can set the target url of the browser using the get method of web driver. For example, if we want to go to google.com, we can do it like this:

webDriver.get("https://google.com");

Finding elements in Web Driver

Now that we have the target page and loaded the HTML in the browser, we can now get the elements that we want to click or type characters. There are different ways to find an element in the web driver. The By class contains ways how you can find the element:

 By.id(String id);
 By.className(String className);
 By.cssSelector(String cssSelector);
 By.linkText(String linkText);
 By.partialLinkText(String partialLinkText);
 By.name(String name);
 By.tagName(String tagName);
 By.xpath(String xpath);

Using the method of WebDriver#findElement and passing the static method of By class, we can then find the element.

WebElement searchFieldElement = webDriver.findElement(By.id("lst-ib"));

Sending actions to element

We can now send characters or click the element that we’ve got. The WebElement class contains the element object. This class contains different method to manipulate the element. For example, to send characters/text to an input field element, we can use:

searchFieldElement.sendKeys("selenium tutorial javapointers.com");

or to click the element button,

WebElement searchButtonElement = webDriver.findElement(By.name("btnK"));
searchButtonElement.click();

Testing the Application

Finally, create a main method to start and test the application. Below is the whole code of our SeleniumTutorial.java

public class SeleniumTutorial {
    enum Browser {
        CHROME, FIREFOX, IE
    }

    private WebDriver webDriver;

    public SeleniumTutorial(Browser browser) throws URISyntaxException {
        setProperties();
        switch(browser) {
            case CHROME:
                webDriver = new ChromeDriver();
                break;
            case FIREFOX:
                webDriver = new FirefoxDriver();
                break;
            case IE:
                webDriver = new InternetExplorerDriver();
                break;
        }
    }

    private void setProperties() throws URISyntaxException {
        URL chromeURL = getClass().getClassLoader().getResource("driver" + File.separator + "chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", Paths.get(chromeURL.toURI()).toFile().getAbsolutePath());

        URL firefoxURL = getClass().getClassLoader().getResource("driver" + File.separator + "geckodriver.exe");
        System.setProperty("webdriver.gecko.driver", Paths.get(firefoxURL.toURI()).toFile().getAbsolutePath());

        URL ieURL = getClass().getClassLoader().getResource("driver" + File.separator + "IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", Paths.get(ieURL.toURI()).toFile().getAbsolutePath());
    }

    public void goToGoogleAndSearch() {
        webDriver.get("https://google.com");
        WebElement searchFieldElement = webDriver.findElement(By.id("lst-ib"));
        searchFieldElement.sendKeys("selenium tutorial javapointers.com");
        searchFieldElement.sendKeys(Keys.ENTER);
    }

    public void quit() {
        webDriver.quit();
    }

    public static void main(String[] args) throws URISyntaxException {
        SeleniumTutorial seleniumTutorial = new SeleniumTutorial(Browser.CHROME);
        seleniumTutorial.goToGoogleAndSearch();
        seleniumTutorial.quit();
    }
}

There are more things to learn in Selenium. In our next tutorial, we will learn how we can wait for the element to be visible or for page to load before we click or send text in an element.

Share this tutorial!