0

So I have been trying to resolve this for several hours. I have no clue what I am doing wrong.

This is a type ahead field I am looking in is <input type="text" id="id_attendees" name="attendees">. When I type in there a js dropdown is created. When I press the Down Arrow on keyboard it works fine and selects the top choice. When I do keyDown --- id=id_attendees --- \40 in IDE it works fine and also selects the choice.

I cannot get it to do the same in Java webdriver though

Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN);

^doesn't work.

driver.findElement(By.id("id_attendees")).sendKeys(Keys.ARROW_DOWN);

^doesn't work

I tried Keys.DOWN in both cases, that doesn't work either. I created a literal String altm = "\u0040"; and all that does is type an @ symbol.

I also tried a bunch of other things as well and nothing is working. I have no clue what am I missing.

EDIT 1:

@Roddy Thank you! - Given that link I added the following that did work (after importing DefaultSelenium and WebDriverBackedSelenium.

DefaultSelenium sel = new WebDriverBackedSelenium(driver,vars.siteurl);
sel.fireEvent("//input[@id='id_attendees']", "keydown");

EDIT 2: --> DOH that doesn't work. I got overzealous apparently.

StanM
  • 827
  • 4
  • 12
  • 33
  • 1
    Probably has something to do with the JavaScript that is populating/creating the drop-down not being triggered by your input (and therefore there not being anything to arrow down *to*.) May want to look at [this question](http://stackoverflow.com/questions/9202061/test-autocomplete-with-selenium-webdriver) as well. – Roddy of the Frozen Peas Aug 13 '12 at 20:05
  • thanks! That at least made it work, will have to keep in mind next time. Do you have any idea on how to avoid it in the first place from a programmer perspective? – StanM Aug 13 '12 at 20:13
  • In general, if there is some sort of JavaScript functionality in whatever you're testing, you want to confirm that your testing logic actually *triggers* the JavaScript in the first place. Validation, callbacks, auto-completes, and forms are usually places where this sort of thing might occur, so it's usually a good idea to take a look at the JS and see how it gets triggered and formulate your test accordingly. – Roddy of the Frozen Peas Aug 13 '12 at 20:20

3 Answers3

0

some time scripts takes some time to load the list so need to add wait,

WebElement ar=driver.findElement(By.id("id_attendees"));
Thread.sleep(1000);
ar.sendKeys(Keys.ARROW_DOWN);
Chetan
  • 2,360
  • 3
  • 18
  • 33
0

I think your use of Actions is not quite right.

The implementation is a builder pattern. Calling sendKeys doesn't send the event, it only stages the event to be fired when you call perform. Note that the return value of sendKeys is an Actions instance.

Actions actionObject = new Actions(driver);
actionObject = actionObject.sendKeys(Keys.ARROW_DOWN); //ASSIGN the return or you lose this event.
actionObject.perform();  //Should do what you want.  Note that this will reset the builder.

Hope that helps.

Jeremiah
  • 1,145
  • 6
  • 8
0

With Actions class, after defining what it will do for you, you need to first build() it. So in your case it would be like this:

Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN).build();

When you want your script to execute that action, you need to perform() it. You can chain it right after your build() method (if you are using it just once, for example) or later in your code whenever you need it, like this:

actionObject.sendKeys(Keys.ARROW_DOWN).build().perform();

OR

actionObject.perform();

Good luck!