Features

Guide to use Selenium with IntellIJ IDEA

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

IntelliJ IDEA is a popular IDE (Integrated Development Environment) written in Java and very popular amongst developers coding in Java or other JVM languages such as Kotlin, Scala, and Groovy.

The aim of this article is to provide an overview on how to use IntelliJ together with WebDriver automated testing.
We'll show you how to install IntelliJ, configure it with the correct (Selenium) dependencies and how to run your very first test, straight from inside the IDE.

How can I set up and configure IntelliJ IDEA for Selenium Testing?

To install IntelliJ, please go to the JetBrains website and pick an edition that you are comfortable with.
At the time of writing, you can choose between the Community Edition (open source) or Ultimate Edition (free trial).
The installation process is straightforward on either Windows, macOS or Linux. Once installed, the IDE will install a JDK to be able to build and run Java based (web-)apps.

If you are more comfortable with installing a JDK or JRE yourself, you can so through various download sites, for example Oracle's or OpenJDK.

Once installed, you can create a new project and accept all the default settings provided by the IDE.

How can I start a new IntelliJ project with Selenium?

Once you've installed IntelliJ IDEA (Community or Ultimate), you can create a new project.
Simply configure it as a Java project and accept all the default settings. When done, you should see a file opened up with a simple Java class.

Next, we can configure IntelliJ to add Maven as a dependency manager. Right click on the project name and click Add Framework Support, choosing Maven as your dependency manager.
The IDE should open a XML filled (called pom.xml) which should contain a section called dependencies.
We will need to add a new dependency with groupId: org.seleniumhq.selenium, artifactId: selenium-java and version: 3.141.59 (or whichever version you prefer).

Please see the example below, this is what your pom.xml should (partially) look like:

Copy code
<dependencies>
   <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>3.141.59</version>
   </dependency>
 </dependencies>

Using Chromedriver, Safaridriver, OperaDriver, Geckodriver with IntelliJ

Depending on which browser(s) you want to test on, you'll need to install or configure various drivers associated with each browser.
Below is a list of the most popular browsers, each with their respective WebDriver dependency.

  • Internet Explorer: IEDriver
  • Microsoft Edge: MicrosoftEdgeDriver
  • Google Chrome: chromedriver
  • Firefox: geckodriver
  • Safari: safaridriver
  • Opera: operadriver

Each of these (third-party) drivers can be downloaded from the browser vendor's website, with the exception of IEDriver which is hosted by the Selenium project.

To use for example GeckoDriver in your Java project, you can set up an environment variable such as webdriver.gecko.driver:

Copy code
System.setProperty("webdriver.gecko.driver", "c:\\path-to\\geckodriver.exe");

How do I use JUnit with IntelliJ and Selenium WebDriver?

While there are various other, popular, test frameworks available for Java, we'll focus on JUnit because is offers various advantages:

  • JUnit is easy to use
  • JUnit is included by default with IntelliJ

Create a new JUnit test by using IntelliJ: simply press option + enter or control + enter to open the "intention action" menu. From there you can create a test. A dialog will open where you can select JUnit.

We can now create a basic JUnit test in the IDE. The example below shows a simple example on how to do this. This does not follow best practices such as the PageObject pattern:

Copy code
import static org.junit.Assert.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.Test;
public class CheckTestingBotTest {
    @Test
    public void site_header_is_on_home_page() {
        WebDriver browser;
        //Firefox's geckodriver *requires* you to specify its location.

        System.setProperty("webdriver.gecko.driver", "c:\\my-dir\\geckodriver.exe");
        browser = new FirefoxDriver();
        browser.get("https://testingbot.com");
        WebElement header = browser.findElement(By.id("header"));
        assertTrue((header.isDisplayed()));
        browser.close();
    }
}

This simple example will use the Geckodriver on your computer to start a Firefox browser, navigate to TestingBot and check if the header of the page is displayed.
The test will pass or fail and then close the Firefox browser.

To run the test, you can use the right-click context menu from the test method code signature, or use Ctrl-Shift-R to run the test.

How can I run JUnit Selenium tests in the Cloud?

TestingBot provides a cloud based Selenium grid with over 3000 browser combinations.
To run your JUnit test with IntelliJ and Selenium on a remote browser grid, all you need to do is use a class called RemoteWebDriver.

With this RemoteWebDriver class, you can instruct a remote grid of browsers to allocate a browser, according to the capabilities you desire (DesiredCapabilities).
The test will send commands back and forth to the remote browser, instructing the browser as if it was running on your own machine.

The example of using a cloud based Selenium grid is that you can run multiple tests simultaneously, reducing your total test duration.
Another advantage is that you do not need to set up, configure and maintain virtual machines with various browsers, versions and operating systems.

To connect to the TestingBot browser grid from IntelliJ IDEA, please see the simple example below.

Copy code
import junit.framework.TestCase;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class SimpleTest extends TestCase {
	private WebDriver driver;

	@Before
	public void setUp() throws Exception {
		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
		capabilities.setCapability("version", "latest");
		capabilities.setCapability("platform", Platform.WINDOWS);
		capabilities.setCapability("name", "Testing Selenium");

		this.driver = new RemoteWebDriver(
		   new URL("http://key:secret@hub.testingbot.com/wd/hub"),
		   capabilities);
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void testSimple() throws Exception {
		this.driver.get("http://www.google.com");
		assertEquals("Google", this.driver.getTitle());
	}

	@After
	public void tearDown() throws Exception {
		this.driver.quit();
	}
}

We've covered the basics in setting up, configuring and running JUnit tests with IntelliJ IDEA. For more information, please head over to our JUnit documentation.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews
TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

Internationalization testing of websites and mobile apps

Learn more about Internationalization Testing: how to prepare and test your website or mobile app for different regions in the world.

Read more
Visual Testing with Playwright

Playwright provides automated browser testing. It offers a built-in feature to perform visual regression testing for your website.

Read more
Testing Flutter Apps with Appium

Learn how to do automated mobile app testing with Flutter and Appium.

Read more
How to do localisation testing for mobile apps

Find out how to perform localisation testing with mobile apps. Change your language or locale with Appium, XCUITest and Espresso.

Read more
Automated Testing with Puppeteer

Puppeteer combined with a test framework provides a great way to run automated browser tests. Follow this guide for more information.

Read more
Tutorial on debugging Cypress tests

This article will focus on how to debug your Cypress tests with Cypress debugger and other developer tools.

Read more