Complete Guide of Selenium Basics and Environment Setup

In this post, we will give you complete guide of Selenium basics knowledge like What is Selenium, What is WebDriver, Exploring Selenium API, Environment Setup for Selenium, automation examples, Locators and how to use locators in Selenium.

What is Selenium?

Selenium is used to automate the browsers.

It is a free open source tool to automate the web based application only across different browsers and platforms. We can use multiple programming languages like Java, C#, PHP, Python, Perl, Ruby in Selenium test scripts.

We cannot test any desktop application and Mobile application.

 

What is WebDriver?

WebDriver is now an official W3C specification, and it is a method of interacting with a web browser.

Previously, with Selenium RC, Selenium would operate with the browser by injecting JavaScript to interact with the elements.

With the adoption of WebDriver, browser manufacturers like Mozilla, Google and Microsoft edge release their browser with the ability to be controlled by a hook that Selenium can tap into.

This hook enables Selenium to interact with web-browser in the same way that humans do.

There are multiple programming languages which are supported by WebDriver such as Java, PHP, Ruby, Python, .Net to create test scripts.

It also supports multiple operating system like Microsoft Windows, Linux and Apple OS.


Complete Guide of Selenium Basics:

Selenium API: 

To explore an API for selenium we need to have a setup.

Eclipse any version is Ok. [Download Eclipse]

Java should be installed.

Selenium jar downloaded from seleniumhq.com

 

Steps to open API just to explore:

Create Java project with name selenium-api , then create a simple Java class with a main method. See below screen.

Java Project Creation

Add the jar files of selenium to this project.

Steps to add jars:

Right click project > go to Build path > Configure build path > libraries > Click add external jars > browse selenium jars from your system.

You can read this post in details [How to Add External jars in Eclipse]

Adding Selenium jars into Project

Click on Apply and Close.

Once it is added project is look like this.

Selenium Referenced Library

 

Now we will start writing selenium code inside main method.

By using Ctrl + space shortcut for auto complete of any existing class names/methods.

In our case write ‘Web’ and Ctrl + space key and select Interface name.

Import WebDriver

While writing WebDriver observe package name, it is coming from as highlighted org.openqa.selenium. This is from selenium jar which we just added.

Now if we try to see the source of WebDriver we may see below screen.

Right click on WebDriver and click on Open declaration or press F3. See in below image.

Open WebDriver Declaration

 

Source code of WebDriver

 

After click on attach source we can browse the source file. After browse src file or jar file we can see the source code of any API.

Remember, we use byte code always for execution, but to explore more we need to read API, so we did above process.


Below some important selenium interfaces we need to look at –

WebDriver:

WebDriver is an interface in java having various methods. It drives the browser directly.

Do Ctrl + O, you will see all methods there or go to outline window.

WebDriver Interface’s Methods:

  • get()
  • getTitle()
  • getPageSouce()
  • getWindowHandles()
  • getWindowhandle()
  • getCurrentUrl
  • findElement(By args0)
  • findElement(By args0)
  • manage()
  • close()
  • quit()

 

WebElement Interface’s Methods:

  • clear()  // This will clear the value
  • click()  // Click the element
  • FindElement(By by)  // Find the first WebElement using the given method
  • FindElements(By by)  // Find all WebElements using the given method
  • getAttribute(String name) // get the value of given attribute
  • getCssValue(String propertyName)   // Get the value of given CSS Property
  • getLocation()  // This will return location of the webelement which is (x,y) coordinates.
  • getSize()     // get width and height of given element
  • getTagName()     // get tag name of the element
  • getText()  // get text of the textbox
  • isDisplayed()  // Is this element displayed or not
  • isEnabled()   // Is this element enable or not
  • isSelected()   // Is this element selected or not
  • sendKeys(String keysToSend)  //Use this method to enter text into text field
  • submit()   // If this current element is a form or element is inside the form then this will be submitted to the remote server.

