Getting Started with Webdriver - Selenium in Java Eclipse

Getting Started with Webdriver - Selenium in Java Eclipse

2019 / 9 / 4

Selenium, Testing, Web Applications, Selendroid

Getting started with webdriver’s java binding is ver easy- all you need to know how to put different pieces together, just like a puzzle.

Component’s you will need:

  1. Eclipse
  2. A Test Framework- We will be using JUnit 5
  3. Wedriver’s Java Code Library
  4. A Browser Driver- We will use Chrome Driver. You can use Safari Driver, Firefox Driver
  5. A Dependency Manager- We will use Maven. Common Dependency Managers are Maven and Graddle. You can use either any of these two or you can use any other dependency manager.

Getting Eclipse

Eclipse is an open-source project maintained by Eclipse foundation and one of the most popular IDE for Java. It has a large marketplace for add-ons, tools and support.
You can download the latest version of Eclipse at Download Eclipse

Eclipse IDE Download

The downloader will save the executable file (.exe) to the browser's “download” directory or to the local drive on your computer. All you need to do is to Download and run it. You will see a list of options.

Eclipse Installer

For this example I have selected Eclipse IDE for Java.

Creating The Project

Eclipse IDE works on the principle of folders and spaces. Different companies and Different teams follow different approaches for workspaces and project structures. Use whatever you are more comfortable with.

In this example we will create a new Java Project by simply clicking on “Create New Java Project” from the opening screen.

Eclipse IDE for Java

Complete the basic information in the form and then click finish to proceed.
NOTE: Please make sure that Java version is 1.8 or higher.

Add Dependency Manager- MAVEN

Maven is a dependency registration and management service that frees you of the task to download and install all dependencies by hand.

Right Click on the Project→Configure→Convert to Maven Project.

Maven Eclipse IDE

Add Dependencies

In the bottom left of the package explorer you will see the pom.xml file.
We will add reference to the JUnit Jupiter and Webdriver into the POM, by adding dependencies xml fragment.
Dependencies are added below build and before </project>.

Here is the snippet that you need to add to the pom.xml file

    <dependencies>
      <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.1.0</version>
      <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>3.141.59</version>
      </dependency>
    </dependencies>

JUnit Jupiter

Adding Webdriver to Eclipse

Before delving into the details on how to add a webdriver in an eclipse project. We first need to see the list of web drivers that are available.

The Browser Driver

It is the webdriver’s lowest component responsible for manipulating browser directly through it’s automation interface. This is the actual program that libraries will call. There a variety of browser driver’s that are available- ChromeDriver for Google Chrome, Geckodriver for Firefox, Internet Explorer Driver for Internet Explorer and Safari Driver for Safari.
Place the browser driver directly in the directory that is part of your System Environment Path variable or you can place it anywhere and mention the exact path to the browser driver in the test case inside the System.setProperty() method.

We will pick webdriver automatically by adding it to the pom.xml file.

Language Bindings

Language bindings are also known as client drivers. These bindings enable you to write test scripts in any language that you are comfortable with. Official bindings are available for Java, Python, Ruby, C#, JavaScript(Node) and many more.

If you are running your tests on one local machine then you need language binding but if you are running tests on many machines then you need a server component.

Selenium Server is a server component when running tests on many machines. This will act as a central hub in grid configuration.

Writing First test case

We will be using JUnit to write test case because of the 2 reasons:

  1. It comes installed with Eclipse
  2. It is very Popular

Right Click on the Eclipse Project-New-JUnit Test Case

JUNit Jupiter

JUnit Jupiter

Give your test case a good name following the naming practice that is followed by your team in your organization.
Now it’s time to do the actual coding and write the test case.

    package org.seleniumhq.selenium.selenium_java;
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import static org.junit.jupiter.api.Assertions.*;
    import org.junit.jupiter.api.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;

    class SampleJUnitTestCase {

      @BeforeEach
          void setUp() {
      }

      @BeforeEach
          void tearDown() {
      }

      @Test
      public void site_header_is_on_home_page() {

            System.setProperty("webdriver.chrome.driver","C:\\Users\\Rashi\\Desktop\\python\\FrugalTesting\\chromedriver.exe");
            WebDriver browser = new ChromeDriver();
            browser.get("https://www.frugaltesting.com");
            WebElement href = browser.findElement(By.xpath("//*[@id=\"get-in-touch\"]/div/div/center/div/a"));
            assertTrue((href.isDisplayed()));
            browser.close();

        }

    }

Here we are opening the homepage of FrugalTesting and closing the browser instance. NOTE: You can either set the exact path of the webdriver-chromedriver in this case inside the System.setProperty() method parameter list or you can set it as Environment variable so that system will automatically pick this webdriver

Run the test by Right Clicking in the body of the test case and then selecting Run Configurations options.You will see the JUnit5 Option in the left hand side of the dialog box that appears.

FrugalTesting

JUnit Jupiter

You might also like