About - Selenium

Definition

Development and Versions

Pre-requisites

Languages supported

Manual vs Automation

Choosing Selenium

Selenium - Components

Selenium IDE

Selenium RC

Selenium WebDriver

Selenium Grid

QTP

Selenium vs QTP

Selenium - IDE

Features

Install and configure

Selenium - Webdriver

About WebDriver

install and configure

First test case

Web element

Element Locators

Example of locators

Chropath

Web driver commands

Eclipse and TestNG

About Eclipse

Choose and download

About TestNG

Testng installation on eclipse

Testng Annotations

Pass Fail and Skip test cases

Run test cases in batch

testng.xml - Explanation

testng grouping

Running tests parallely

dependsOnMethods

dependsOnGroups

Prioritization

Passing parameters

Data providers

Read data from excel

Ant - XSLT reports

Testng listeners

Cross browser testing

Page Object Model



Selenium - Example Element Locators
 


For Example in the Page source we have the following element like below:
< input type="text" name="username" id="username"/>

This is a editbox where use can enter username.Here Editbox is called a WebElement.
To identify this Element we use name or id attribue...

The code to locate and identify this element is as follows:
driver.findElement( BY.name("username")).sendKeys("Ananda")

In the above code :

a) findElement is for locating element and

b) BY is a class name which consists of various method like .name() .id .link .tagName etc... For each locator one method is there.For locating with ID attribute, we have BY.id, for locating with name By.name etc...

c) sendKeys is command to send data to the WebElement here is Ananda which is the data sent to Edit box locator.

Let's see how this code works with example...

Test case for login functionally on FlipKart a) Navigate to https://www.flipkart.com/account/login

b) User enters Email

c) User enters password

d) User clicks on Login button.

Code for the following:
package com.ananda.selenium.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SelenniumDemo1 {

public static void main(String[] args) throws InterruptedException {

/*
System.setProperty("webdriver.gecko.driver", "C://Program Files/Mozilla Firefox/firefox.exe");

//System.setProperty("webdriver.gecko.driver", "C://Program Files\Mozilla Firefox");
WebDriver driver= new FirefoxDriver();
System.out.println("browser opened");
driver.get("http://www.google.com");

System.out.println("google opned");<
br> //Thread.sleep(2000);

driver.close();

System.out.println("browser closed");


*/

System.setProperty("webdriver.chrome.driver", "C://Program Files/Google/Chrome/Application/chrome.exe");

WebDriver driver= new ChromeDriver();
driver.get("https://www.flipkart.com/account/login");

driver.findElement(By.className("_2zrpKA _1dBPDZ")).sendKeys ("anandamay10@gmail.com");
driver.findElement(By.className("_2zrpKA _3v41xv _1dBPDZ")).
sendKeys("password"); driver.findElement(By.className("_2AkmmA _1LctnI _7UHT_c")).click();

//Thread.sleep(2000); driver.close();

}

}

Explanation of process for finding and choosing Element locators:

a) Navigate to https://www.flipkart.com/account/login

b) Right click on the page and select Inspect Element Q as follows. The following frame appears on the down where source code and inspecting the page source code.



Select the arrow icon as shown below




1)After clicking on the arrow icon, move the mouse to corresponding element on browser and click on the element

2)Then corresponding element will get highlighted in the inspecting frame.

3)After that choose the element locators for that corresponding element. Here its email .

4)In this scenario there is no name, Id, link etc..

5)Only two types of locators are available. One is Classname and another is TagName:


Here we will take className as locator to locate the email element. Heres how the code looks like...


package com.ananda.selenium.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SelenniumDemo1 {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C://Program Files/Google/Chrome/Application/chrome.exe");

WebDriver driver= new ChromeDriver(); driver.get("https://www.flipkart.com/account/login");

driver.findElement(By.className("_2zrpKA _1dBPDZ")).sendKeys ("anandamay10@gmail.com");
driver.findElement(By.className("_2zrpKA _3v41xv _1dBPDZ")). sendKeys("password");
driver.findElement(By.className("_2AkmmA _1LctnI _7UHT_c")).click(); //Thread.sleep(2000);

driver.close();

}




Execute the above code and the login process is automated this way. No manual entering of fields and no manual clicking of buttons.

Based on different type locator attributes available, you can choose one of the locator.




I conduct online tutions on Manual Testing, Selenium and the following. For more details contact anandaibmindia@gmail.com
Manual Testing - 1600/-           Selenium - 1600/-
           Jsp and Servlet -2000/-           Java training - 2000/-