Tuesday, October 28, 2014

Selenium automation quick start - Automate Google Search

In this quick tutorial I'll show you how to use Selenium Firefox Driver to Automate Google Search
When running this simple application we hope to get below output without user interaction.

1. Firefox will be opened automatically
2. The text "fast cars" will be inserted in to search box automatically
3. Search form will be submitted
4. Search result page will be displayed

Ok lets start.
1. Create a new java project and name it as SeleniumLearn
2. Download the Selenium library from Selenium Download Page and add to the project
3. Add a new Java class and name it as SeleniumExample
4. Modify the class as below.

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

public class SeleniumExample  {
    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();

        driver.get("http://www.google.com");

        //The name of the google search text box is "q". 
        //You can find this by looking at the html source of the google search page.
        //Webdriver can find the search text box by that name as below
        WebElement searchBox = driver.findElement(By.name("q"));

        //Enter the search text
        searchBox.sendKeys("fast cars");
        //Below command will submit the form to which the searchBox belongs
        searchBox.submit();
    }
}

5. Now run this file and you will see the desired output.

No comments:

Post a Comment