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.
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.
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 and SeleniumCookbook
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: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 and GoogleSearchTest
in the Name: textbox and click on the Finish button, as shown in the following screenshot:This will create the GoogleSearchTest.java
class in the com.secookbook.examples.chapter01
package.
- Add the following code in the
GoogleSearchTest
class: - 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.
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
:
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.
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:
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: