Selenium Tutorial JAVA and JUnit in 7 Easy Steps

Selenium Tutorial Java and JUnit article will help you to learn Selenium Webdriver and its all features. I will explain the Selenium Webdriver step by step for beginners. After this article, you will create your first Selenium Testing project without any pain. Also, you can answer basic Selenium Interview Questions in test automation interviews. Let’s start to Learn Selenium Webdriver Java and JUnit.

Selenium Tutorial Java and JUnit Audience

This selenium tutorial is designed for beginners who have little or no automation experience. At the end of this selenium webdriver tutorial, you will have a web automation environment and you will write your first Selenium automation script.

Selenium Testing Prerequisites

Web Automation Framework: Selenium Webdriver

Development Kit: JAVA JDK

Web Browser: Chrome

Java Test Runner: JUnit

IDE: IntelliJ (I prefer IntelliJ) also you can go with Eclipse

Build System: Maven

Environment Editor (Optional): Rapid Environment Editor (RapidEE)

Nice to have: Before starting this tutorial, it is nice to have basic knowledge of JAVA or any other object-oriented programming languages.

Selenium Automation with Step-By-Step Instructions

Step-1: Installing Chrome

Link: Download Chrome

You can download the latest version of the Chrome web browser above link. When you downloaded the setup file, you can easily install it by pressing “Next” buttons.

Step-2: Installing Ranorex Selocity

Firebug and Firepath died! We can use Chrome and Ranorex Selocity plugin instead of them. You can find all the details for Ranorex Selocity in this article:  Ranorex Selocity Article.

Step-3: Installing Environment Editor

With RapidEE you can easily edit Windows Environment Variables. To get RapidEE, go to Download RapidEE and download suitable version with respect to your windows operating system and install it by pressing “Next” buttons. When your installation finished, you will see below screenshot.

selenium tutorial java

Step-4: Installing JAVA JDK

Go to Java JDK Download Page and select the appropriate version of JDK and click “Accept License Agreement” and download JAVA JDK as shown below.

selenium tutorials

• After downloading JAVA JDK, you can install it by clicking “Next” buttons as shown below:

learn selenium

selenium tutorial for beginners

selenium webdriver tutorial

selenium online

learn selenium webdriver

• Then we should check JAVA with command prompt by typing following command “java –version”. This command will give us JAVA version and this means we have successfully installed JAVA JDK.

selenium examples

Step-5: Installing Maven (Build Tool)

Based on the concept of the project object model (POM), Maven can manage a project’s build, reporting, and documentation from a central piece of information. It is the easiest way of installing Selenium. It can automatically download all dependencies which are written in your pom.xml file and build your project with standard project layout format. You can find all details about maven from Maven HomePage.

To install maven first go to the download page (Download Maven) and click “binary zip archive”

selenium webdriver java

When you downloaded the installation file, then we should follow the instructions which are written on Maven Installation Instructions page.

1- First, copy “” and paste under “C:\Program Files” and then extract it. After extracting package it will look like as below.

selenium junit

2- Set JAVA_HOME environment variable.

• Open RapidEE and click “Add new variable” button.

webdriver junit

• Write “JAVA_HOME” as a variable name.

webdriver examples

• Then press “F7” and select the JAVA JDK path (C:\Program Files\Java\jdk1.8.0_60) and then click “OK” button.

selenium tutorial

• Finally, save the changed setting by clicking save icon.

 step by step webdriver

3- Adding maven folder (C:\Program Files\apache-maven-3.3.3\bin) into the PATH variable.

• The first select path variable

selenium tutorial java

• Then click “Add value to the current variable

rapidEE installation

• Copy and paste maven bin directory and press enter. Then click the save button.

selenium for dummies

4- Check maven installation is succeeded or not.

• Open a cmd window and type “mvn –v” then press enter. You should see below result.

webdriver projects

Step-6: Installing IDE (IntelliJ)

We will write our test automation codes by using IDE. There is two major IDE version on the market now. They are Eclipse and IntelliJ. In this tutorial, I will go with IntelliJ. If you want to familiar with Eclipse you can go on with Eclipse IDE for this selenium tutorial.

JetBrains is developing IntelliJ and you can download its community edition version for free from their website. Its download link: Download IntelliJ

