Yash
Yash

Reputation: 81

Test autocomplete with Selenium webdriver

I have a text box in which when I type one letter say 's' , it displays a list of results ( like google search) .

I am using latest selenium webdriver with java.

I have tried

sendKeys("s"),

JavascriptLibrary jsLib = new JavascriptLibrary();

jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onkeyup");

    jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onblur");

    jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onclick");

    jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onmouseup");


driver.findElement(By.id("assetTitle")).sendKeys(Keys.ENTER);

None of these work even after adding wait after each of the steps.

Any suggestions?

Thanks.

Update :-

WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com");
    WebElement query = driver.findElement(By.name("q"));
    query.sendKeys("s");
driver.findElement(By.xpath("//table[@class='gssb_m']/tbody/tr/td/div/table/tbody/tr/td/span")).click();
    driver.findElement(By.name("btnG")).click();

Update 2 : -

WebDriver driver = new FirefoxDriver();
    driver.get("http://www.kayak.com/");
    WebElement query = driver.findElement(By.name("destination"));
    query.sendKeys("s");

Update 3 :- I tried with Selenium 1 and the fireevent method works by passing parameter as 'keydown'. This should be a temporary workaround for now.

WebDriver driver = new FirefoxDriver();
    driver.get("http://www.kayak.com/");
    DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");

    sel.type("//input[@id='destination']", "s");
    sel.fireEvent("//input[@id='destination']", "keydown");

Upvotes: 8

Views: 20633

Answers (3)

I found a workaround about this. My problem was:

  1. Selenium inputted 'Mandaluyong' to an auto-suggest location field
  2. The auto-suggest field appears together with the matched option
  3. Then selenium left the auto-suggest drop-down open not selecting the matched option.

What I did was:

        driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
        driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);

This is because on a manual test, when I try to press TAB key, two things were done by the system:

  1. Picks the matched option from the auto-suggest drop-down
  2. Closes the auto-suggest drop-down

Upvotes: 3

some_other_guy
some_other_guy

Reputation: 3404

I believe you are testing auto-suggest here (not auto-complete)

Steps I follow -

  1. Enter something in the input field
  2. Click on the suggestion you want to choose. (You can find the xpath using some tools like Firebug with Firepath, Chrome, etc.)
  3. Verify the text in the input field is same as expected.

Upvotes: 1

Yash
Yash

Reputation: 81

This should be a temporary workaround for now.

WebDriver driver = new FirefoxDriver();
    driver.get("http://www.kayak.com/");
    DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");

    sel.type("//input[@id='destination']", "s");
    sel.fireEvent("//input[@id='destination']", "keydown");

Upvotes: 0

Related Questions