Selenium

Introduction:

Selenium Tutorials: Selenium is a free and open source test automation tool for testing web applications.

Selenium can be installed on most of the operating systems like

  • Windows 
  • Linux 
  • Apple iOS   etc.

Cross browser testing is possible using selenium i.e., we can run the test scripts in different browsers like

  • Firefox 
  • IE 
  • Chrome 
  • Opera 
  • Safari  etc.

Selenium is a suite of tools having various testing functions used to test all types of web applications. One of the key features in selenium is “Multi browser testing“. Selenium supports below programming languages:

  • Java
  • C++
  • Python
  • Pearl
  • PHP
  • Ruby

In this selenium tutorial you will learn about selenium framework, TestNG reporting, locators to identify web elements, interview questions, and selenium quiz.

Pre-requisites to learn selenium:


Selenium Web Driver:

WebDriver API is a new feature that has been added in Selenium 2.0. The main purpose of Webdriver is to provide a simple programming interface and to overcome the limitations faced in the Selenium RC API.

Setup Selenium WebDriver project:

We need any of the below java development IDE’s to work on Selenium. Prior to this, we need to download Selenium Standalone Server from http://www.seleniumhq.org/download/

Extract the zip file to a folder. It contains the required jars to run selenium scripts.

Setup Java SE Development Kit:

Download and install the Java SDK from –

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Setup Eclipse:

Download Eclipse IDE from –

http://www.eclipse.org/downloads/eclipse-packages/



Setup TestNG:

Can be installed via two methods.

  • From Eclipse Marketplace.
  • From update site.

How to use Webdriver API:

We have to create a Web Driver instance and instantiate the driver to any particular browser.

Firefox:

WebDriver wd=new FirefoxDriver();

Internet Explorer:

WebDriver wd=new InternetExplorerDriver();

Google Chrome:

WebDriver wd=new ChromeDriver();

Safari:

WebDriver wd=new SafariDriver();

Opera:

WebDriver wd=new OperaDriver();


How to locate web elements in the UI:

First, we need to identify the elements/objects present in the DOM to do any action. The element is maybe a textbox, textarea, dropdown, radio button, checkbox, or text present in the UI. A web element can be identified using below locators.

WebElement element = driver.findElement(By.id(“id”));

In the above code, WebElement is used to store the element present in the DOM, findElement() method is used to locate the element in the DOM. By() method is having different identifiers to locate the element.

  • By ID
  • By Xpath
  • By Linktext
  • By CssSelector
  • By Name

Example:

driver.findElement(By.id(“username”)).sendKeys(“selenium_user”);

or

WebElement element=driver.findElement(By.linkText(“TestNG”));
element.click();


Sample web driver script having usage of all the locators:

 

 


Difference between findElement() and findElements() method:

*findElement() method returns only one web element. If the element does not found in the DOM, it throws NoSuchElementException.

*findElements() method returns zero or more web elements in the form of list. If element is not found in the DOM, it returns an empty list. If one or more elements with same identifying criteria found, then it returns all elements in the form of list.

Syntax:

import java.util.List;

List<WebElement> list=driver.findElements(By.tagName(“a”));


Firebug and Firepath:

These are the Firefox plugins or add-ons used to inspect the HTML elements. 

firebug

Inspecting the web element:

To verify whether Firebug is installed, check for the Firebug icon in the top right corner of the browser. To inspect any web element, click on the Inspect Element icon and move the cursor to the respective HTML element. Firebug highlights the HTML code of the web element or displays the xpath of the element in the XPath bar.

 

How to work with different web elements (Hands on):

Select a dropdown using selenium webdriver.

Tags: Selenium tutorials, selenium web driver, selenium framework

More links: Convert string to int in Java
 

One thought on “Selenium

Leave a Reply

Your email address will not be published.