Introduction to Selenium WebDriver

Selenium WebDriver lets you write code to perform automated tests of web pages. Unlike Selenium IDE (which records clicks that you perform in the browser and plays them back), WebDriver requires you to write code to connect to the API which controls the browser. The code can be written in Java, C# or a few other languages.

The Selenium WebDriver documentation has some good instructions, but while following them, I ran into a few hitches, but was able to find the answers I needed from other pages on the internet. Hopefully I have referenced those other pages correctly.

Obtaining Selenium WebDriver for Use with Java

  1. Install Apache Maven. This will be used for downloading a project that can be imported into Eclipse or NetBeans.
  2. Create the pom.xml file shown on the WebDriver documentation page.
    • Make sure to update the XML file with any potentially updated information on the Maven Download page.
  3. Add the Maven installation directory to your PATH.
  4. In the command prompt, navigate to the directory where you put the pom.xml file.
  5. Run this command: mvn clean install (As shown in the WebDriver Documentation)
  6. If you get an error about JAVA_HOME not being set, then:
    1. Install the Java Development Kit. (You need the JDK, not just the JRE. Thanks Atlassian for the clarification!)
    2. Add a JAVA_HOME variable to your system environment variables. Make sure it points to the home directory of where you installed the JDK.
    3. If you still get the same error, restart your computer and run the command again. (Thanks Michael Nesterenko's for your response at Stack Overflow for the idea of restarting the machine.)
  7. If the command runs successfully, you should now have a folder called "target" that was created in the same folder as your pom.xml file.

Setting up a Selenium WebDriver Project for Java in Eclipse

  1. Use MVN to create an Eclipse project in by running this command in the same folder as your pom.xml file: MVN eclipse:eclipse (as seen in these instructions.)
  2. Import the project into Eclipse. See instructions.
  3. If your project has errors about the M2_REPO classpath variable being missing.
  4. Your project should have no errors at this point.
  5. Add a new class to your project, with a static main method.
  6. Run your application to see if it will even run.
  7. If you get the error: "Editor does not contain a main type", then:
  8. Your empty application should at least run at this point.
  9. In the main() method, write your test code. Refer to this example Selenium WebDriver code.
  10. If you are using the Firefox driver in your code, then do the following before running, or you will get an error:
    • Make sure Firefox is installed.
    • Add the path to Firefox in your system's environment path variable.
    • Restart Eclipse
  11. If you are using the Internet Explorer driver in your code, then do the following before running or you will get an error:
  12. If you are using the Chrome driver in your code, then do the following before running or you will get an error:
    1. Download and unzip the Chrome driver.
    2. Add the path to it in your system's environment path variable.
    3. Restart Eclipse.
  13. Run your application.
  14. Assuming you have an actual test script set up in your code, Firefox or Internet Explorer (depending on which browser driver you used in your code) should launch at this point and the test should be performed automatically.


Popular posts from this blog

Tic Tac Toe - Using App Inventor Beta

Xamarin Forms vs React Native

Solved: Ionic ENOENT Invalid device state error when using sudo on Mac