Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ


Outline

Selenium Webdriver is the most popular open source web test automation framework across the wide range of browsers and platforms. In this tutorial, you will learn how to do web test automation with Selenium Webdriver and the related tools.

Audience

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

Prerequisites

Web Automation Framework: Selenium Webdriver

Development Kit: JAVA JDK

Web Browser: Firefox

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 on JAVA or any other object-oriented programming languages.

Step-By-Step Instructions

Step-1: Installing Firefox

Link: https://www.mozilla.org/tr/firefox/new/

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

Step-2: Installing Firefox Add-ons

IMPORTANT NOTE!

Firebug and Firepath died! Please, skip step 1 and step 2! 🙁

I am so sorry but we can use Chrome and Selocity plugin instead of them! Below you can find the Selocity article!

Click and go to Ranorex Selocity Article 

1- Firebug: Go to http://getfirebug.com/ and click “Install Firebug” button and do the followings:

Note: The Firebug extension isn’t being developed or maintained any longer. We invite you to use the Firefox DevTools instead, which ship with Firebug.next. See also Migration from Firebug guide.

firebug_1

• Click Download link.

firebug_2

• Click “Add to Firefox” button.

firebug_3

• Click “Install Now” button.

firebug_4

• You will see “Firebug has been installed successfully.” Message.

firebug_5

• Open a website and press “F12” button. Firebug automatically opens and with Firebug we can easily analyze HTML, CSS, DOM, cookies of all websites very easily.

firebug_6

Brief Explanation of How to Use Firebug:

• First, click the pointer icon.

firebug_7

• Then, select any element of the website, then firebug automatically highlights its html.

firebug_8

• When you right-click the highlighted HTML, you easily reach its XPath and CSS selectors.

firebug_9

2- FirePath: You can edit, inspect and generate XPath 1.0 expressions, CSS 3 selectors and JQuery selectors with FirePath. To install FirePath go to https://addons.mozilla.org/en-US/firefox/addon/firepath/ and click “Add to Firefox” button and then click “Install Now” button. When it is installed, restart the Firefox.

Brief Explanation of How to Use FirePath:

• First, click F12 button to open FireBug, then you will see FirePath tab at the most right side as shown below.

firepath_1

• Then, for example, if you highlight all paragraph, just type “.//img” then press enter.

firepath_2

Also, you can get an element’s XPath expression:

firepath_3

To find an element’s CCS selector, you can do the followings:

firepath_4

firepath_5

Step-3: Installing Environment Editor

With RapidEE you can easily edit Windows Environment Variables. To get RapidEE, go to http://www.rapidee.com/en/download 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 window.

rapidEE_1

Step-4: Installing JAVA JDK

Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and select the appropriate version of JDK and click “Accept License Agreement” and download JAVA JDK as shown below.

java_jdk_1

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

java_jdk_2

java_jdk_3

java_jdk_4

java_jdk_5

java_jdk_6

• 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.

java_jdk_7

Step-5: Installing Maven (Build Tool)

Based on the concept of 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 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 https://maven.apache.org/index.html

To install maven first go to download page (https://maven.apache.org/download.cgi) and click “binary zip archive”

 maven_1

When you downloaded the installation file, then we should follow the instructions which are written on https://maven.apache.org/install.html page.

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

maven_2

2- Set JAVA_HOME environment variable.

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

rapidEE_2

• Write “JAVA_HOME” as variable name.

rapidEE_3

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

rapidEE_4

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

 rapidEE_5

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

• First select path variable

rapidEE_6

• Then click “Add value to the current variable

rapidEE_7

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

rapidEE_8

4- Check maven installation is succeeded or not.

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

maven_result

Step-6: Installing IDE (IntelliJ)

We will write our test automation codes by using IDE. There is two major IDE version in 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.

JetBrains is developing IntelliJ and you can download its community edition version for free from their website. Its download link: https://www.jetbrains.com/idea/download/

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

Step-7: Starting to Automate with Selenium Webdriver & Maven & JUnit & 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”

intelliJ_1

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

intelliJ_2

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

intelliJ_3

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

intelliJ_4

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.

intelliJ_5

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_6

7) It’s time to get JUnit to automate our first Selenium test. Hence, you should go https://github.com/JUnit-team/JUnit/wiki/Download-and-Install and copy Maven dependency code snippet.

maven_dependency

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.

pom.xlm

9) Now, we will get Selenium Webdriver. It will be our test automation framework. For this, you should go to http://www.seleniumhq.org/download/maven.jsp, copy maven dependency and paste in to our pom.xml’s dependencies section.

selenium_maven_conf

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

selenium_maven_dependecy

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

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

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

automation_1

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

automation_2

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”.

automation_3

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

automation_4

• Drag&Drop FirstAutomation test file to our package.

automation_5

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

automation_6

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

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

automation_7

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

automation_8

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

automation_9

Extra Info: Important points while you are writing your code with IntelliJ

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

automation_10

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

automation_11

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

automation_12

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

automation_13

Summary

  • Installed FireBug & FirePath 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 test automation code and run it.
  • Learned important key points of IntelliJ while you are coding.

Keywords

Selenium, Webdriver, Java, IntelliJ, Maven, JUnit, Automation, Web Test Automation, RapidEE, FireBug, FirePath, Selocity

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
extentreports

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 5 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
extentreports

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
extentreports

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across the wide range of browsers and platforms. In this tutorial, you will learn how to do web test automation with Selenium Webdriver and the related tools....
extentreports

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
extentreports

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...

Getting Started with RobotFramework on Windows

By Onur Baskirt / Apr 25, 2016 / 12 Comments
What is RobotFramework? RobotFramework is a GENERIC test automation framework for acceptance testing and acceptance test-driven development (ATTD). What it means that you can do web, mobile, desktop and other test automation activities with related test libraries. These libraries can...
By | 2017-10-22T03:11:29+00:00 September 8th, 2015|Selenium, Test Automation, Web Automation|26 Comments

About the Author:

Onur Baskirt
Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as 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. Now, he is working as Head of Software Testing and Manager of two Development Teams at Kariyer.net. 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.

26 Comments

  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
      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?

    Adamsın

    • Onur Baskirt
      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

    Hello,
    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

    Hello,
    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? https://uploads.disquscdn.com/images/4c78885bc77d053cd1dc33f1091b4957bcd120a3066c915a3db31fb5a369be11.png

  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

    Hi,

    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 https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases

    at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:104)
    at com.swtestacademy.webdriver.FirstAutomationTest.firefoxTest(FirstAutomationTest.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

    Process finished with exit code -1

    • Onur Baskirt
      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
      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
        Java
        Junit
        POM
        BDD
        Maven

        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

Leave A Comment