Back to Resources

Blog

Posted May 4, 2017

Getting Started with WebDriver in Java Using IntelliJ on OSx

In this article we’ll show you how to run WebDriver in Java using IntelliJ on OSx.

quote

IntelliJ is a popular, widely used Java Integrated Development Environment (IDE) made by JetBrains. It's available for OSx and Windows. In this article, we'll work on the OSx platform and show you how to get the various parts and pieces, plus write and run one simple test in Java.

This article is one in a series showing how to get WebDriver working in various editors and language platforms.

For an overview of how WebDriver works, please see the section “WebDriver Overview” in the article “Getting Started with Webdriver/Selenium for Java in Eclipse”.

The Components You'll Need

To create and run WebDriver tests in Java using IntelliJ you’ll need the following components:

  • Java

  • IntelliJ

  • A test framework (We’ll use JUnit; there are many you can use)¥ WebDriver’s Java bindings

  • WebDriver’s Java Standalone Server library

  • Mozilla’s Gekodriver proxy for Firefox

Getting IntelliJ

IntelliJ IDEA comes in two releases: Ultimate, a commercial product that supports a wide range of development scenarios, and Community, a free version for Java Virtual Machine and Android development. Both releases are available on the Mac, Linux, and Windows platforms. All variants are available for download from IntelliJ’s homepage.

This article will use the Community variant on Mac’s OSx.

Installing IntelliJ is a matter of following instructions for the version you’ve downloaded. Starting IntelliJ the first time will prompt you for various defaults (locations, keyboard mappings, etc.). This article uses all default settings.

Creating The Project

At startup IntelliJ will prompt you for basic actions—you can choose Create New Project right from the opening screen.

For this example choose the simple Java project with none of the additional library options selected and click Next.

In the next screen leave the Create project from template checkbox cleared and click Next

Give the project a good name and select a directory to store your project.

At this point you’re at the IntelliJ project’s home.

Adding WebDriver and JUnit to IntelliJ

There are many ways to include WebDriver in your IntelliJ projects. Most mature teams use a build and dependency management system like Maven or Gradle. This post uses a simple static library inclusion due to the wide variances in how teams organize Maven and Gradle.

Load Libraries Into IntelliJ

Download the Java WebDriver bindings and the standalone server (selenium-server-standalone-3.4.0.jar and selenium-java-3.4.0.zip, e.g.) and save them to a handy spot. I generally include a “libs” folder in my projects for external libraries like this. Depending on your architecture you may need to unzip the zip file.

Use File => Project Structure to get the Project Structure dialog open, then select the Modules menu on the left. Click the Dependencies tab on the right, then use the “+” icon and add in the directory you saved the Selenium files to—”libs” in this example.

Here’s what the dialog looks like when you’re done.

You’re done!

Adding The Firefox Driver Proxy

As noted in the WebDriver Overview in the first post of this series, you’ll need to have a proxy for your test to talk to the actual browser. This example uses Firefox, so you’ll need to grab the appropriate proxy. Proxies for all WebDriver-supported browsers are listed on the SeleniumHQ’s list of Third Party Drivers. Firefox’s driver is part of Mozilla’s Gecko Driver releases. Make sure to grab the driver that’s appropriate for the version of Windows you’re running (x32, x64).

Download the zip file and extract the driver to a location on your system. You’ll need to add that location to your system’s PATH environment variable.

Important Note: You may need to restart your editor in order for changes to the PATH environment to take effect.

Writing Your First Test

WebDriver doesn’t know how to do anything other than talk to the browser driver. As a result, you’ll need some sort of test framework to execute your tests, make assertions, and report test status. We’ll use JUnit because 0) it’s very popular, 1) it’s simple to use, and 2) it’s included in IntelliJ by default! There are many other test frameworks for the Java platform.

Add a new Java file by right-clicking the project, then selecting New => Java Class.

Give the class file a good, clear name and click OK.

Now it’s time to transform that simple class file in to a JUnit test class. As with all IDEs, IntelliJ gives you multiple ways to do the same action. You can use menu actions, or IntelliJ’s “intention action” to both import JUnit libraries to the project and reference them correctly in the class file. Place the cursor on the class name, then use opt-Enter to call up the “intention action” menu. Select the Create Test option.

You’ll likely see a warning stating “No Test Root found.” This relates to the completely flat project structure we’re using for this extremely simple demo. For production work ensure you’re following your team’s proper structure! Dismiss this warning and carry on.

Next you’ll see the Create Test dialog. Select JUnit 5 in the Testing Library drop down. Note there’s a warning that no test library can be found. Use the Fix button to resolve the issue by calling up a sub-dialog. Select the button to use the JUnit library that comes with IntelliJ and click OK twice to close the dialogs.

A Simple Test

Below is a complete test case that starts a browser locally, executes a very simple test, then closes out the browser instance. The example is extremely simple and doesn’t follow normal practices like using Page Object Patterns. This is example code, not production code!

importstaticorg.junit.Assert.*; importorg.openqa.selenium.By; importorg.openqa.selenium.WebDriver; importorg.openqa.selenium.WebElement; importorg.openqa.selenium.firefox.FirefoxDriver; importorg.junit.Test; publicclass CheckSauceLabsHomePage { @Test publicvoid site_header_is_on_home_page(){ WebDriver browser; //Firefox's geckodriver *requires* you to specify its location. System.setProperty("webdriver.gecko.driver","/Users/jimholmes/Utils/geckodriver"); browser =new FirefoxDriver(); browser.get("http://saucelabs.com"); WebElement header = browser.findElement(By.id("site-header")); assertTrue((header.isDisplayed())); browser.close(); } }

Running The Test

Running the test is simple: Use the right-click context menu from the test method’s signature, or use Ctrl-Shift-R to run the test.

You’ll see the browser start, work through the test steps, and finally the green indicator in the Test Results pane.

Wrapping It All Up

In this post you learned a bit about the different versions of IntelliJ, where to find the free Community version, how to create a basic project and add the various WebDriver pieces necessary for Java WebDriver tests, and we showed you an extremely simple test.Good luck with your explorations of WebDriver!

Published:
May 4, 2017
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.