Pages

Saturday, August 10, 2013

How to verify Google Search List Options using Selenium RC/ Webdriver

Below is the snippet for list the google search list options using both Selenium RC and web driver


package selenium;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.Selenium;

public class GoogleTests {
    static WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        driver = new FirefoxDriver();
        driver.get("http://www.google.co.in");
        driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Selenium webdriver");
         // Selenium Code to print all the options from the dropdown suggestionlist
        Selenium selenium = new WebDriverBackedSelenium(driver,
                "http://www.google.co.in");
        Thread.sleep(2000);
        System.out.println("Below list is printed by Selenium RC");
        int listCount = selenium.getXpathCount(
                "//table[@class='gssb_m']/tbody/tr").intValue();
        for (int i = 1; i < listCount; i++) {
            String listValues = selenium
                    .getText("//table[@class='gssb_m']/tbody/tr[" + i
                            + "]/td[1]/div/table/tbody/tr[1]/td[1]/span");
            System.out.println(listValues);
        }
        // Webdriver Code to print all the options from the dropdown suggestion list
        System.out.println("Below list is printed by Webdriver");
        List<WebElement> options = driver.findElements(By
                .xpath("//table[@class='gssb_m']/tbody/tr"));
        for (WebElement webElement : options) {
            System.out.println(webElement.getText());
        }
        // driver
        driver.close();
        driver.quit();

    }

}