Setting Up a Selenium WebDriver Java Project
By: Ranorex Webtestit
Tags: Selenium, Webtestit, Test Automation, Java, Selenium Webdriver

Setting Up a Selenium WebDriver Java Project

Selenium WebDriver is one of the most popular automation tools when it comes to testing web applications. In this article, we explain how to quickly set up a Selenium WebDriver Java project. Ranorex Webtestit Follow May 27, 2019 · 7 min read

In order to start writing your first automated tests in one of the supported programming languages, you need to set up your testing environment. This means, choosing your preferred IDE, getting all the dependencies needed for your project, and finally writing your tests.

Setting up your project can indeed be troublesome, as you need to make sure that your project has all the required files ( in Java projects, all the JAR’s, libraries, etc.), and, of course, the right version of Selenium WebDriver and Selenium Grid if you want to run your tests remotely. Besides that, your project needs to be easy for your co-workers to understand and it must be maintainable.

How to set up a Selenium WebDriver project in Java is a question commonly asked on popular developers and QA forums. In this article, we will describe the basic steps needed in order to start working on your first project.

Get your preferred IDE

As a tester, you will have to code your tests, so any help like code autocomplete, quick fixes and tips are most welcome. Eclipse, IntelliJ and NetBeans are some of the most popular IDEs used for test automation. Flexibility, plugin and deployment support, code highlighting, and code autocomplete are the features that you can benefit from.

Create a Java project and download the required JAR files & WebDrivers

Let’s assume that you want to use Eclipse IDE and make a simple test using Google Chrome as a browser. So go ahead and create a new Java project, add a new package and a new class inside of it. Your project should look something like this:

Now you need to download the Jar files from Selenium’s official web page. Select Java under the supported language list and unzip the downloaded archive.

Setting up your project path

The next step is to add the downloaded Jar files to your project path. Right click on Project ‘SeleniumJavaProject‘ and select ‘Properties’ > ‘Java Build Path’. Move to ‘Libraries’ tab and click on ‘Add External JARs…’ and add all the JAR files that you have previously downloaded. Your properties dialog should now look similar to the image below.

Optional: Download Apache POI and Log4j JAR files

As an automated tester, one of your tasks can be extracting data from sources like Microsoft Excel, CSV or other file types that are used in data-driven testing. Apache POI is a favorite Java API of automation testers as it helps them read, write and create Microsoft Excel files. It also supports other file formats like DOC, DOCX, and PPT. You will have to import Apache POI to complete this task. Here you can find out more about data-driven testing with Apache POI.

Logging is considered to be one of the best practices if used in the accurate proportion in your test automation framework. Log4j creates helpful logs after test execution and helps you with debugging while providing valuable information and warnings. Here you can find out more about how to set up Log4j logger in your project.

Downloading the WebDriver

Selenium WebDriver is used to automate web application testing to verify that it works as expected. It supports many browsers such as Firefox, Chrome, Opera, Safari, etc. In order to execute your test on Google Chrome browser, you need to download the chrome driver and set it up in your project. If you want to execute your test on other browsers you will, of course, need the corresponding drivers. Download the chrome driver from here. Make sure to download the driver version that works with the version of your Google Chrome browser. Place the driver in a folder on your hard drive so that it is easy to locate.

Start your first test

Ok, you are almost ready to perform your first test! In order to do so, you will need to set up the above-downloaded chrome driver (if you want to run your test on chrome) and call it in the test. Also, you need to import the required libraries needed to run your test.

In the test itself, you need to provide the location of the chrome driver using the System.setProperty( method as presented in the example below:

All right, now you have set up your project, downloaded and imported the selenium JAR files, and set up the chrome driver and you can start writing your first test. The next step consists of instantiating the web driver instance and providing the desired URL of the site under test. This is the starting point of most of the automated test cases. After instantiating the driver and providing the URL your test should look something like this:

There is one last thing (hopefully) to take care off — the imports. If you would try to run the test as it is right now, you would obviously get an error due to the missing imports (WebDriver and ChromeDriver). So you need to import them either by using the quick-fix or importing them manually:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

Running your test and keeping it maintainable

Now you should be ready to execute your test. You have all the freedom when it comes to creating your test scenarios and test cases. At this point, we would like to draw your attention to the Page Object Pattern. If you want your test framework to be maintainable, understandable and DRY, we strongly recommend following this pattern. You can find out more about the Page Object Pattern and all its benefits here.

When creating a new project, just follow the same procedure. Create a Java project, keep your project readable, import required JAR files, and download the latest WebDriver. More projects require more time effort to maintain them, so keep it simple and follow the automated testing best practices, like using the Page Object Pattern. It may seem like a lot of work setting up a project, and it is, but you will get used to it, eventually. But, the moment when the WebDrivers or some of the JAR files need to be updated, that is where the real struggle begins. Recently chrome driver creators changed the policy of supporting the last three versions of Google Chrome browser with the latest chrome driver, so keep in mind that now you need a matching version of chrome driver and chrome browser!

Complete all these steps with a single click!

Now imagine that you can do all of these steps in just one click! One click and you have created and scaffolded the project, downloaded the latest WebDriver and all the dependencies, imported all the required libraries and you are ready to go. With Ranorex Webtestit all of this is possible, you will get an IDE made just how testers like it — flexible, maintainable, easy to use and available on all platforms, with a support forum where you can find answers to all of your questions and super useful how-to videos of test automation workflows.

Together with Ranorex Selocity Chrome extension, you can automate every web application with ease, saving your valuable time that you would spend on setting up multiple projects in a “traditional” way and keeping them updated. Check out the video below on creating a simple Java project and creating a simple test following the Page Object Pattern in just under two minutes and a few clicks.

Conclusion

Setting up a fresh Selenium Java project is an everyday task of an automation test engineer that requires a significant amount of time and effort. Keeping the project maintainable is another challenge that you need to overcome in order to have a functional automated test framework. Using Ranorex Webtestit, you can avoid spending your time on setting up new projects, downloading the drivers and other dependencies. Set up your project with one click, generate reliable selectors for web elements, send them to Ranorex Webtestit and run your test on different browsers. Simple as that! You can download Ranorex Webtestit and try out the free one month trial here. We are looking forward to hearing from you.