Java Selenium Automation

1.Project Overview

This guide will cover creating a Java Selenium automation test suite.

The code generated in this guide is hosted at https://github.com/kirbycope/java-selenium

2.Installing the JDK

  1. In your web browser, navigate to http://www.oracle.com/technetwork/java/javase/downloads/index.html
    001
  2. Select “DOWNLOAD” for the JDK
    002
  3. Select “Accept License Agreement”
    003
  4. In the list of files, select the appropriate download package for your system
    004
  5. Select “Save”
    005
  6. After the download has finished, select “Run” from the prompt
    006
  7. If the User Account Control displays, select “Yes”
    007
  8. Select “Next >”
    008
  9. Select “Next >”
    009
  10. Select “Next >”
    010
  11. Select “Close”
    011

3.Installing Eclipse IDE

  1. In your web browser, navigate to http://www.eclipse.org/downloads/packages/
    001
  2. In the list of “Eclipse IDE for Java Developers”, select the appropriate download package for your system
    002
  3. Select “Download”
    003
  4. Select “Save”
    004
  5. After the download has finished, select “Open” from the prompt
    005
  6. Copy the “eclipse” folder to “C:\Program Files”
    06
  7. (Optional) Delete the .zip file
    007
  8. Launch “eclipse.exe”
    008

4.Creating the Framework

  1. Select “OK”
    001
  2. Close the “Welcome” tab
    02
  3. Select “File” → “New” → “Java Project”
    03
  4. Name the project “AutomationFramework” and select “Finish”
    04
  5. Right-click the “AutomationFramework” project and then select “New” → “Folder”
    05
  6. Name the folder “lib” and then select “Finish”
    06

4.1.Adding JUnit to the Framework

  1. In your web browser, navigate to https://github.com/junit-team/junit4/wiki/Download-and-Install
    001
  2. Select the link for “junit.jar” 2
  3. Select “jar” in the top row
    3
  4. Select “Save”
    4
  5. Select “Open folder”
    5
  6. Drag-and-drop the file into the “lib” folder
    6
  7. Select “OK”
    7
  8. Right-click on the file and select “Build Path” > “Add to Build Path”
    8

4.2.Adding Selenium to the Framework

  1. In your web browser, navigate to http://www.seleniumhq.org/download/
    1
  2. Select the link for “Selenium Standalone Server”
    2
  3. Select “Save”
    3
  4. Select “Open folder”
    4
  5. Drag-and-drop the file to the “lib” folder
    5
  6. Select “OK”
    6
  7. Select the “Download” link for the Java Selenium Client
    7
  8. Select “Save”
    8
  9. Select “Open folder”
    9
  10. Right-click the file and select “Extract All…”
    010
  11. Select “Extract”
    11
  12. Drag-and-drop the .jar file into the “lib” folder
    12
  13. Select “OK”
    13
  14. Select the Selenium files in the lib folder, right-click on one of them, and then select “Build Path” → “Add to Build Path”
    14

4.3.Creating a Base Class for Tests

In this framework, we will create a class that our tests will inherit from. Inheritance will allow all of our test classes to share some base functionality or properties without a lot of copy paste. Its purpose in this framework is to define the @Before and @After for every @Test.

  1. In Package Explorer, right-click the “src” folder and then select “New” → “Package”
    001
  2. Name the package “JUnit” and then select “Finish”
    002
  3. In Package Explorer, right-click the “JUnit” package and then select “New” → “Class”
    01
  4.  Name the class “TestBase” and then select “Finish”
    004
  5. In the class, declare a variable to hold a test’s WebDriver (i.e. browser)
    005
  6. Select the “💡” and then select “Import WebDriver”
    006
  7. Below that  variable, define the Base URL
    007
  8. Below that variable, paste in the following code:

    • The @Before will run before a @Test
    • The window is set to full-screen to avoid the browser opening at whatever size you opened it last (change this to handle responsive design)
    • All cookies are deleted to ensure a clean browser session
    • The implicit wait time is extended from 0 (the default) to 30 seconds
    • Fix all the missing Imports by selecting each “💡” and then selecting the first suggestion
  9. Below that method, paste in the following method:

    • The @After will run after a @Test
    • Fix the missing Import by selecting the “💡” and then selecting the first suggestion
  10. Save your work and close the “TestBase.java” tab
    10
  11. In your web browser, navigate to https://sites.google.com/a/chromium.org/chromedriver/downloads
    11
  12. Select the link for the Latest Release
    12
  13. Select the appropriate download for your system
    13
  14. Select “Save”
    14
  15. Select “Open Folder”
    15
  16. Open the archive
    16
  17. Copy “chromedriver.exe” to the location specific in step 8 (above)
    17

4.4.Creating A Common Utilities Class

Methods that can be considered global (not only needed for one project) should be part of the framework. One simple example is taking a screenshot of the WebDriver (i.e.browser) when a test fails.

  1. In Package Explorer, right-click on the “src” folder and select “New” → “Package”
    001
  2. Name the package “Utilities” and then select “Finish”
    002
  3. Right-click on the “Utilities” package and select “New” → “Class”
    003
  4. Name the class “SeleniumUtils” and then select “Finish”
    004
  5. In the class, paste the following code:

    • Note: Check the next screenshot to make sure you have added the correct Imports
  6. Save your work and close the “SeleniumUtils.java” tab
    006

5.Creating a Test Suite

A Test Suite is a collection of tests cases intended to test an application. In our paradigm, we will create a new Visual Studio solution that references the AutomationFramework. This allows us to test multiple web applications using the same framework. The Test Suite will contain the tests and necessary page object models (POMs).

  1. Select “File” → “New” → “Java Project”
    01
  2. Name the project “TimothyCopeCom” and then select “Finish”
    02
  3. Right-click the “TimothyCopeCom” project and then select “New” → “Folder”
    03
  4. Name the folder “lib” and then select “Finish”
    04
  5. Open the workspace on your local machine, copy all the libs of the AutomationFramework to the TimothyCopeCom
    5
  6. Refresh Project Explorer
    006
  7. Select the files in the new “lib” folder, right-click and then select “Build Path” → “Add to Build Path”
    007

