keepcalculator

06 Numbers and Powers

Selenium with IntelliJ IDEA: Java & Maven Setup

Card No. 06·12034 · Updated · Standard formula, shown below

This guide walks you through setting up Selenium WebDriver with Java and Maven inside IntelliJ IDEA, so you can write and run browser automation tests. You’ll create a Maven project, add the Selenium dependency, write a simple test that opens a browser, and run it from the IDE. No prior Maven or Selenium experience is needed, but basic Java and IntelliJ familiarity helps.

What You’ll Need

  • IntelliJ IDEA (Community or Ultimate edition, version 2021.1 or newer)
  • JDK 8 or later (JDK 11 or 17 recommended)
  • Maven (bundled with IntelliJ, no separate install needed)
  • A browser like Chrome or Firefox (Selenium will drive it)
  • Internet connection to download dependencies

Step-by-Step

  1. Install IntelliJ IDEA – Download from JetBrains website and install. The Community edition is free and sufficient for this guide.
  2. Install JDK – If you don’t have one, download from Oracle or use a package manager. IntelliJ can also download a JDK for you.
  3. Create a new Maven project – Open IntelliJ, click New Project. In the left panel choose Maven. Set the Project SDK (choose your JDK). Name the project (e.g., selenium-demo) and set a GroupId (e.g., com.example) and ArtifactId. Click Finish.
  4. Wait for Maven to initialize – IntelliJ will create a pom.xml file. It may take a moment to download Maven wrapper files.
  5. Add Selenium dependency – Open pom.xml. Inside the <dependencies> section (if it doesn’t exist, create it), add the following:
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.21.0</version>
    </dependency>

    The version number may be different; check the latest stable on Maven Central. Save the file.

  6. Let Maven download dependencies – IntelliJ will automatically resolve dependencies. You’ll see a progress bar in the bottom right. If not, click the Load Maven Changes icon (circular arrows) in the Maven tool window.
  7. Create a Java class – In the src/main/java or src/test/java directory, right-click and select New > Java Class. Name it FirstSeleniumTest.
  8. Write a simple test – Replace the class with:
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class FirstSeleniumTest {
        public static void main(String[] args) {
            WebDriver driver = new ChromeDriver();
            driver.get("https://www.google.com");
            System.out.println("Title: " + driver.getTitle());
            driver.quit();
        }
    }

    This opens Chrome, navigates to Google, prints the page title, and closes the browser.

  9. Run the test – Click the green arrow next to the main method and select Run. If Chrome is not installed, you’ll get an error – install it or change to FirefoxDriver (requires GeckoDriver).
  10. Handle driver binaries – Selenium 4 includes Selenium Manager, which automatically downloads the correct driver for Chrome, Firefox, Edge, or Safari. If you’re using an older Selenium version, you’ll need to download ChromeDriver manually and set the system property.
  11. Create a proper test with JUnit (optional) – Add JUnit dependency to pom.xml:
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.10.2</version>
        <scope>test</scope>
    </dependency>

    Then create a test class in src/test/java with @Test annotations.

  12. Use Maven to run tests – Open the Maven tool window (right side), expand Lifecycle, and double-click test. This runs all tests in src/test.

Common Mistakes

  • Wrong Selenium version – Using an old version that doesn’t work with your browser. Always use the latest stable.
  • Missing driver executable – With Selenium 3 and earlier, you must download ChromeDriver and set System.setProperty("webdriver.chrome.driver", "path"). Selenium 4 handles this automatically.
  • Browser not installed – Selenium cannot launch a browser that isn’t installed. Ensure Chrome or Firefox is present.
  • Wrong Maven coordinates – Typos in groupId or artifactId lead to dependency resolution errors.
  • Not refreshing Maven – After editing pom.xml, you must load changes or the dependencies won’t be available.
  • Using JUnit 4 with JUnit 5 imports – Mixing versions causes confusion. Stick to one.

Tips

  • Use WebDriverManager (from io.github.bonigarcia) to automatically manage driver binaries, even with Selenium 3. It’s a popular library that simplifies setup.
  • Always call driver.quit() in a finally block or use try-with-resources to avoid browser leaks.
  • Add a .gitignore file to exclude target/ and IDE-specific files.
  • Use @BeforeEach and @AfterEach in JUnit to set up and tear down the driver for each test.
  • Keep your browser updated – Selenium Manager works best with recent versions.
  • If you’re behind a corporate proxy, configure Maven settings.xml to use it.

Troubleshooting

  • Dependencies not downloading – Check internet connection. In IntelliJ, go to File > Settings > Build, Execution, Deployment > Build Tools > Maven and ensure Always update snapshots is checked. Also try Maven > Reload Project.
  • Error: Cannot resolve symbol 'WebDriver' – Right-click project, select Maven > Reimport. If that fails, check that the dependency is in pom.xml and the version is correct.
  • Browser launches but test fails – The page may have loaded slowly. Add an implicit wait: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS).
  • ChromeDriver version mismatch – Selenium Manager should handle it, but if you see an error about version mismatch, download the correct ChromeDriver manually and set the system property.
  • Maven build fails with No tests to run – Make sure your test class is in src/test/java and its name ends with Test (e.g., MyTest.java).
  • IntelliJ doesn’t show Maven tool window – Go to View > Tool Windows > Maven.

Frequently Asked Questions

Do I need to install Maven separately?

No. IntelliJ IDEA bundles Maven and uses it automatically when you create a Maven project.

Why do I get a SessionNotCreatedException?

This usually means the browser version is incompatible with the driver. Update your browser and ensure Selenium Manager downloads the matching driver.

Can I use Firefox instead of Chrome?

Yes. Change ChromeDriver to FirefoxDriver and ensure Firefox is installed. Selenium Manager will handle the GeckoDriver.

How do I write a test that actually asserts something?

Use JUnit assertions, e.g., assertEquals("Google", driver.getTitle()). Add JUnit dependency as shown in step 11.

What is the difference between quit() and close()?

close() closes the current browser window; quit() closes all windows and ends the driver session. Use quit() to free resources.

Can I run Selenium tests in headless mode?

Yes. For Chrome, use ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); and pass it to ChromeDriver(options).

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →