Setup Eclipse for Automation Testing

Update

This article is still relevant but a more comprehensive guide exists at http://timothycope.com/c-selenium-automation. This new guide also covers Page Object Design Pattern (POM) and Selenium Server (Grid).

Background

Eclipse is an IDE for many languages and in this case we will use it for Java development. Tests created using Selenium IDE can be exported to Java and ran in Eclipse using the JUnit plugin.

Getting What you Need

Eclipse will be an executable while jUnit and Selenium will be .jar files.

  1. Eclipse (for Java Developers) – http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/mars1
  2. JUnit – https://github.com/junit-team/junit/wiki/Download-and-Install 
  3. Selenium (Server and Client) – http://www.seleniumhq.org/download/
  4. Selenium IDE – http://www.seleniumhq.org/download/

Setting It All Up

  1. Install Eclipse
    1. For OSX, when the .tar.gz file opens move the extracted app to your Applications folder.
  2. Run Eclipse
    1. For OSX, open your Applications folder and then right-click Eclipse and select Open. Select Open from the prompt.
  3. For now, just use the default workspace. So, simply select Ok when the workspace prompt appears
  4. Close the welcome screen by clicking the X on its tab
    1. You should now see a blank project explorer
  5. From the menu select File -> New -> Java Project
  6. Name the project “Example” and select Finish
  7. Right-click in the Package Explorer pane and select New -> Folder
  8. Select “Example” as the parent folder
  9. Name the new folder “lib” and select Finish
    1. This will act as our “libraries” folder
  10. Copy over the .jar files to the “lib” folder (you can drag and drop)
    1. junit-#.#.jar
    2. selenium-server-standalone-#.#.#.jar
    3. selenium-java-#.#.#.jar
  11. Select Copy files on the prompt
  12. Select all files in the lib folder, right-click and select Build Path -> Add to Build Path
    1. This will copy the files into a folder called “Referenced Libraries” in your Package Explorer

Creating an Example Test Using Selenium IDE

  1. Install Selenium IDE if you haven’t already
    1. Use Firefox to download using the link on the Selenium downloads page
  2. Open Firefox
  3. Click the Selenium icon to the right of the address bar
  4. Enter https://www.google.com as your Base URL
  5. Click the record button (if not already recording)
    1. Navigate to https://www.google.com/
    2. Type Tim
    3. Click Search button
    4. Right-click on the page and select waitForTitle Tim – Google Search from the context menu
    5. Click the record button to stop recording
  6. Click the Play Current Test button
    • Ensure all steps are highlighted in green, meaning the test passed
  7. Click File -> Export Test Case As… and select Java/JUnit4/Webdriver
  8. Save as “test01.java

Adding the Test to the Eclipse Project

  1. Add/Drag-and-drop the .java file into the “src” folder of your project
    1. Select Copy file if prompted
  2. There will be errors reported during the import so let’s fix those:
    1. In the Package Explorer, drill down to the newly added test and double-click it
      1. Example -> src -> default package -> test01.java
    2.  The declared package “com.example.tests” does not match the expected package “” test01.java /Example/src line 1 Java Problem
      1. Remove the line “package com.example.tests;”
    3. The public type Test01 must be defined in its own file test01.java /Example/src line 12 Java Problem
      1. Rename the class “test01”
    4. Save the changes and refresh the package explorer (F5)

Running Your First Test

  1. Right-click the test in the package explorer and select Run As -> JUnit Test
  2. Eclipse will open the JUnit pane and execute the test in Firefox (the default WebDriver).
  3. Upon completion you should see the result(s)

Running More Tests

You can add more test to the default package. When you want to run them all you can select the default package folder and run as a JUnit test.

Running Tests Using a Headless Browser

A headless browser is an “internet browser” without a GUI. For this example, we will use HtmlUnit. It is an HtmlClient wrapper for Java applications. Get it here, http://sourceforge.net/projects/htmlunit/files/htmlunit/.

  1. Download the .jar file
  2. Add the .jar file to your “lib” folder
  3. Add the file to your build path
  4. Using the test created (above):
    1. Using //, comment out the line private WebDriver driver;
    2. Add a line under that, private HtmlUnitDriver driver;
      1. Hover over WebClient and select Import ‘HtmlUnitDriver’ (org.openqa.selenium.htmlunit)
      2. This will add the reference for WebClient from the HtmlUnit jar
    3. Using //, comment out the line driver = new FirefoxDriver();
    4. Add a line under that, driver = new HtmlUnitDriver();
    5. Add a line under that, driver.setJavascriptEnabled(true);
  5. Save your changes
  6. Execute the test
    1. Note: HtmlUnit will show a bunch of warnings, ignore them for now as it is normal behavior
      1. These warnings related to the page being loaded (akin to DevTools Console) and not the test itself
    2. Note: Google changes IDs based on browsers so this example will fail since it was recorded in Firefox and will be ran using HtmlUnit [Source].
      1. You can set the User-Agent by setting, driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38);
      2. You will also have to hover over BrowserVerison and add the missing reference, Import ‘BrowserVersion’ (com.gargoylesoftware.htmlunit)
      3. Documentation on BrowserVersion