Locators in Selenium:

Selenium uses locators to find the web elements of your webpages that it needs to interact with. There are eight locators we can use in selenium.

  • ID
  • Name
  • Link Text
  • Partial Link Text
  • Tag Name
  • CSS Class
  • CSS Selector
  • XPath

 

How to use locators in Selenium?

Many locators are there in Selenium.

ID:

Id to Select the Element with a specified @Id attributes.

It is a unique reference to a web element which is set by developer while writing the code.

Syntax:

driver.findElement(By.id(“IdOfElement”));

Pros:

It is preferable to have a unique id, so it unlikely to meet similar values.

Cons:

Feasible for elements with fixed IDs but not for the generated ones.


Name:

Name to Select the First Element with the specified @Name attributes.

Every form has input fields with unique names. Most of the time, names are unique, but it’s not a restriction.

Syntax:

driver.findElement(By.name(“NameOfElement”));

Pros:

It is more appropriate to use it when you are using similar types of elements on web page.

Cons:

Using it with a dynamically generated list is tough.


Link Text

Link text to select the Link element which contains the matching text.

It is a perfect way to find the links on a page.

Syntax:

driver.findElement(By.linkText(“textOfLink”));

Pros:

It will only work for anchor tags.

Use it for checking navigation flows.

Cons:

You need to provide the link text for it to work.


Partial Link Text

Partial Link Text to Select Link element which contains text matching the specified partial Link text.

It is almost similar to the previous locator.

Syntax:

driver.findElement(By.partialLinkText(“WritePartialTextOfLink”));

Pros:

It is good if link name is very long.

Cons:

As we do write complete link, it may conflict with other links will be duplicated.


TagName:

These are tag name of html.

If we want to know list of all tags and size, then this is easy to get by this locator.

FindElements need to be used as there may be many tags of the same name.

This method will return a list of WebElements, later we can iterate that list.

Syntax:

driver.findElement(By.tagName(“a”));


CSS Selector:

CSS Selector to access the elements.

CSS Selectors are no different that the Xpath. But they are powerful.

Unlike the Xpath, these are not dependent on DOM structure.

These are helpful to find the webelements which is difficult to do with xpath.

Syntax:

driver.findElement(By.cssSelector(“#username”));

 Pros:

Relatively faster than Xpath.

Its usage is growing as the web page are getting more style-centric.

It’s easy to define a unique CSS locator as you can combine multiple CSS attributes.

Cons:

It’s not a easy to form a Css Selector and requires a deeper understanding of the CSS/javaScript.

Xpath:

Xpath is a xml path in xml document to track an element.

Xpath is a good way for walking through the DOM structure of the web page.

Xpath locators are robust and reliable.

While using this you should be very careful while forming an Xpath as it may not work if there are changes in the web application.

Syntax:

//tagName[@attributes Name=’value’]

Xpath is divided into two parts.

 1. Absolute XPATH: It starts from the root element within the web page or part of the page and goes to identify the target web element.

Example:

/html/body/div[1]/section/div[1]/span[1]

2. Relative XPATH: The relative Xpath is easy to manage as those are short and concise.

It is also better than the previous xpath style as it may survive the changes in the page HTML.

Though, building a relative xpath is time consuming and quite difficult as you need to check all the nodes to form the path.

It starts with double slash (//)

Example:

//input[@email=’username’]

 

In coming post we will see How to handle dynamic Xpath in Selenium.

Must Read:

 How To Select Multiple Checkbox in Selenium


Summary:

In this post, we have covered ‘Complete Guide of Selenium Basics, Envrionment setup for Selenium, Different types of locators and how to use it.

We have also learnt pros and cons of all locators.

I am sure this content added some additional value in your skills and also helpful to start selenium from scratch.

Final word, Bookmark this post Complete Guide of Selenium Basics  for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!


Also Read Below Post:

How To Resolve Element Not Interactable Exception

 

 

Leave a Comment