Automate Google.com using Selenium Webdriver

In this post you will learn to automate Google Search using Selenium Webdriver with Java. This is a real time example of Explicit wait command. Google search is an ajax call. We will handle this ajax call using Explicit wait command of selenium Webdriver.





What is ajax?
Ajax stands for Asynchronous Javascript and XML. Ajax is a new technology for creating for faster and more interactive web applications. What ajax does is:
  • Update a web page without reloading/refresh it
  • Read data from the server after the page is loaded
Google search is a best example of ajax. In case of google search, you simply type keyboard in search bar and just below the search bar a suggestion box with matching suggestions appears instantly, this is ajax. We can handle ajax in Selenium webdriver with the use of Explicit Wait command. I have shared below the code for automating Google search. But I recommend to execute below test case manually first and then automate it.

Assignment - Print all the options displayed in Google suggestion box

Test Steps:
  1. Launch the Firefox.
  2. Open URL - http://www.google.com
  3. Enter keyword "techlistic" in search bar
  4. Wait for ajax suggestion box to appear
  5. Get/store all the options of suggestion box in a list
  6. Print all the suggestions one by one.
Code:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class AjaxGoogleSearch {

 WebDriver driver;

  @BeforeMethod
  public void start(){
    driver = new FirefoxDriver();
  }

 @Test
 public void testGoogleSearch() {
    // Open google.com
    driver.get("http://www.google.com");
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    
    // Enter "techlistic selenium tutorials" in google search box
    driver.findElement(By.name("q")).sendKeys("selenium tutorials techlistic");    
    // Wait for suggestions box to be appear for 20 seconds
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.className("sbdd_b")));
    
    WebElement list = driver.findElement(By.className("sbdd_b"));
    List rows = list.findElements(By.tagName("li"));
    
    // Iterate over suggestions box and print suggestions one by one
    for (WebElement elem : rows) {
        System.out.println(elem.getText());
    }
 }
}

Happy Learning!

Assignment 3- Automate Practice Form <<  Previous  ||  Next  >> Assignment 5- Automate User Registration


Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

  1. Here List will be along , it passes the test with correct locator of ajax suggestion box - sbtc, but unable to print the text which displays using for loop. can u please suggest. My code is as below:
    package techlisticDotCom;


    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.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;

    /**
    * Test to automate:
    * Launch the Firefox.
    * Opens google.com
    * Type "selenium tutorials techlistic" in search bar
    * Wait for ajax suggestion box to appear
    * Print all the suggestions one by one
    *
    */
    public class AjaxGoogleSearch {

    WebDriver driver;

    @BeforeMethod
    public void start() {
    System.setProperty("webdriver.chrome.driver","C:\\Tanu Docs\\TS\\AUTOMATION PRACTICE\\AllDownloadedFiles\\chromedriver76_win32\\chromedriver.exe");
    driver=new ChromeDriver();
    }

    @Test
    public void testGoogleSearch() {
    //open google
    driver.get("https://www.google.com");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    //enter techlistic tutorials in search box
    //driver.findElement(By.cssSelector("input#fakebox-input")).sendKeys("selenium tutorials techlistic");
    driver.findElement(By.name("q")).sendKeys("selenium tutorials");

    //wait for suggestions
    WebDriverWait wait=new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.className("sbtc")));

    WebElement list=driver.findElement(By.className("sbtc"));
    List rows=list.findElements(By.tagName("li"));

    for(WebElement elem:rows) {
    System.out.println(elem.getText());
    }



    }


    }

    ReplyDelete
    Replies
    1. after List i added WebElement inside greater and less than signs, which auto gets removed from above code...

      Delete
  2. Code is not running at all, just opening web browser adn entering search keyword and stopping, not showing any error also in console.
    public class GoogleSearchAjax {

    public static void main(String[] args) {
    WebDriverManager.chromedriver().setup();
    WebDriver driver=new ChromeDriver();
    String URL="https://www.google.co.in/";

    driver.get(URL);
    driver.findElement(By.name("q")).sendKeys("Uday");
    WebDriverWait wait=new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.className("sbct")));
    WebElement list=driver.findElement(By.className("sbct"));
    List rows=list.findElements(By.tagName("li"));

    for(WebElement elem: rows) {
    System.out.println(elem.getText());
    }
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate Browser Actions on Godaddy.com using Selenium

Automate E-Commerce Website using Selenium Webdriver