views:

1225

answers:

6

I have a test case that requires typing in a partial value into an ajax based textfield and verifying the list has the expected content. If it does, select the content. Any idea how to make this work?

+2  A: 

Your question is slightly ambigious.

Most browsers keep a value cache that is based on the name of the field: This is the value that is being suggested as autocompletion by your browser even though you may never have visited the site before. This feature is non-standard across all browsers, and there's going to be no standard way for selenium to detect/analyze this. You can still do it, but you'll have to make javascript functions that determine the values yourself. Then you can use "eval" in selenium to execute these functions. I have not seen any js libraries that can tell you these values in a cross-browser compatible way.

The other alternative is that you use ajax to do a server-side submit of the partially entered value. In this case it's just a matter of typing the values into the textbox and asserting that the expected values turn up. Normally the autocomplete suggestions show up in some layer on the client side.

krosenvold
I see why my question could be ambiguous but what i was driving at is in the second part of your answer. In my case, i have an ajax-based textfield that provides user with options based on partial entered value. Can you use selenium to capture these options and verify the expected value is in it?
Afamee
+3  A: 

I'd do this as follows:

  • type to enter the value in the text field.
  • waitForTextPresent or verifyTextPresent to check the autocomplete content
  • click or mouseDown to click on the item in the autocomplete list

The trick is going to be making the final click be just in the right place. You should be able to use an XPath expression that searches for the text you're expecting to find it.

Dave Webb
+4  A: 

The type command may not be enough to trigger the autocomplete. Dave Webb's suggestions are otherwise spot on. My only addition would be that you might need the typeKeys command, which causes slightly different JavaScript events to be fired, which may be more likely to trigger the autocomplete widget.

Shameless plug: if you were a BrowserMob load testing customer, you could get free answers to these questions from our support team ;)

Patrick Lightbody
Good point about type and typeKeys.
Dave Webb
A: 

Please use typeKeys instead of type. Also use mouseDown instead of click. It works fine.

A: 

I found I needed to do a focus on the field before doing typeKeys to get it to work.

johnnymire
A: 

I recently wrote a HOWTO on this very topic - using Selenium to test an AJAX-driven JQuery autocomplete menu:

link text

Cheers, Jason

Jason Buberel