Saturday, May 31, 2014

Use of all locators

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AllLocators {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in/?gfe_rd=cr&ei=1fOJU_eBNKjM8gfOsYCAAw");
       
        WebElement useClass = driver.findElement(By.className("gbqfba")); //By.className()
        System.out.println("using className, text is :"+useClass.getText());//Google Search
       
        WebElement useLinkText = driver.findElement(By.linkText("Gmail")); //By.linkText() method can be used only with 'a' tag
        System.out.println("using linkText, text is :"+useLinkText.getText());//Gmail
       
        WebElement usePartialLinkText = driver.findElement(By.partialLinkText("Gmai")); //By.partialLinkText() method can be used only with 'a' tag
        System.out.println("using Partial linkText, text is :"+usePartialLinkText.getText()); //Gmail
       
        List<WebElement> tagName = driver.findElements(By.tagName("a")); //By.tagName(), this will give all the webelements which have tagname as 'a'
        System.out.println("using tag name, number of 'a' tag is :"+tagName.size()); //this will print the number of 'a' tag in the web page
       
        WebElement id = driver.findElement(By.id("gbqfba"));//By.id(), this is the fastest locator in performance, google search button
        System.out.println("using id, text is :"+id.getText());//Google Search
       
        WebElement cssSel = driver.findElement(By.cssSelector("span[id='gbqfsa']"));
        System.out.println("using css Selector, text is :"+cssSel.getText());//Google Search
       
        WebElement xpath = driver.findElement(By.xpath("//span[@id='gbqfsa']"));//By.xpath()
        System.out.println("using xpath, text is :"+xpath.getText());//Google Search
       
        WebElement name = driver.findElement(By.name("btnK")); //By.name()
        System.out.println("using name, text is :"+name.getText());//Google Search
       
        driver.close();
        }
}

No comments:

Post a Comment