3

I'm just getting started on Selenium, so I'm still wrapping my head around all the moving parts. I've got my first test suite up and running, but can't for the life of me get jQuery event listeners to fire in IE.

This is especially problematic on dynamic AJAX dropdowns such as $('#country').live('change',showStates) select -> id=country | label=United States

The selenium driver triggers the events in response to the test script for all other browsers, but nothing I've tried causes or forces the change event to fire. I've tried using every method I could find documented on SO, including:

None of these commands fire the event in IE9, 8 (emulated) or 7 (emulated). FWIW It's unclear if these actually cause the event to fire in other browsers as the other browsers fire the change event without an additional call. I'm running the standalone selenium jar 2.33 on Windows 7.

How do I fire change events in IE with selenium?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RSG
  • 7,013
  • 6
  • 36
  • 51
  • Also changed Protected Settings per http://stackoverflow.com/questions/11157902/selenium-webdriver-and-internetexplorer?rq=1 – RSG Jun 03 '13 at 23:43

2 Answers2

1

From my experience triggering javascript/jQuery directly is not the safest way to go about things. Instead try use UI interactions to simulate a user. For example to trigger the 'country.change()' event, just change the country in the country field using Selenium (you may have to tab out of the field). I believe this is the intended use.

I have found some Drop-down Lists can be painful. I would try the following set of actions on your Drop-down List:

  1. Get the currently selected option (call a getText on the topmost element of the DDL)
  2. Click the DDL (to display the options
  3. Get a list of displayed options
  4. Discover the distance between the current option and the intended
  5. Send the appropriate number of DOWN or UP keys (depending on direction) followed by a ENTER or TAB key

I have had to follow this sort of process when working with a third party UI package that formats the DDL separately from where it handles the manipulation of the DDL. This means when Selenium is interacting with the element it is use to the intended effects do not occur because they are being handled elsewhere by javascript that is not with in the context of the element Selenium has the handle of. Sending keys mimics a users actions and forces the javascript to execute itself. Let me know if this is unclear and whether it works.

Nashibukasan
  • 2,028
  • 23
  • 37
  • thanks, I've tried tab and clicking away to other elements. Any chance you can post a working form example? `Select` elements seem to present the biggest problem. – RSG Jun 04 '13 at 02:22
  • I handle Select elements by utilising SendKeys and I follow these actions: click the select box, send the correct amount of DOWN or UP keys, send an ENTER or TAB. That should work. Though I never had an issue with Seleniums 'select' command either in the .NET bindings for WebDriver. – Nashibukasan Jun 04 '13 at 03:00
  • wow, tabbing out of a `select` is a gem! My issue was actually getting a select change event to fire using ChromeDriver, not IE, but you've sorted it for me :) – StackExchange What The Heck Jun 11 '13 at 15:48
  • Happy it helped @yochannah. Did you try my above suggestion @RSG? – Nashibukasan Jun 12 '13 at 00:49
0

Fire the change event by performing the actions that would trigger it. That is a fundamental part of the WebDriver philosophy.

If your dropdown is implemented with standard <select> and <option> tags, you can use the Select utility class.

WebElement country = driver.findElement(By.id("country"));
Select selector = new Select(country);
selector.selectByVisibleText("Lithuania");

This won't trigger a change unless the new option is different, saving you some conditional logic as well.

Joe Coder
  • 4,498
  • 31
  • 41