5.1.Referencing the Framework

  1. In Package Explorer, right-click the “TimothyCopeCom” project and then select “Properties”
    01
  2. Select “Java Build Path”
    02
  3. Select “Projects”
    003
  4. Select “Add…”
    04
  5. Select “AutomationFramework” and then select “OK”
    05
  6. Select “Apply”
    06
  7. Select “Libraries”
    7
  8. Select “Add Class Folder…”
    8
  9. Select the checkboxes for “JUnit” and “Utilities” and then select “OK
    9
  10. Select “Apply”
    010
  11. Select “Project References”
    10
  12. Verify the checkbox for “AutomationFramework” is selected and then select “OK”
    7

5.2.Creating a Page Object Model

A Page Object Model (POM) is used in the Page Object Design Pattern. The POM is composed of the following components:

  1. In Project Explorer, right-click the “src” folder and then select “New” → “Folder”
    01
  2. Name the folder “POMs” and then select “Finish”
    02
  3. Right-click the “POMs” folder and then select “Add” → “Class”
    03
  4. Name the class “HomePageObject” and then select “Finish”
    04

5.2.1.Create the Constructor

  1. In the class, paste in the following code:
  2. Fix all the missing Imports by selecting the “💡” and then selecting the first suggestion
    02

5.2.2.Create the Find Element Logic

  1. Below the constructor, type public WebElement linkHome;
    01
  2. Select the “💡” and then select “Import WebElement”
    02
  3. Above that, type @Findby(css = "")
    03
  4. Select the “💡” and then select “Import FindBy”
    04
  5. In Chrome, open http://timothycope.com/
    015
  6. Right-click on the first item in the navigation menu and select “Inspect”
    016
  7. In the Elements panel, right-click on the element and then select “Copy” → “Copy selector”
    017
  8. In the quotes of the FindsBy statement, paste in the copied selector
    08

5.2.3.Create the Page Specific Methods

  1. In the class, paste in the following code:
  2. Save your work and close the “HomePageObject.java” tab
    2

5.3.Creating a Test

  1. In Package Explorer, right-click the “src” folder for the “TimothyCopeCope” project and then select “Add” → “Folder”
    01
  2. Name the folder “Tests” and then select “Finish”
    02
  3. Right-click the “Tests” folder and then select “New” → “Class”
    03
  4. Name the class “HomePageTests” and then select “Finish”
    04
  5. Modify the class to inherit from the TestBase class by adding extends TestBase
    05
  6. Select the “💡” and then select “Import TestBase”
    6
  7. In the class, paste in the following code:

    • Fix the missing Imports (see screenshot in next step to verify)
  8. Save your work and close the “HomePageTests.java” tab
    008

5.4.Running the Tests

Steps 1 and 2 in this section are optional as when you run a test the JUnit view opens automatically. They are included to show how to open the view without running tests.

  1. Select “Window” → “Other…”
    01
  2. Expand “Java”, select “JUnit”, and then select “OK”
    02
  3. In Package Explorer, right-click on “HomePageTests”, and then select “Run As” → “JUnit Test
    03
  4. After the test executes, you will see the results in the JUnit View
    04

6.Adding Grid

Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.
– http://www.seleniumhq.org/docs/07_selenium_grid.jsp#what-is-selenium-grid

6.1.Getting and Starting Grid

  1. In your web browser, navigate to http://www.seleniumhq.org/download/
    011
  2. Select the download link under Selenium Standalone Server
    012
  3. Select “Save”
    013
  4. Select the Windows key and type cmd
    014
  5. Select “Command Prompt”
    015
  6. Type cd Downloads and then press [Enter]
    016
  7. Type java -jar selenium-server-standalone-<version>.jar -role hub and then press enter
    017
    • Replace <version> with the version number of Selenium Server Standalone
  8. In your web browser, navigate to http://localhost:4444/grid/console
    018
  9. Select “view config”
    019
  10. Verify that the role is “hub”
    020
  11. (FYI Only – Do not do while following this guide) To stop the hub press [Ctrl]+[C] in the Command Prompt
    021

6.2.Starting the Node

The node for this example will run locally (same machine that the hub is on) but ideally will be setup on another machine.

  1. Select the Windows key and type cmd
    001
  2. Select “Command Prompt”
    002
  3. Type cd Downloads and then press [Enter]
    003
  4. Type java -Dwebdriver.chrome.driver=<path> -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register and then press [Enter]
    04
    • Replace <path> with the path to the chromedriver.exe
      • This should be in ..\c-sharp-selenium\AutomationFramework\AutomationFramework\bin\PROD
    • Replace <version> with the version number of Selenium Server Standalone
  5. In your web browser, navigate to http://localhost:4444/grid/console
    005
    • You’ll see your registered node (on port 5555)
  6. (FYI Only – Do not do while following this guide) To stop the hub press [Ctrl]+[C] in the Command Prompt
    006

6.3.Using Grid to Run Tests

This guide uses a WebDriver setup. For Selenium 1 RC, follow the official quick-start guide.

  1. In Eclipse, open TestBase.java
    001
  2. Below the baseUrl, type protected String hubUrl = "http://localhost:4444/wd/hub";
    002
  3. In the setUp() method, comment out the first two lines
    003
  4. Below that add the following code:
  5. Save your work and close the TestBase.java tab
    005
  6. (With the Hub and Node running) Run the test
    006
Suggest Edit