user3843636
user3843636

Reputation: 13

Selenium Automation Test Case for Google Search

package com.test.webdriver;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestExample {
  private WebDriver driver;
  private String baseUrl;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.google.com";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testExample() throws Exception {
        driver.get(baseUrl + "/webhp?tab=ww&ei=PaDQU4j6N4-QuATW2oB4&ved=0CBMQ1S4");
        driver.findElement(By.xpath("id=gbqfq")).sendKeys("Test");
        driver.findElement(By.xpath("id=gbqfba")).click();
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
}

Can someone please tell me why the above code is not working? I am running the above quote in eclipse and getting bellow errors

Eclipse Version: 3.7.1 Firefox Version : 30

Upvotes: 1

Views: 2457

Answers (3)

Vignesh Paramasivam
Vignesh Paramasivam

Reputation: 2480

First, your xpath is incorrect, and the button you're trying to click is no more displayed, once you type something inside the textbox.

This will work,

driver.findElement(By.xpath("//*[@id='gbqfq']")).sendKeys("Test"); 
driver.findElement(By.xpath("//*[@id='gbqfb']/span")).click(); 

You're using incorrect path of a different button. Why?

bcoz, when you type something in google, the button you tried to click(Google button) will get disappeared, and totally a new page will come(you can notice that the textbox moves to the top right, and a blue search icon will appear).

So please notice what is happenning in the application, and then try to automate it.

Upvotes: 1

Zach
Zach

Reputation: 1006

Hi this should be working now I've left some comments in the Comment block Cheers.. Hope this helps see links that might help with locators

https://thenewcircle.com/static/bookshelf/selenium_tutorial/locators.html

http://www.w3schools.com/xpath/xpath_syntax.asp

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestExample {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
  driver = new FirefoxDriver();
  baseUrl = "http://www.google.com";
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}

  @Test
  public void testExample() throws Exception {
    driver.get(baseUrl + "/webhp?tab=ww&ei=PaDQU4j6N4-QuATW2oB4&ved=0CBMQ1S4");
    driver.findElement(By.xpath("//*[@id='gbqfq']")).sendKeys("Test");

    /**Comment 
    The element with id "gbqfba" becomes invisble as page nagivates to results/ suggestion page
    instead a button with id "gbqfb" becomes available to click to submit
    driver.findElement(By.xpath("//*[@id='gbqfba']")).click(); */

    //I decided to submit whatever is entered into the SearchField here and everything passes
    driver.findElement(By.xpath("//*[@id='gbqfq']")).submit();

    }

   @After
  public void tearDown() throws Exception {
  driver.quit();
  String verificationErrorString = verificationErrors.toString();
  if (!"".equals(verificationErrorString)) {
  fail(verificationErrorString);
  }
}
 }

Upvotes: 0

Robbie Wareham
Robbie Wareham

Reputation: 3448

Your xpath is invalid.Try:

"//*[@id='gbqfba']"

Most browsers will allow you to test Xpath.

Alternatively, the css selector would be;

"#gbqfba"

Or Selenium also allows you to find by id;

"gbqfba"

Personally I prefer the CSS selector. Any reason you have chosen XPath?

Upvotes: 0

Related Questions