Setup Selenium, JUnit, and Eclipse

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

Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.
http://docs.seleniumhq.org/

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
http://junit.org/

Eclipse is a platform that has been designed from the ground up for building integrated web and application development tooling. By design, the platform does not provide a great deal of end user functionality by itself. The value of the platform is what it encourages: rapid development of integrated features based on a plug-in model.
https://www.eclipse.org/

Setup Eclipse

  1. Go to https://github.com/junit-team/junit/wiki/Download-and-Install and download “junit.jar”
    • (Optional) You can also grab the “hamcrest-core.jar”
      • “junit.jar” contains common functions of hamcrest, which can be expanded by using the core library
  2. Go to http://www.seleniumhq.org/download/ and download “Selenium Server” and “Selenium Client”
    • We will be using the Java client, which comes in an archive. We are only concerned with the .jar not including “srcs” in the file name
  3. Go to https://www.eclipse.org/downloads/ and download the version you need for your operating system
    • I use Eclipse IDE for Java Developers
  4. Extract the archive
    • (Optional) Move the extracted folder to your application folder
  5. Run the executable for Eclipse
  6. Select a workspace
  7. Click File -> New -> Java Project
    • For standard IDE Find/Select Java Project
  8. Name your project and click Finish
    • The default settings are good for this tutorial
  9. Create a new folder called “lib” in your project to hold the external assemblies
    • The name does not matter, but should make sense
  10. Drag-and-Drop the .jar files for Selenium Server, Selenium Client, and JUnit into the new folder
    • When prompted, select Copy Files
  11. Expand the lib folder and select the .jar files
  12. Right-click and select Build Path -> Add to Build Path
    • This will create a new folder called “Referenced Libraries” that contains the binaries for the .jar files

Creating Your First Test

Until you become a Subject Matter Expert you will likely be using the Selenium IDE for Firefox to write your tests. You can download the IDE from http://www.seleniumhq.org/download/

  1. Open Firefox
  2. Click the Selenium icon to the right of the address bar
  3. Enter https://www.google.com/ as your Base URL
  4. Click the record button
    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
  5. Click the Play Current Test button
    • Ensure all steps are highlighted in green, meaning the test passed
  6. Click File -> Export Test Case As… and select Java/JUnit4/Webdriver
  7. Save as test01.java in your project’s src folder
  8. Refresh your project in Eclipse (F5)
  9. Fix any errors in the code and save
    • Remove package com.example.tests;
    • Rename public class Test01 to test01
  10. Right-click somewhere in the code file and select Run As -> JUnit Test
    • Or, click the green run button on the Eclipse toolbar
  11. A browser window should open and run the test
  12. The test results should display on the left pane of the Eclipse window