Automate Google Search with 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
When you write something into a Search Box say "app" and it shows you similar search options that is ajax. And you can automate that auto suggestion box with the help of Explicit command in Selenium Webdriver.

Google search is a best example of ajax. In case of google search, you simply type any keyword 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 for better understanding.

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 "selenium tutorial 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.
  7. You can also click on any of the suggestions using the click() command.
Example Code:
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 {

	public static void main() {

	// Set Driver path
	System.setProperty("webdriver.chrome.driver", "C:\\AUTOMATION\\chromedriver.exe");
	WebDriver driver=new ChromeDriver();

	//open google
	driver.get("https://www.google.com");
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

	//enter techlistic tutorials in search box
	driver.findElement(By.name("q")).sendKeys("selenium tutorial techlistic");

	//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());
	}
	}
}

Code Explanation:
  • Set Chromedriver executable path.
  • Initialize Webdriver chrome browser instance.
  • Get Url.
  • Set Implicit Wait of 30 secs, it means selenium will throw exception and test case would fail if any element won't be visible to selenium within 30 secs.
  • WebDriverWait is Explicit Wait in Selenium. It is used to handle ajax calls. We also call them dynamic elements.
  • We have given 20 secs to wait for search options.
  • And we are using object of WebDriverWait class to call until() method to set a condition for our wait. That's why we also call Explicit Wait as Conditional Wait.
  • Inside until(), ExpectedConditions class is used, this class has plenty of conditional methods to be used. We have used presenceOfElementLocated.
  • Now, we created a list where we are storing all the 'li' element values, means we are storing all the search suggestion values.
  • And in the end, using for each loop to print all the elements stored in list.
Note - Please note that Google continuously change it's page elements so please be aware that the elements used in above example code might be changed while you are automating google search. So, please make sure that you are using the correct page elements.

Happy Learning!

Assignment 2- Automate Practice Form <<  Previous  ||  Next  >> Assignment 4- Automate User Registration, Buy Product and Search Functionality on e-Commerce Website

Follow Us

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

Comments

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Automate an e-Commerce website with Selenium Webdriver

How to Handle Dynamic Web Table in Selenium WebDriver

Top 50 Selenium Webdriver Interview Questions

Automate Browser Actions and Menu links with Selenium Webdriver

Top 25 Selenium Webdriver Commands for Test Automation