New to Telerik Test Studio? Start a free 30-day trial
JavaScript Events
Invoking JavaScript Events
Telerik Testing Framework allows you to invoke JavaScript events directly with or without event arguments. For example:
C#
// Invoke the OnFocus event
HtmlButton b = Find.ById<HtmlButton>("b");
b.InvokeEvent(ScriptEventType.OnFocus);
// or
Actions.InvokeEvent(b.BaseElement, ScriptEventType.OnFocus);
// To invoke an event with an argument do something like this
HtmlTextArea area1 = Find.ById<HtmlTextArea>("area1");
Element body1 = Find.ById("body1");
MouseEvent me = new MouseEvent();
me.Type = "mouseover";
me.SetRelatedTarget(body1);
area1.InvokeEvent(me);JavaScript Event Handlers
The framework gives you the ability to attach a .NET event handler to your JavaScript happening in the browser. Start by defining your event handler like this:
C#
private volatile bool _clickHandled;
private System.Threading.AutoResetEvent _clickARE;
private void OnClick(object sender, JavascriptEventArgs e)
{
_clickHandled = true;
_clickARE.Set();
}All that's left is to attach the event handler to an element on the DOM like this:
C#
[TestMethod]
public void ClickHandler()
{
_clickARE = new System.Threading.AutoResetEvent(false);
_clickHandled = false;
HtmlButton b = Find.ById<HtmlButton>("b");
// Attach a listener to the click event on the button.
b.AddEventListener("click", OnClick);
// Invoke the event.
b.InvokeEvent(ScriptEventType.OnClick);
// Wait until the event is fired.
_clickARE.WaitOne(500);
Assert.IsTrue(_clickHandled);
}