Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
like image 819
Yash Avatar asked Feb 08 '12 21:02

Yash


People also ask

How do you inspect auto suggestions?

With the suggestions dropdown visible and DevTools opened, press F8 and switch to the DevTools. You will immediately break at the JS blur handler for the field, so the dropdown will not be removed, thus you'll be able to inspect its DOM.


1 Answers

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
like image 186
TesterFromA3rdWorldCountry Avatar answered Oct 24 '22 08:10

TesterFromA3rdWorldCountry