Selenium 3.0 – Introduction and Installation (Selenium, Maven, Junit, IntelliJ)

Introduction

Selenium is the most popular free open source web automated testing suit. Available across a wide range of browsers and platforms.

seleniumsuite

In this tutorial we are going to dig deeper into Selenium 3.0 and how to configure and run our first automated test. Selenium 3.0 (Also known as Selenium WebDriver) offers a well-designed Object Oriented API that can communicate with the application under test via language binding with native browser drives. In simpler words Selenium WebDriver provides us with an interface that we can utilize to use the browsers native ability to interact with the application under test.

seleniuminterface

As a result of being a thin wrapper that is language binding with browsers WebDriver can support multiple language and browsers. For our tutorial we are going to demonstrate building our first test using Java and the Firefox.

Installation and Setup

In this tutorial we are going to install and setup Selenium using the following tools and frameworks.

Automation Framework: Selenium 3.0 (Selenium WebDriver)

Java Test Runner: Junit (TestNG can also be used)

Web Browser: Firefox.

Browser Driver: Gecko Driver.

Build System: Apache Maven.

IDE: Idea IntelliJ.

Note: This tutorial follows the steps from the installation tutorial from Software Test Academy. The main difference is adapting to the changes in Selenium 3.0.

Download and Install Java Development Kit (JDK)

Download Link: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Instruction for successful installation:

  • Select the appropriate JDK version and select “Accept License Agreement” to begin download.
  • After downloading the JDK proceed with installation.
  • Add the “JAVA_HOME” in your environmental variables.
    • On “My PC” right-click and press properties.
    • From the left side panel select “Advanced System Settings”

      env-variable-1
      Click on “Advanced system settings” on the left.
    • Click on ‘Environment Variables’

      env-variable-2
      Click ‘Environment Variables’
    • Add New Variable “JAVA_HOME” with the value set to the location the JDK was installed usually “C:\Program Files\Java\jdkenv-variable-3
    • Confirm successful installation of JDK by running the following command in the command prompt “java -version”, the following screen shot shows the expected response.sussesful-jdk

Download and Setup Apache Maven (build tool)

Download Link: https://maven.apache.org/download.cgi

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. I find it much easier setting up Selenium through Maven.

Instruction for successful installation:

  • Download latest Binary “Zip Archive Version” (eg. “apache-maven-3.3.9-bin.zip”)
  • Unzip downloaded folder in “C:/Program Files”
  • Add the Maven folder in the environmental variables.
    1. On “My PC” right-click and press properties.
    2. From the left side panel select “Advanced System Settings”
    3. Click on “Environmental Variables”
    4. Add New Variable “PATH” with the value set to the location the Maven folder is located in (Eg. “C:\Program Files\apache-maven-3.3.9\bin”)
    5. Confirm successful installation of Maven by running the following command in the command prompt “mvn -v”, the following screen shot shows the expected response.maven-sussesful

Download and Setup Gecko Driver (Firefox Driver)

Download Link: https://github.com/mozilla/geckodriver/releases

Selenium used to be responsible for providing the driver for each browser. Starting with Selenium 3.0 each browser vendor is responsible of providing its own implmentation for its driver. All browsers follow the W3C specification for browser automation. For Gecko Driver (Still in Beta) is currently the appropriate driver for Firefox.

Instruction for successful installation:

  • Download “Gecko Driver”
  • Unzip driver to “C:\Gecko Driver”

geckodriver

Download and Install Idea IntelliJ IDE

Download Link:  https://www.jetbrains.com/idea/download/

Intellij is my preferred choice for an IDE. I recommend using Idea IntelliJ for valid reasons; however eclipse could also be used. No need for instruction just click ‘next’ on the installation manager and you should be all set.

Setting up the enviroment

  1. Run IntelliJ IDE and click on ‘File’ and ‘New Project’.
  2. Select Maven from the left side and locate your Java SDK on the top and click ‘Next’.intellij-1
  3. Fill GroupID and ArtifactID and click ‘Next’. (I went for “SeleniumTestOne” however anything should work.intellij-2
  4. Fill in the project name as you like and click ‘Finish’. (Again I went for “SeleniumTestOne”)
  5. After clicking ‘Finish’ press on ‘Enable Auto-Import’ to automatically import the files added in the pom.xml file.intellij-4
  6. Setting up the pom.xml file to import the needed Selenium and JUnit libraries.
  7. Go to the project tree it should be shown as below.
    • Right click on ‘java’ then new ‘Package’, and give your package a name. (My example “FirstSeleniumTest”)
    • Right click on the newly created ‘Package’ then new ‘Java Class’, give your class a name. (My example “SeleniumFirstTest”)projecttree
  8. Write your first Java test automation code as below:
    package FirstSeleniumTest;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class SeleniumFirstTest {
       @Test
       public void testOne(){
           System.setProperty("webdriver.gecko.driver", "C:\\GeckoDriver\\geckodriver.exe");
           //creae new WebDriver
           WebDriver FireFoxDriver = new FirefoxDriver();
           //Open google
           FireFoxDriver.get("https://www.google.com/ncr");
           //insert Hello! in search area
           FireFoxDriver.findElement(By.name("q")).sendKeys("Hello!");
        }
    }
  9. Press run and wait for the result.firstresult

We have concluded running our first automation test using Selenium, IntelliJ, Maven and JUnit. If you have any questions please feel free to ask. 🙂

Sources:

  1. http://www.seleniumhq.org/docs/
  2.  http://www.swtestacademy.com/quick-start-to-selenium-webdriver-with-java-junit-maven-intellij/
  3. http://www.guru99.com/introduction-to-selenium.html
  4. http://toolsqa.com/selenium-webdriver/selenium-introduction/
  5. https://dzone.com/articles/why-idea-better-eclipse
Selenium 3.0 – Introduction and Installation (Selenium, Maven, Junit, IntelliJ)