Monday, August 24, 2015

Difference between Absolute & Relative Xpath example on google using selenium

Hello friends........you know that, google is a grate search engine in Internet world....most of the software engineers can't live without google......But now I want to show you a simple fun in google home page 

This example can show you the difference between the functionality of Absolute & Relative Xpath.

The code is follows.....................


if you like the code....just share my blog with your friends. 





package sampletestng;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Googletest {
//Initialize webdriver
public static WebDriver driver= new FirefoxDriver();
@BeforeTest
public void beforeOperations(){
String url="http://www.google.co.in/";
//Launch Google
driver.get(url);
}
@Test
public void gmaIllink() {
//try to click on gmail link using absolute xpath of gmail link in google home page
driver.findElement(By.xpath("html/body/div[1]/div[3]/div[1]/div/div/div/div[1]/div[2]/a")).click();
driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
if(driver.getTitle().contains("Gmail")){
Reporter.log("Gmail link validated successfully with absolute xpath");
}
else{
Reporter.log("Gmail link validation failed with absolute xpath");
}
//get back to google home page
driver.navigate().back();
//try to click on gmail link using relative xpath produced by firebug
driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[2]/a")).click();
if(driver.getTitle().contains("Gmail")){
Reporter.log("Gmail link validated successfully with Relative xpath generated by firebug");
}
else{
Reporter.log("Gmail link validation failed with Relative xpath generated by firebug");
}
//get back to google home page
driver.navigate().back();
//try to click on gmail link using relative path which was created by following xpath axis
driver.findElement(By.xpath("//a[text()='Gmail']")).click();
if(driver.getTitle().contains("Gmail")){
Reporter.log("Gmail link validated successfully with Relative xpath of Xpath axis");
}
else{
Reporter.log("Gmail link validation failed with Relative xpath of xpath axis");
}
}
}


What's happening in the above code is, Those three xpaths are of gmail link in google home page. But instead of xpath which was generated using xpath axis, remaining two will fail to locate gmail link in the page. That is the fun here....



Thankyou...................Prasad

 

1 comment:


  1. Selenium WebDriver fits in the same role as RC did, and has incorporated the original 1.x bindings. It refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just "WebDriver" or sometimes as Selenium 2.
    Selenium Training Institute in Chennai

    ReplyDelete