Webdriver is an API that enables testers to find and use web elements in their automated tests. To get set up, you will need to find out what the latest version of Webdriver is. Navigate to this link: http://docs.seleniumhq.org/download and in the section called Selenium Client and Webdriver Language Bindings, find the Java entry and make a note of the latest client version.
Next, you will need to add this text to your project’s POM file:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.39.0</version> //replace the version number here with the most current version number
</dependency>
Add the text after the JUnit dependency, so that the dependencies section now looks like this:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.39.0</version>
</dependency>
</dependencies>
In the command line, navigate to your project folder. Then type:
clean install
This will download all of the dependencies needed to run Webdriver in your project.
Let’s set up a new test using Webdriver:
1. Right-click on the myFirstTests package and select New->Class
2. Give the class a name of secondTest
3. The SecondTest class should open in the center pane, and should look like this:
package myFirstTests;
public class SecondTest {
}
4. In between the brackets, add the following:
@Test
public void test() {
}
5. Add the following to the import section:
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
6. In between the brackets following test(), add this code:
driver = (WebDriver) new FirefoxDriver();
String URL = “http://www.google.com”;
driver.get(URL);
driver.quit();
7. Right-click on the test name secondTest and choose Run As-> JUnit Test. You should see a Firefox browser open, navigate to Google, and close again, and on the JUnit tab of Eclipse, you should see that the test has passed.
Congratulations! You are now all set up to begin writing your own automated Webdriver tests! In my next post, I will discuss the Webdriver syntax.