Configuring the Selenium WebDriver test development environment for Java with Eclipse and Maven
Selenium WebDriver is a simple API that can help with browser automation. However, much more is needed when using it for testing and building a test framework. You will need an Integrated Development Environment
(IDE
) or a code editor to create a new Java project and add Selenium WebDriver and other dependencies in order to build a testing framework.
In the Java world, Eclipse is a widely used IDE, as well as IntelliJ IDEA and NetBeans. Eclipse provides a feature-rich environment for Selenium WebDriver test development.
Along with Eclipse, Apache Maven provides support for managing the entire life cycle of a test project. Maven is used to define project structure, dependencies, build, and test management.
You can use Eclipse and Maven to build your Selenium WebDriver test framework from a single window. Another important benefit of using Maven is that you can get all the Selenium library files and their dependencies by configuring the pom.xml
file. Maven automatically downloads the necessary files from the repository while building the project.
This recipe will explain how to configure Eclipse and Maven for the Selenium WebDriver test development. Most of the code in this book has been developed in Eclipse and Maven.
Getting ready
You will need Eclipse and Maven to set up the test development environment. Download and set up Maven from http://maven.apache.org/download.html. Follow the instructions on the Maven download page (see the Installation Instructions section on the page).
Download and set up Eclipse IDE for Java Developers from https://eclipse.org/downloads/.
Note
The examples for this book are built in Eclipse version 4.4.2 (codenamed Luna) for Java Developers. This comes with the Maven plugin bundled with other packages.
How to do it...
Let's configure Eclipse with Maven to develop Selenium WebDriver tests using the following steps:
- Launch the Eclipse IDE.
- Create a new project by selecting File | New | Other from the Eclipse Main Menu.
- On the New dialog, select Maven | Maven Project, as shown in the following screenshot, and click Next:
- Next, the New Maven Project dialog will be displayed. Select the Create a simple project (skip archetype selection) checkbox and click on the Next button, as shown in the following screenshot:
- On the New Maven Project dialog box, enter
com.secookbook.examples
in the Group Id: textbox andSeleniumCookbook
in the Artifact Id:textbox
. You can also add a name and description optionally. Click on the Finish button, as shown in the following screenshot: - Eclipse will create the SeleniumCookbook project with a structure (in Package Explorer) similar to the one shown in the following screenshot:
- Select pom.xml from Package Explorer. This will open the
pom.xml
file in the editor area with the Overview tab open. Select the pom.xml tab next to the Overview tab, as shown in the following screenshot: - Add the WebDriver and JUnit dependencies highlighted in the following code snippet to
pom.xml
in the<project>
node:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.secookbook.examples</groupId> <artifactId>SeleniumCookbook</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.47.1</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
You can get the latest dependency information for Selenium WebDriver and JUnit from http://seleniumhq.org/download/maven.html and http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html respectively.
Tip
TestNG is another widely used unit-testing framework in Java World. If you want to add TestNG support to the project instead of JUnit, you can get its Maven entry at http://testng.org/doc/maven.html.
- Select src/test/java in Package Explorer and right-click to show the menu. Select New | Class, as shown in the following screenshot:
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
- Enter
com.seleniumcookbook.examples.chapter01
in the Package: textbox andGoogleSearchTest
in the Name: textbox and click on the Finish button, as shown in the following screenshot:This will create the
GoogleSearchTest.java
class in thecom.secookbook.examples.chapter01
package. - Add the following code in the
GoogleSearchTest
class:package com.secookbook.examples.chapter01; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import org.junit.*; import static org.junit.Assert.*; public class GoogleSearchTest { private WebDriver driver; @Before public void setUp() { // Launch a new Firefox instance driver = new FirefoxDriver(); // Maximize the browser window driver.manage().window().maximize(); // Navigate to Google driver.get("http://www.google.com"); } @Test public void testGoogleSearch() { // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Clear the existing text value element.clear(); // Enter something to search for element.sendKeys("Selenium testing tools cookbook"); // Now submit the form element.submit(); // Google's search is rendered dynamically with JavaScript. // wait for the page to load, timeout after 10 seconds new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase() .startsWith("selenium testing tools cookbook"); } }); assertEquals("Selenium testing tools cookbook - Google Search", driver.getTitle()); } @After public void tearDown() throws Exception { // Close the browser driver.quit(); } }
Tip
Downloading the example code:
You can download the example code files for all the Packt books you have purchased from your account at http://www.packtpub.com. If you have purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The example code is also hosted at https://github.com/upgundecha/secookbook.
- To run the tests in the Maven life cycle, select the SeleniumCookbook project in Package Explorer. Right-click on the project name and select Run As | Maven test. Maven will execute all the tests from the project.
How it works...
Eclipse provides the ability to create Selenium WebDriver test projects easily with its Maven plugin, taking away the pain of project configurations, directory structure, dependency management, and so on. It also provides a powerful code editor to write the test code.
When you set up a project using Maven in Eclipse, it creates the pom.xml
file, which defines the configuration of the project and its structure. This file also contains the dependencies needed for building, testing, and running the code. For example, the following code shows the dependency information about Selenium WebDriver that we added in pom.xml
:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.47.1</version> </dependency>
Most open source projects publish this information on their websites. In this case, you can check http://seleniumhq.org/download/maven.html; you can also get this information from Maven Central at http://search.maven.org/#browse. Maven will automatically download libraries and support files mentioned for all the dependencies and add to the project without you needing to find, download, and install these files to the project. This saves a lot of our time and effort while managing the dependency-related tasks.
Maven also generates a standard directory structure for your code, for easier management and maintenance. In the previous example, it created the src/test/java
folder for the test code and the src/test/resources
folder to maintain resources needed for testing, such as test data files, utilities, and so on.
Maven provides life cycle steps such as building the test code and running the test. If you are working with the Java development team, then you might find the application code and test code together in Maven. Here, Maven supports building the application code, then firing the tests, and releasing the software to production.
There's more…
Maven can also be used to execute the test from the command line. To run tests from the command line, navigate to the SeleniumCookbook
project folder through the command line and type the following command:
mvn clean test
This command will traverse through all the subdirectories and run the clean command to delete/remove earlier build files. It will then build the project and run the tests. You will see the results at the end of execution on command line, as shown in the following screenshot: