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.
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
- Install Apache Maven. This will be used for downloading a project that can be imported into Eclipse or NetBeans.
- 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.
- Add the Maven installation directory to your PATH.
- In the command prompt, navigate to the directory where you put the pom.xml file.
- Run this command: mvn clean install (As shown in the WebDriver Documentation)
- If you get an error about JAVA_HOME not being set, then:
- Install the Java Development Kit. (You need the JDK, not just the JRE. Thanks Atlassian for the clarification!)
- Add a JAVA_HOME variable to your system environment variables. Make sure it points to the home directory of where you installed the JDK.
- 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.)
- 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
- 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.)
- Import the project into Eclipse. See instructions.
- If your project has errors about the M2_REPO classpath variable being missing.
- Add M2_REPO as a classpath variable in Eclipse and point it to your Maven repository folder on your computer.
- Instructions for finding the Maven repository folder on your computer.
- Instructions for adding the M2_REPO classpath variable in Eclipse.
- Your project should have no errors at this point.
- Add a new class to your project, with a static main method.
- Run your application to see if it will even run.
- If you get the error: "Editor does not contain a main type", then:
- You need to tell Eclipse where the Source is for your project. (For instructions, refer to Dave McQueen's response on CodeRanch on May 22, 2009.)
- Your empty application should at least run at this point.
- In the main() method, write your test code. Refer to this example Selenium WebDriver code.
- 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
- If you are using the Internet Explorer driver in your code, then do the following before running or you will get an error:
- Make sure to download and unzip The Internet Explorer Driver Server on your machine.
- Add the path to it in your system's environment path variable.
- Restart Eclipse.
- If you are using the Chrome driver in your code, then do the following before running or you will get an error:
- Download and unzip the Chrome driver.
- Add the path to it in your system's environment path variable.
- Restart Eclipse.
- Run your application.
- 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.
Comments