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:
Code:
Assignment 3- Automate Practice Form << Previous || Next >> Assignment 5- Automate User Registration
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
Assignment - Print all the options displayed in Google suggestion box
Test Steps:
- Launch the Firefox.
- Open URL - http://www.google.com
- Enter keyword "techlistic" in search bar
- Wait for ajax suggestion box to appear
- Get/store all the options of suggestion box in a list
- Print all the suggestions one by one.
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
Feel free to ask queries or share your thoughts in comments or email us.
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:
ReplyDeletepackage 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());
}
}
}
after List i added WebElement inside greater and less than signs, which auto gets removed from above code...
DeleteCode is not running at all, just opening web browser adn entering search keyword and stopping, not showing any error also in console.
ReplyDeletepublic 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());
}
}