In Part 2 (here) of this guide, we went through downloading the Intellij IDE and installing Selenium and Junit through Maven. This is all we need to get started with writing automated tests, so let’s go ahead and jump straight in!

Before we go on, I am going to assume that you have the Firefox browser installed. If you don’t, go ahead and download it (here). Firefox is a great browser to use initially with Selenium, as it does not require any further configuration. We will cover using Selenium with Chrome and other browsers in later posts, but for now let’s stick with Firefox.

In Intellij, on the left-hand side is the ‘Project’ window. Go ahead and expand the folders until it looks similar to the screenshot below:
When you are writing automated tests, you want all of the tests to be under the ‘test’ folder. Once you get more advanced with Selenium, we will add code under the ‘main’ folder in the form of Page Objects, but there is no need to worry about that at this early stage. Right-click on the ‘java’ folder under ‘test’ and choose ‘new > Java Class’. Give the class a name such as “MyFirstTest” and click OK to create the class:
We now have an empty class with no code in it. Let’s dive in at the deep-end and write a Selenium test. The test will launch a Firefox browser, go to a site called, check that the Title of the page is as expected, and finally close the browser.

Every test that we write needs to have its own method. Inside the “MyFirstTest” class, go ahead and type the following:

public class MyFirstTest {

    public void goToWebPage()


You might be confused as to exactly what @Test does, and it might even have become highlighted red and be showing an error for you, such as in the screenshot below:


The @Test is showing known as a Junit annotation, which simply marks all the code in the goToWebPage() method as a unit test to be executed. The reason that it is in red is because we have not actually imported Junit into the class yet. Luckily Intellij makes it really easy to identify and import these dependencies. Simply click anywhere on the @Test, press ‘ALT + Enter’ and choose ‘Import Class’ from the popup menu. The nasty error should have gone away now.

Great, so we have an empty test, but its not actually doing anything. Let’s change that. Type in the code below, and we will talk through what each line does. As you are typing the code in, Intellij should prompt you to import the required classes with small popups. If you miss any of the popups for some reason, just press ‘Alt + Enter’ on the error message to import the required class. Your final code should look like this:

public class MyFirstTest {

    public void goToWebPage()
        FirefoxDriver driver = new FirefoxDriver();


        assertTrue(driver.getTitle().equals("The Internet"));


The first line is creating a new instance of the Firefox driver, but us to use in our test.

The next line will navigate to the website for us. We start by calling the ‘driver’ that we just created, then pressing ‘.’ and typing ‘get()’.

TIP: as you are typing the first few letters, you should see that Intellij gives you options on all the methods available. You can simply type ‘.’ then press ‘CTRL + SPACE’ to see all the options available. This feature of Intellij is very useful, especially later on when you are not sure what method you need to call.

We next type the URL of the website into the .get() method, so we have ‘.get(“’);’. This will load the webpage in the browser.

When you type in the line with ‘assertTrue’, you will likely see that it stays red and Intellij does not initially give you an option to fix it. To resolve this, we need to do what is a called a ‘Static Import’. This is similar to importing a class (like we did for @Test), but we are just importing the functionality for the ‘assertTrue’ method. Click anywhere on the red ‘assertTrue’ and press ‘ALT + Enter’, choose ‘Static import class’. You will probably be presented with a few different options. Choose the option highlighted in the screenshot:

Intellij4 Intellij5

What the code on the ‘assertTrue’ line is doing, as you can probably guess, is checking that the title of the page is ‘The Internet’. We simply type ‘driver’ and then call the ‘.getTitle()’ methods and then check that is correct by typing ‘(.equals(“The Internet”));’.

The final line ‘driver.quit()’ simply closes the browser for us at the end of the test. It is important to always add this line at the end of a test, otherwise you will be left with many open browsers!

Congratulations, you just wrote your first Selenium automated test! I can’t lie, when I first got to this point (as simple as it seems now) I was really pleased with myself and felt that I was finally starting to understand just a little bit about how this whole web automation thing works. Let’s go ahead and run the test. Right-click on the ‘goToWebPage()’ method and choose run (as in the screenshot below):


After a few seconds, Firefox should load and you should see the website load. Selenium will check that the title of the website is correct (‘The Internet’) and then close the browser. You should see a nice green message at the bottom of the IDE, which is Junit telling you that the test passed:


What about when the test fails though? Let’s try it out! Inside the ‘assertTrue’ change the text to the following and run the test again:

assertTrue(driver.getTitle().equals("NOT THE INTERNET"));

This time, the test fails (as expected, because the title of the page is not the same) and we get an error message as below:


Notice how the browser did not close! This is because the test stopped at the ‘assertTrue’ line and so did not execute the final ‘driver.quit()’ line. This is not the ideal behaviour, but how can we solve it?

In the next post I will dive in just a tiny bit deeper to Junit, and show you a simple solution to the problem we encountered above with the browser window staying open on a failed test.

PS – If this is your first time using Intellij, you might be wondering why your screen is all white and mine is mostly dark. If you want to change the theme, simply go to ‘File > Settings > Appearance & Behaviour > Appearance’ and select a different ‘Theme’ from the dropdown. Enjoy!


