Introduction to Selenium

What is Selenium?

Selenium basically automates browsers. You can refer more details about it on the link http://www.seleniumhq.org/. Selenium also emulates user interaction with a web page.


What is WebDriver?

WebDriver is an official W3C Specification and it is basically a method of interacting with a web browser. Initially, with the presence of Selenium RC, one could operate Selenium along with the browser by injecting javascript in order to interact with the elements.

But with the adoption of WebDriver, browsers like Google, Mozilla and Microsoft were released with the ability to be controlled by a hook that Selenium can tap into. This hook enables Selenium to interact with the Web browser the same way a human being does.



In order to explore Selenium API we need to have the following pre-requisites:

  • Any version of Eclipse is acceptable.

  • Installing Java is a must

  • Download Selenium jar from seleniumhq.com

Steps to open API just to explore.

  • Create a Java project with the name “selenium-api-exploring” and then create a simple Java class comprising of main method as shown below:

Selenium API Explorer

  • Add the jar file of selenium in your Java project which contains many classes having byte code which means class files will be there but not the source code i.e. Java file.

  • Now right click on project >> go to properties >> libraries tab >> click on add external jar >> browse selenium jar ( i.e where you have stored it in your computer).

Steps to add selenium jar file in java project in eclipse

  • After that click on “OK”.

  • Once it has been added to your project it will look as shown below:

selenium jar file is located in referenced library folder of java project in eclipse

  • “Selenium API” is not developed by “Oracle” and so we need to add the “JAR” file into our project and by default, we only have built-in predefined Java code base in eclipse, for e.g. classes like System, String.

  • Now we will start to learn by writing a simple selenium code inside main method.

  • You can use “Ctrl + space” shortcut key provided by Eclipse for auto completion of any existing class names.

  • But here we will write „Web and “Ctrl + space” key.

for auto completion of any existing class names in eclipse press ctrl + space

  • While writing “WebDriver” we will observe that package is coming from org.openqa.selenium as shown above and this is the selenium jar which we just added.

  • Now we will see the source of “WebDriver” as shown in the below screen.

  • Right click on “WebDriver” and select “Open Declaration” or press F3.

to view source of webdriver in selenium right-click on web driver and select open declaration

  • Here we need to browse the source file.

To view source of webdriver in selenium go to attach source and then add external location of source file

  • You need to download the Selenium source code here separately in order to see it. You can download it from http://javabykiran.com/selenium/download/

  • Now you can read the source code which is in readable form.

  • The above process is just to see the source code of any API.

  • Remember, we will always use byte code for execution, but in order to explore more we need to read API, so we performed the above procedure.

  • Below are some of the important selenium interfaces we need to look at.

This is an interface in Java having various methods. In order to see all the methods we use the shortcut key “Ctrl + O” or else in Menu Bar go to Window > Show View > outline as shown below:

view web driver interface methods in java by pressing ctrl + o in eclipse java project

  • Now select any method in “Outline” and start reading and remember what is the use of every method.


WebDriver Interface

  • get()

  • getTitle()

  • getPageSource()

  • getWindowHandles()

  • getWindowHandle()

  • getCurrentUrl()

  • findElement(By arg0)

  • findElements(By arg0)

  • manage()

  • navigate()

  • close()

  • quit()


WebElement Interface

  • clear() // If the element is a text entry, this will clear the value.

  • click() // “Click” this element.

  • FindElement(By by) // Finds the first WebElement using the given method.

  • findElements(By by) // Finds all elements within the current context using the given mechanism.

  • getAttribute(String name) // Gets the value of the given attribute of the element.

  • getCssValue(String propertyName) // Gets the value of a given CSS property.

  • getLocation() // Gets the location of the rendered element.

  • getSize() // Gets the width and height of the rendered element

  • getTagName() // Gets the tag name of the element.

  • getText() // Gets the visible text

  • isDisplayed() // show whether is the element displayed or not

  • isEnabled() // shows whether is the element currently enabled or not

  • isSelected() // Determines whether or not the element is selected or not.

  • sendKeys(String keysToSend) // This method is used to simulate typing into an element which may set its value.

  • submit() // If the current element is a form or an element within a form, then this will be used to submit it to the remote server.

As we have discussed about adding the selenium standalone jar in the project, is enough for automating any website.
Some points to be noted before going ahead.

  1. Selenium jar and Firefox version should be compatible.
    Here we have used selenium-standalone version 52 and Firefox version 42.0

  2. Make sure your Firefox is installed in the program files.

  3. Keep the Firefox updates off by going to Options >> advanced >> update >> never update option.

  4. Add Firebug add-on to Firefox.

  5. Add Firepath add-on to Firefox.

  6. Also add the WebDriver Element Locator add-on to Firefox.

  7. Refer the below given image for all add-ons in the firefox.

guide to add add-ons of firebug, firepath and webdriver element locator in the firefox

guide to add add-ons of firebug, firepath and webdriver element locator in the firefox

For the above example, write the below given code inside main method.

Here findElement is a very important method which returns the “WebElement”. After this we need to call methods of WebElement as shown above from API.

We will now try to locate the username text box in our website, so that the text can be simulated into the text box. See the below given screen:

selenium locator example

Observe tag marked with number (4) in the above screen where we will observe that id has a unique identifier for this textbox. This textbox is unique by this id.

Here the id is called as locator type. There are many types of locators like id, xpath, name etc. Here email is locator value which will be different for different textbox/input controls of html.

Now as we see our program performs the following tasks as mentioned below based on the code given below the tasks:

  • Opening of browser

  • Entering login details

  • Click login button

  • Click logout

  • Close browser

Full-fledged automation for offline website. Try the below given points while doing automation,

  1. Remove redundancy

    1. This is very important aspect while designing framework.

    2. As a beginner we may write any kind of raw code but later we will discover that all things are redundant.

    3. In order to avoid this just create some methods which will replace the redundant code and this methods can be called as and when and wherever we need it.

  2. Create utility classes.

    1. Zip file creation

    2. Excel read

    3. Screenshot

  3. Folder Structure

    1. automation

    2. utility

    3. common functionality

    4. test cases

    5. configure

    6. data