Testing autocomplete fields with Selenium

For quite a while I struggeled with testing autocomplete fields (like the one from the script.aculo.us library) using Selenium tests.

As you may already know – the “type” command does not trigger the autocomplete to look for suggestions. You rather have to use the keyUp/keyDown/keyPress command. Unfortunately their behaviour with autocomplete varies between different browsers. All this makes it a real mess (and complicated looking tests) to test a simple autocomplete field.

The solution I found out about recently is to use the “typeKeys” command which seems to be a new comfort feature of Selenium. It triggers all the above key handlers for you.

However – I noticed that sometimes it is helpful / necessary to use “typeKeys” in combination with the “type” command – to get working results. For now – I am using “type” to enter the field value leaving out the last character to be inserted by “typeKeys”.

Advertisements
This entry was posted in Selenium and tagged , . Bookmark the permalink.

20 Responses to Testing autocomplete fields with Selenium

  1. ccanestro says:

    Hi,
    I have the same problem, trying to test an autocomplete field then select from the drop down menu and fill the field. Was you able to do it? Could you please be a bit more specific? I will appreciate all the help and taking in account that I am just starting to learn selenium. Thanks in advance for your help

    C.

  2. mrgierer says:

    I am using these steps for testing an autocomplete field:

    $this->type(“iddepartment”, “spor”);
    $this->typeKeys(“iddepartment”, “t”);
    for ($second = 0; ; $second++) {
    if ($second >= 60) $this->fail(“timeout”);
    try {
    if ($this->isTextPresent(“entry #1”)) break;
    } catch (Exception $e) {}
    sleep(1);
    }

    $this->click(“//li[contains(text(), ‘entry #1’)]”);

    Note the usage of “type” and “typeKeys” in combination, which gave the best results to me.

  3. les says:

    I faced a similar issue with autocomplete – in my case the autocomplete was not coming up even when i typed into the fields manually after stopping the script in debug mode.
    In the normal mode ie without selenium autocomplete is working fine.

  4. Ganesh says:

    I do no see this “typeKeys” method in the selenium javadoc that i have. Is it present in an advanced version of selenium.

  5. mrgierer says:

    Please find the “typeKeys” command in the Selenium Reference: http://seleniumhq.org/projects/core/reference.html

  6. Luiz says:

    Thank you. It helped me a lot!

    But in my case I needed to set an interval between the keystrokes to activate the auto-completion. I used only “typeKeys” because your solution didn’t work for me.

    Anyway, thanks a lot!

  7. mala says:

    I am successful in getting the drop down but once I click on an item in the drop down, the list does not go away. I have tried the click command, mouse down, I have added pause.

    Any ideas?

  8. Guru Padman says:

    mala,

    You need to use mouseDown instead of click.
    Then it works fine without any problem.

    Please try and comment!

  9. GS says:

    I used this code to select the ajax populated list and it seems to have worked for me in IE.

    selenium.type(…)
    selenium.typeKeys(“”)

    Click the auto populated list, like so,

    selenium.click(“//div[@id='<your id'] /ul / li[1]")

  10. Rachel says:

    Hi!

    The combination of “type” and “typeKeys” really works! Selenium was driving me crazy!

    THANKS FOR SHARING IT

    😀

  11. This is really helpful.

    I had to use mouse Over and press enter to select an item from the list (suggestions)

    mouseOver
    //li[@class=”ui-menu-item”]/a

    keyPressAndWait
    ctl00_cphContent_clCategory_txtContentValue
    \13

  12. ajeeshvl says:

    It really helped me…

  13. srinath says:

    Thanks, It helped me a lot . works good

  14. tols says:

    Guys thanks for this. i got it working using the following sequence

    selenium.type(…) – without the value
    selenium.typeKeys(“”) – with the value

    MouseOver – Your Search panel
    ctl00_cp1_ucSearchPanel3_txtSmartSearch

    click – On your target
    //li[@id=’AirportCode–SSH’]/div

  15. amit says:

    @tols : Your suggestion worked for me.

    However since I use selenium.typeKeys(‘ ‘) , it types in a blank space after my input in order to make trigger the Ajax Call.
    For the record, we use YUI 2.8.0 r4 [http://developer.yahoo.com/yui/2/] & DWR [http://directwebremoting.org/dwr/index.html].

    Also http://blogs.atlassian.com/developer/2007/08/selenium_is_the_pain_worth_it.html seems to have a similar combo [yui & dwr]

  16. Sarah says:

    Thank you, this was really helpful!

  17. jessen says:

    I can’t select from an autocomplete drop down even though i used every possible commands
    I tried click,mouseDown,mouseOver i even tried focusing the field
    I have no issues triggering the autocomplete the selection of the results is the problem

  18. prem says:

    Hai All,
    Is it possible to use regular expression for identifying locators in the place of id/name.

    Thanks in Advance
    Prem

  19. Arul Baskar says:

    hi
    Selenium ide record autocomplete textbox enter text list of items show any one list item click not working and typekeys use list of records show. Refer to this http://stackoverflow.com/questions/5543724/selenium-how-to-select-an-object-by-class

    command: mouseover,click
    target: xpath=//li[contains(@class, ‘ui-autocomplete’)]/li[1]/a this not working

    mouseOver
    //li[@class=”ui-menu-item”]/a

    please help me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s