New to Telerik Test Studio? Download free 30-day trial

Invoking Javascript

Telerik Testing Framework supports directly invoking JavaScript functions from your .NET test code. One key advantage to this support is that you can use it to do your JavaScript unit testing without having to integrate yet another unit testing framework into your development environment. You can also avoid having to learn/support this additional framework. Abilities include:

  • Invoke JavaScript functions and get return values.

  • Handle JSON objects returned after invoking a JavaScript function.

  • Invoke script events directly on a page element. (e.g. OnBlur(), OnMouseOut(), etc)

  • Attach .NET event handlers to actual JavaScript events.

  • Perform logging/tracing of your JavaScript routines. The logging is done directly into the unified log you are using.

Invoking JavaScript Functions

The Actions.InvokeScript() enables you to simply provide a JavaScript function name to invoke. For example:

// Assume our page has JavaScript method Test1() & Test2() & Test3()

// Call a JavaScript function.
Actions.InvokeScript("Test1()");

// Call a JavaScript function with parameters
// NOTE: It is important to enclose literal string parameter with double quote and not single
// quotes. IE will fail if you don't do so.
Actions.InvokeScript(@"Test2(""WebAii"", 4)");

// Call a JavaScript function and get its return values
string jsRetValue = Actions.InvokeScript("Test3();");

// Call a JavaScript function that returns a value
int val = Actions.InvokeScript<int>(@"Test3(""Bear"", ""Utah"")");
' Assume our page has JavaScript method Test1() & Test2() & Test3()

' Call a JavaScript function.
Actions.InvokeScript("Test1()")

' Call a JavaScript function with parameters
' NOTE: It is important to enclose literal string parameter with double quote and not single
' quotes. IE will fail if you don't do so.
Actions.InvokeScript("Test2(""WebAii"", 4)")

' Call a JavaScript function and get its return values
Dim jsRetValue As String = Actions.InvokeScript("Test3();")

' Call a JavaScript function that returns a value
Dim val As Integer = Actions.InvokeScript(Of Integer)("Test3(""Bear"", ""Utah"")")

Here is a code which calls an Angular function against input field:

Actions.InvokeScript("angular.element(" + element.ClientSideLocator + ").val('" + text.Replace("'", "\\'") + "').triggerHandler('input')");

JavaScript Unit Testing Using NUnit or Visual Studio Team Test

Below is an example of how you can define JavaScript unit tests that is consistent with the rest of your tests and manages their execution and reporting using the same unit testing infrastructure, whether it is NUnit or Visual Studio Team Test.

[TestMethod]
public void JsUnitTest1()
{
    // return 'true' for pass or 'false' for fail.
    string strBool = Actions.InvokeScript("JsUnitTest1()");
    Assert.IsTrue(bool.Parse(strBool));
}
<TestMethod()> _
Public Sub JsUnitTest1()
    ' return 'true' for pass or 'false' for fail.
    Dim strBool As String = Actions.InvokeScript("JsUnitTest1()")
    Assert.IsTrue(Boolean.Parse(strBool))
End Sub

See also

In this article