Types of Locators to identify the WebElements

A web page consists of set of different web elements. We need to know what are the different elements on a web page and then how to locate the web elements.

Here are some common web elements we encounter most of the time while automating the web page.
Text box
Drop-down List
Checkbox
Radio button
Hyperlink
TextArea
Image
Button
Window

An element locator is simply a method for finding an element on the web page.
We can use the following locators based on priority to identify the elements on the web page.
– Locating By Id
– Locating By Name
– Locating By Class
– Locating Hyperlinks by LinkText
– Locating By Xpath
– Locating By CSS

Steps to find the element locator for the web element:
-> Search for the locator which will find the element by ID or proceed to the next step.
-> Search for the locator which will find the element by Name or proceed to the next step.
-> Search for the locator which will find the element by Class or proceed to the next step.
-> Search for the locator which will find the element by LinkText or proceed to the next step.
-> Search for the locator which will find the element by XPath or proceed to the next step.
-> Search for the locator which will find the element by CSS or proceed to the next step.

While following the above steps to locate elements, a question may come to our mind that why there are so many types of locators instead of any one to identify the elements.

Element Locator is a way of finding the HTML element on the web page. Every HTML element on the page we are testing with Selenium will have a unique Name or ID that we can always refer to. But, usually we are doing black box testing and have no access to the developer’s code and there is difficulty in finding the correct elements due to absence of unique identifier like Name or ID. Also, there are some web applications which recreates the element ID each time dynamically when the application is run. In this case, it is very difficult to handle if we depend on only one type of locator.

Some basic syntax for different locators:
1. By ID:
driver.findElement(By.id(“element id”))

2. By NAME:
driver.findElement(By.name(“element name”))

3. By CLASS:
driver.findElement(By.className(“element class”))

4. By Link:
driver.findElement(By.link(“link text”))

5. By XPath:
driver.findElement(By.xpath(“xpath expression”))

6. By CSS Selector:
driver.findElement(By.cssSelector(“css selector”))

These locators can be identified by using Firebug and Firepath plugins in Firefox Browser

Create and Execute your first Webdriver script using Eclipse

I have already posted in my last blog how to install and Configure Webdriver using Eclipse and Java. Now, its time to write the Java code for your testcase of the web application. I will give one simple example of creating a webdriver script with explanation in the form of comments.

For this, we need to write the following code into Eclipse.

package MyPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MyClass {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();  //Create a new instance of Firefox Browser
        driver.get("https://www.google.co.in/"); //Open the URL in firefox browser
        driver.manage().window().maximize(); //Maximize the Browser window
        String str = driver.getCurrentUrl(); //Get the current page URL and store the value in variable 'str'
        System.out.println("The current URL is " + str); //Print the value of variable in the console
    }
}

Now, click on the Run Menu of the Eclipse window to execute the script.

As an output, It will open the firefox browser , maximize the browser window, get the current page URL and print the value in the Console.

How Much Java is needed to learn for writing Selenium Script

Selenium is a web automation tool which enables us to automate the web browsers. Selenium Webdriver supports different languages like Java, python, c#, Ruby or other. So we can choose any language to write our script. If we want to learn Selenium, we need to have basic knowledge about one programming language – don’t have to be an expert.

Here I have listed down some of the Java essentials which we need to learn before jumping to Selenium Scripting

  • OOPS concept – Class, Objects, Abstraction, Polymorphism, Inheritance and Encapsulation
  • Programming essentials – Object Instances, variables, method overloading/overriding concepts and packages, Constructors,Abstract Class, Interface etc..
  • Control Statements – If statements, While, do-While, Switch – This will help us in writing the scripts for multiple scenario statements and decision making scenarios.
  • Looping statements – This can be used to execute the same statements multiple times for ex; iterating through a large table to find a record and logging into 5 different accounts using same method.
  • Arrays Concepts – We don’t want to keep writing variables all over our test scripts. For that we need to create a placeholder for similar type of variables. So, concept of Array allows us to string related variables together into one single variable that’s easy to use.
  • Threads and Multithreading Concepts – Multithreaded program contains multiple activities that can run concurrently and each part can handle different task at the same time making optimal use of the available resources. That will help us in achieving better performance.
  • Java Collections Framework – This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections. Collections can be used in various scenarios like Storing phone numbers, Employee names database etc. They are basically used to group multiple elements into a single unit.
  • File Streams – This will be helpful in externalization of data through CSV, Excel or Java Properties file.
  • Exception Handling – When an error occurs in a Java program it usually results in an exception being thrown. Java exception handling enables the Java applications to handle the errors. Exception handling is a very important aspect of writing robust Java applications or components.

There are many things in Java. But we need these basic things before jumping to Selenium scripting.

Regards,
Pratima Rout
QA Engg., Mindfire Solutions