When you download the setup file, you can complete the installation by clicking the next buttons. It is so easy.

Step-7: Starting to Automate with Selenium Webdriver JUnit & Maven & IntelliJ

This is the most critical step of this tutorial. At the end of this step, we will have done our first automation project. We are using Maven as our build tool so it will automatically download everything we need for web test automation. So, now let’s start to automate step-by-step.

1) Open IntelliJ and click “Create a New Project”


2) Select “Maven” as the project type and then browse JAVA folder as Project SDK and then click the “Next” button.

webdriver sample code

3) Fill GroupId and ArtifactId for maven project as you wanted. In this selenium tutorial, I preferred to write “firstSeleniumTest”.


4) Give a name to your project and then click “Finish” button. I selected “firstSeleniumTest” as my project name.

selenium tutorial

5) After clicking “Finish” button, our project will be created and you will see below screen. Then, you should click “Enable Auto-Import”. Thus, when you change your pom.xml maven automatically imports the required files according to your changes.

selenium testing

6) You can see project tree by clicking arrow icons. In pom.xml we hold all of our maven information. Under the test -> java we will create our first selenium test automation file.

IntelliJ junit

7) It’s time to get JUnit to automate our first Selenium test. Hence, you should go JUnit GitHub Page and copy Maven dependency code snippet.


8) Go to pom.xml file and create <dependencies> </dependencies> section and paste JUnit dependency into it. Before we clicked “Enable Auto-Import” so after pasting JUnit dependency, IntelliJ automatically imports related JUnit files into our project.


9) Now, we will get Selenium Webdriver. It will be our test automation framework. For this, you should go to Selenium Home Page, copy maven dependency and paste into our pom.xml’s dependencies section.


• After pasting Selenium dependency, IntelliJ automatically starts to download related files.


Finally, we finished preparing everything. Let’s start automation!

Note: You can check how to use Selenium 3.0 in this article.

10) Go to project tree, right click on java folder, then go to New, and then click “Java Class”


11) After that give a name to your first automation test and then click “OK”.


12) If you want to organize your test files, you can put them into a package. To do this, Right click to java folder, click “Package”.


• Then give a name to a package and click “OK”.


• Drag&Drop FirstAutomation test file to our package.


• After that, our automation test file is located newly created package as shown below.


13) Now we will start to write test automation code for this selenium tutorial. I tried to emphasize important information with comments in below code.

14) It is time to run the code! 🙂 To do this you can click the run icon on the upper menu or right click test automation file and then click Run ‘FirstAutomationTest’ or you can just press ctrl+shift+F10


• If the test passed you will see this on the output screen as shown below.

test automation

• When the test is failed, the output screen will look like below. You will get detailed error info on the output screen.


IntelliJ Tips & Tricks

When you start to write your class, IntelliJ automatically shows all the classes start with your class first letters.


When you select your class, IntelliJ automatically imports related source classes.


IntelliJ automatically shows all Classes when you are instantiating an object.


After object name, if you put a dot, it shows that object’s methods.



  • Installed Ranorex Selocity and learned their usage.
  • Installed JAVA JDK.
  • Installed Maven.
  • Set required environmental variables for JAVA JDK and Maven.
  • Installed IntelliJ and learned how to create a Maven project.
  • Wrote our first selenium webdriver test automation code and run it.
  • Learned important key points of IntelliJ while you are coding Selenium Webdriver Java and JUnit.

Next Selenium Webdriver Article

Click Next Selenium Webdriver Tutorial: Selenium Webdriver API

Selenium Webdriver Tutorial Series

By |2018-12-04T15:16:12+00:00November 20th, 2018|Test Automation|31 Comments

