Wednesday 3 July 2013

Selenium WebDriver : getSize() Vs getLocation() functions


Here are the difference between getSize() and getLocation() methods :

getSize() : 
  1. It will returns the "Dimension" object.
  2. If you want to get the width and Height of the specific element on the webpage then use "getsize()" method.
Sample code to get the width and height :
  1. WebDriver driver = new FirefoxDriver();   
  2. driver.get("https://google.com");  
  3. Dimension dimesions=driver.findElement(By.id("gbqfsa")).getSize();  
  4. System.out.println("Width : "+dimesions.width);  
  5. System.out.println("Height : "+dimesions.height);  
Note:
Makesure to import "org.openqa.selenium.Dimension;" package.

getLocation() :
  1. It will return the "Point" object.
  2. If you want to get the exact "x" and "y" coordinates of the element then use "getLocation()"  method.

Sample code to get the "x" and "y" coordinates :
  1. WebDriver driver = new FirefoxDriver();   
  2. driver.get("https://google.com");  
  3. Point point=driver.findElement(By.id("gbqfsa")).getLocation();  
  4. System.out.println("X Position : "point.x);  
  5. System.out.println("Y Position : "point.y);  
Note:
Makesure to import "org.openqa.selenium.Point;" package.

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...