With WatiN, I would like to wait for the jQuery event

I hope someone out there did it. I have some custom jQuery modifications that use the jQuery event system to fire certain events for processing. I would like to be able to do automated testing in response to this.

Has anyone worked on a better binding from WatiN to jQuery? I saw several posts in the transition for jQuery selectors, as well as a message here about waiting for this text to change ... It would be great if someone added jQuery selection and event support ... even document.getElementsBySelector could be nice.

+7
source share
3 answers

I do not know if this was what you expect, but I had time to study, and I came up with a solution to the question: with WatiN, I would like to wait for the jQuery event.

The goal is to write in C #:

Browser.WaitForEvent("#link", "onclick"); 

I wrote a test page (available at http://www.sp4ce.net/debug.html ), where it triggers the onclick event in the link when you click on the button.

Then I wrote an extension method .WaitForEvent

 public static void WaitForEvent(this Browser browser, string selector, string eventname) { using (EventMonitorWatcher monitor = new EventMonitorWatcher(browser, selector, eventname)) { int timeWaited = 0; var maxTimeWait = Settings.WaitForCompleteTimeOut * 1000; do { Thread.Sleep(Settings.SleepTime); timeWaited += Settings.SleepTime; } while (!monitor.IsEventCalled() && timeWaited < maxTimeWait); } } 

This method uses an EventMonitorWatcher , the verification of which is triggered by an event.

 public class EventMonitorWatcher : IDisposable { private readonly Browser _browser; private readonly string _selector; private readonly string _eventname; public EventMonitorWatcher(Browser browser, string selector, string eventname) { _browser = browser; _selector = selector; _eventname = eventname; _browser.Eval(string.Format("startEventMonitor('{0}', '{1}')", _selector, _eventname)); } public bool IsEventCalled() { string result = _browser.Eval(string.Format( "isEventCalled('{0}', '{1}')", _selector, _eventname)); return result == "true"; } public void Dispose() { _browser.Eval(string.Format("stopEventMonitor('{0}', '{1}')", _selector, _eventname)); } } 

This monitor uses three javascript (jquery powered) methods that start monitoring, stop monitoring and check if an event has been triggered.

 startEventMonitor = function(selector, event) { $(selector).data('eventMonitorResult', false); var eventMonitorHandler = function() { $(selector).data('eventMonitorResult', true) }; $(selector).data('eventMonitorHandler', eventMonitorHandler); $(selector).bind(event, eventMonitorHandler); }; stopEventMonitor = function(selector, event) { $(selector).undbind(event, $(selector).data('eventMonitorHandler')); $(selector).data('eventMonitorResult', false); }; isEventCalled = function(selector, event) { return $(selector).data('eventMonitorResult'); }; 

Here you are, then you need to enter this javascript as a line of your page when the browser starts.

 [Test] public void Test() { using(var browser = new IE("http://www.sp4ce.net/debug.html") { browser.Eval(JavaScript); browser.WaitForEvent("#link", "onclick"); } } 

In this test, when you click the "click" button, the test should end directly and your browser will close;

+7
source

Why not add some javascript that will be attached to the events you want to listen to. In the javascript event code that is raised when the event (s) is triggered, you can add an element to the DOM with the results that interest you. Create a Div for the example and set innerHtml using the output you want to check. Give the Div a unique identifier.

Main stream:

  • browser.RunScript (javascript_here_to_attach_to_the_events_you_are_interested_in) Suppose that when the event fires, your code you enter creates a Div with Id = "my_event_result".

  • Perform actions that run your validation code (for example, entering good / wrong values) Example: browser.TextField (textFieldIdWithValidation) .Typetext ("some value");

  • Wait for the Div: browser.Div ("my_event_result") WaitUntilExists (); to appear.

  • Approve the value (s) in the div: bool result = browser.Div ("my_event_result"). Text == "Expected Test";

NTN, Jeroen

+3
source

I do not expect this to be the right answer, since I do not have time to check WatiN, just a couple of notes that will help you do your job, jQuery Monkey patch 1:

since all jQuery events are fired using a function trigger.

 var _trigger = jQuery.prototype.trigger; jQuery.prototype.trigger = function () { _trigger.apply( this, arguments ); alert( Array.prototype.join.call( arguments, '-' ) ); } 

This will make your page warn all arguments given every time something is triggered (like a line with a '-' as a separator), usually the first argument will be an event. WatiN should have some kind of function that will catch warnings, if I do not propose to abandon it and go with watij or something like that, I recommend HTMLUNIT

Also, if you are interested in what triggered the event, inside the function above, this will point to this:

Material you should check:
http://api.jquery.com/event.result/
http://api.jquery.com/event.data/

0
source

All Articles