About the Author:

Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as a research assistant, design verification engineer, and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. After Teknosa, he worked as Head of Software Testing and Manager of two Development Teams at Now, he is working as a Senior Technical Consultant at Emirates Airlines in Dubai. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.


  1. Neinstein and Associates February 20, 2016 at 5:14 am - Reply

    Very nice article, exactly what I needed.

  2. aravind March 15, 2016 at 2:59 pm - Reply

    can u explain the same using IE and Eclipse IDE ?

    • Onur Baskirt March 15, 2016 at 3:51 pm - Reply

      Hi, Onur Yazir will explain how to do this with Eclipse IDE soon.

  3. Stephan May 11, 2016 at 12:03 pm - Reply

    Thank your very much, this is pretty straight forward.

    in Step 13: If you copy and paste the code, it will not run at all. Thats why i was stuck for hours 😀

    Solution : type the testcase by hand and IntelliJ will automatically set the correct paths

    But this article is by far the fastest and best one to setup a testing Enviroment !

    • Kymchy May 30, 2016 at 4:42 pm - Reply

      Thanks for the helpful comment

  4. Sandesh June 1, 2016 at 6:20 am - Reply

    Very nice article …

  5. Kleberson June 22, 2016 at 1:20 am - Reply

    Perfect !
    Simple and objective

  6. Ulvi August 30, 2016 at 10:11 pm - Reply

    What’s advantage of FirePath over FireBug?


    • Onur Baskirt August 31, 2016 at 1:58 am - Reply

      FirePath is just an add-on of firebug. When you install it, you can see a new tab as “firepath” and with firepath you can find the CSS or XPATH of an element easier. Also, you can find the CSS or XPATH without using firepath. It depends on your choice and the way of locator finding. 😉 “Sen de adamsın!” 🙂

  7. Cristian Pandele February 1, 2017 at 5:29 pm - Reply

    I have this issue when i am trying to run a script, a cannot import “org.openqa.selenium.WebDriver;”. I think it is a maven problem. Any ideeas?

  8. Cristian Pandele February 1, 2017 at 5:30 pm - Reply

    I have this issue when i am trying to run a script, a cannot import “org.openqa.selenium.WebDriver;”. I think it is a maven problem. Any ideeas?

  9. Julia June 21, 2017 at 11:43 pm - Reply

    This was an awesome tutorial, thank you.

  10. Shiva June 25, 2017 at 7:30 am - Reply

    Thanks. Really helpful.

  11. Olcay Talanci July 10, 2017 at 6:16 pm - Reply


    Nice great article! But at step 13 when I copy your code into Intellij I get test failed messag below. What do I wrong? Does it not work with firefox webdriver? Or must I set gekko webdriver?

    java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see The latest version can be downloaded from

    at org.openqa.selenium.remote.service.DriverService.findExecutable(
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(
    at org.openqa.selenium.remote.service.DriverService$
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(
    at org.openqa.selenium.firefox.FirefoxDriver.(
    at org.openqa.selenium.firefox.FirefoxDriver.(
    at com.swtestacademy.webdriver.FirstAutomationTest.firefoxTest(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(
    at org.junit.runners.ParentRunner.runLeaf(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.ParentRunner$
    at org.junit.runners.ParentRunner$1.schedule(
    at org.junit.runners.ParentRunner.runChildren(
    at org.junit.runners.ParentRunner.access$000(
    at org.junit.runners.ParentRunner$2.evaluate(
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
    at com.intellij.rt.execution.junit.JUnitStarter.main(

    Process finished with exit code -1

    • Onur Baskirt July 12, 2017 at 4:49 pm - Reply

      You should use gecko driver afther selenium 3.x versions.

  12. Hiren July 17, 2017 at 1:34 pm - Reply

    Very nice and simple explanation , I searched a whole internet but u r explanation is out of the world please do let me know if you have a full Junit project on POM and BDD scenarios
    I am making a framework and will be using this technologies

    • Onur Baskirt July 17, 2017 at 1:39 pm - Reply

      We have some BDD with cucumber post in our website by Canberk Akduygu and Ozgur Kaya. Thank you. I did not use BDD in real life. 😉

      • Hiren July 18, 2017 at 12:01 pm - Reply

        Hi, Onur thanks a lot for the prompt reply actually I am looking for the whole example with

        I saw one post here with a good POM example but unfortunately it is in C#

  13. radhika October 10, 2017 at 12:04 am - Reply

    its simple and easy to understand

  14. Sumeet May 10, 2018 at 12:12 pm - Reply

    Do we require to add:

  15. Sankar June 18, 2018 at 7:46 pm - Reply

    while running in selenium java version 2.42.1 i am observing the following error
    org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files\Mozilla Firefox\firefox.exe) on port 7055; process output follows:

  16. […] Test Framework: TestNG (The other popular test runner framework is JUnit) […]

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.