  • […] the last post (here) we ran our first Selenium automated test, and we felt good about it! However, straight away we […]

  • ken says:

    for assertTrue, it does not give me an option of Static import method. I see following options:
    Create local variable or field or parameter. Or, rename field.

    Cannot solve this issue somehow..

  • James Willett says:

    at the top of the class, try typing “import static org.junit.Assert.assertTrue;”

  • ken says:

    Update: I was able to fix the issue by simply pressing ctrl+space(twice). Also, firefox latest version 40.0 has trouble with Selenium. I switched to firefox 30 and it worked like a charm.

  • sue says:

    This is perfect for me to learn about Maven and Intellij – for some odd reason i had been learning to write test cases on eclipse and all of a sudden it failed with connection issues. It seems with Firefox 48 which is what I have – and since I am a test person where I work, we have to run with the latest browser versions. So here, I create the JUNIT test case as what is indicated – I went and made the start page on Firefox blank – because that kept coming up and it is no connection and it closes. so Now I am getting the following error:

    org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files (x86)\Mozilla Firefox\firefox.exe) on port 7055; process output follows:
    Add-on”,”description”:”Initiate Jabber Calls from web page”,”creator”:”Cisco Systems, Inc.”,”homepageURL”:null},”visible”:true,”active”:false,”userDisabled”:true,”appDisabled”:false,”descriptor”:”C:\\Program Files (x86)\\Cisco Systems\\Cisco Jabber\\Plugins\\Click2XPlugin\\jabber_calls_add_on-11.5.0002-fx-windows.xpi”,”installDate”:1464490912000,”updateDate”:1464490912000,”applyBackgroundUpdates”:1,”bootstrap”:true,”skinnable”:false,”size”:8519,”sourceURI”:null,”releaseNotesURI”:null,”softDisabled”:false,”foreignInstall”:true,”hasBinaryComponents”:false,”strictCompatibility”:false,”locales”:[],”targetApplications”:[{“id”:”{ec8030f7-c20a-464f-9b0e-13a3a9e97384}”,”minVersion”:”38.0a1″,”maxVersion”:”43.0″}],”targetPlatforms”:[],”multiprocessCompatible”:false,”signedState”:2,”seen”:true}
    1470687646279 DeferredSave.extensions.json DEBUG Save changes
    1470687646279 addons.xpi DEBUG Updating database with changes to installed add-ons
    1470687646279 addons.xpi-utils DEBUG Updating add-on states
    1470687646283 addons.xpi-utils DEBUG Writing add-ons list

    • Carlos says:

      Hi Sue,
      But does your test actually manages to open a firefox browser? or just throws you that error?
      Mine was opening a Firefox browser but not doing anything, the solution in the end was to downgrade to FF 47 so i would suggest you do the same and try if that solves it. I searched quite a bit for this error and I saw in multiple sites similar situations and the solution for most was that, downgrade to a previous version of FF or selenium. With new versions always come incompatibilities issues.
      Let us know if that worked 🙂

  • Drew says:

    LOVE the tutorial however when I attempt to run the test, the browser opens, but does not go to intended site. after 90 seconds the test fails with this:
    org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host on port 7055 after 45000 ms

  • Pelo says:


    I am new in Java automation and tried doing this tutorial and now I am stuck. I get the error below when I import firefoxDriver

    Error:(2, 35) java: package org.openqa.selenium.firefox does not exis

    • Gabriel says:

      I tried this today and it worked for me.

      Can you show us your code?

      It seems, at first glance, that you aren’t importing your driver.

      “import org.openqa.selenium.firefox.FirefoxDriver;”

  • MnemonicCarrier says:

    Mind completely blown – these tutorials are AWESOME!

    Just a quick note for anyone wanting to use the latest (at time of writing) Selenium 3.0.1 and JUnit 4.12:

    In your pom.xml:



    And, in your file, change the FirefoxDriver driver = new FirefoxDriver() line like this:

    public void goToWebPage()
    //FirefoxDriver driver = new FirefoxDriver();
    WebDriver driver;
    System.setProperty(“webdriver.gecko.driver”, “”);
    driver = new FirefoxDriver();
    assertTrue(driver.getTitle().equals(“The Internet”));

    You can download the geckodriver from here:

    Just remember to change your to wherever you’ve saved and extracted your geckodriver. For example, if you saved and extracted it to /home/testing/, then you would use:

    System.setProperty(“webdriver.gecko.driver”, “/home/testing/geckodriver”);

    Is there a way to donate to these tutorials? I’d like to chip in a little something, as these are the kind of tutorials that careers are built from.

    • James Willett says:

      Thanks MnemonicCarrier 🙂

    • Irving Ortiz says:

      Hi, great contribution,
      I have a couple doubts…

      is not supposed that geckodriver is included on maven dependency ?

      so we dont need to downloaded geckodriver but what is the path?

      I’ll appreciate the answers, regards !!

  • Jason D says:

    Agreed. This was awesome!

    I had to update the POM.xml to 3.3.1 but it works like MnemonicCarrier says.

    And I don’t see how to add geckodriver.exe to maven either, but this might be an idea:

Leave a Reply