Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to fire jQuery change() event on selectlist from WatiN

Tags:

I have a select box

 <select id="myselectbox">    <option value='a'>First option</option>    <option value='b'>Second option</option>  </select> 

And jquery

 $('#myselectbox').change(function() {     ...  }); 

And a WatiN test

 SelectList list = Browser.SelectList(Find.ById("myselectbox"));  list.Select("First option"); 

In IE, this changes the select box, but doesn't fire the jquery event handler. I am aware of the many issues surrounding the change() event handler in IE. So I have tried adding a number of things to the test to force the event to fire:

 list.Blur();  list.Keypress('\r');  list.Click(); 

I also tried to click outside the select box to remove the focus, hoping this would fire the event.

Any ideas?

like image 340
fredw Avatar asked Sep 14 '10 20:09

fredw


2 Answers

You could manually fire the event:

$('#myselectbox').change(); 

The other JavaScript events you're firing don't change the value, so they won't fire the change event.

like image 161
Matt Ball Avatar answered Oct 15 '22 12:10

Matt Ball


Going off of Matt Ball's answer and the OP's additional steps, I added the following extension method to a test helper class.

using WatiN.Core;  namespace Some.Test.Project {     public static class WatiNExtensions     {         public static void ForceChange(this Element e)         {             e.DomContainer.Eval("$('#" + e.Id + "').change();");         }     } } 

Now, so far anyway, I've succeeded in doing:

SelectList list = Browser.SelectList(Find.ById("myselectbox")); list.Select("First option"); list.ForceChange(); 

OK, it's a minor convenience at best, but I'm finding it useful. Anyway, upvotes from me for the OP and the answer!

like image 31
Rex Miller Avatar answered Oct 15 '22 11:10

Rex Miller