Follow Us

This blog assumes that you have a prior understanding of Coded UI Test and some experience with Recording and Playback on either Internet Explorer or Cross Browser. A good starting point for Cross Browser testing would be here. This blog explains the new “JavaScript Execution” capability of the Coded UI test on Internet Explorer as well as Cross Browser that we added in VS Update 2.

With Update 2, Users will be able to run JavaScripts on the webpages they are testing. This will help the users in achieving certain complex actions or using custom controls that are otherwise difficult or entirely not possible due to the limitations of the web browser or coded ui test or any other issues. This API will also serve as an alternative to the actions that take relatively long time to execute in comparison with running a simple JavaScript. It might also simplify the test code if multiple steps are required in achieving the desired effect without JavaScript..

Selenium NET Bindings and the Selenium ChromeDriver made available to you through an installer on Visual Studio Gallery.

API Definition and Usage:

Upon Installation of the VS Update 2, this feature will automatically be available as API (mentioned below) in BrowserWindow Class.

object ExecuteScript(string script, params object[] args)

I hope that the readers of this blog are already acquainted with the BrowserWindow Class. BrowserWindow class is the type of the window objects that get generated if you record on internet explorer using Coded UI Test. You can refer to the Cross Browser Scenario videos mentioned in this link for Firefox or Chrome. If you are either using coded ui test builder to generate code or hand coding the test, then you should have access to the window object of the browser, which is of type BrowserWindow. For Handcoding coded UI Tests, you can refer to this blog.

You can call the “ExecuteScript” method using the window object. ExecuteScript takes the script as first argument and any optional arguments to the script, if any. If there is a return value expected, then the API will return that value, otherwise it will return null.

Script Syntax:

Script Syntax follows the ECMA standards and no further editing is required. However, if you want to return a value from the script, you will have to use the return keyword at the end of the script. If you add a return at the middle of script, then any script code after the return value statement will not be executed. Reason for this behavior is due to the fact that “ExecuteScript” cannot know which value to return from the script, unless explicitly told. JavaScript can be any complex script with hundreds of lines containing variable definitions, function definitions, function calls etc. To make the decision of return value easier, “ExecuteScript” will return the value based on the return keyword. Please note that the usage of “return” keyword inside function definitions is not affected due to this.

Script can directly call any JavaScript functons that are already defined inside the web page. Any Script errors will result in UITestJscriptExecutionException.

Arguments Usage:

Arguments for the script can be variables that you cannot directly embed in the script or based on dynamically changing variables. Arguments types that can be sent to the API are “double”, “long”, “bool”, “string”, “HtmlControl” or a List of objects where each object can be any one of the aforementioned types. Script should specify arguments as per ECMA standards. “ExecuteScript” supports the same return types as the argument types. we also support Nested Lists, but we suggest shallow nest depth. If an HTML element that you are trying to return is no longer available after script execution and before script can return the said control, either due to page navigation or dynamic controls, then the API will return NULL and will not throw UITestControlNotAvailableException. Reason being that the Coded UI Test cannot rollback the executed script just because of one or few controls of dynamic nature.

Note the usage of return keyword so that the function result can be returned back to the test. Script will get executed as if the arguments[i] are substituted with the corresponding argument value. Any incorrect arguments or bad argument indexes will result in an UITestJscriptExecutionException.

Usage Examples:

All the below examples assume that “browserWindow” is the browser window object of your Coded UI Test.

Example 1 – Note here that the ExecuteScript API does not support Int, and only supports long:

Example 2 – ExecuteScript can return the specific HtmlControls from the JavaScript and one can use them as UI Test Controls. Here, the code returns the Amazon’s search box from the script and sets the text using UI Test Control APIs:

1) If the JavaScript takes long time to execute, the ExecuteScript API will NOT timeout. Currently, CUIT does not provide any way of setting any kind of execution timeout for this API. User will have to implement his/her own way of timing the execution.

2) “ExecuteScript” API will not return “Int” types directly even if you are expecting an Integer. User will have to cast it to “long” at first and then to “Int” if they really want “Int”. See Usage Example 1.

3) API cannot define functions in a normal way such that the user can use them later as if they are defined in the web page. User can define and call the function normally in the same script i.e. single execution call, but cannot do it across multiple execution calls. Our API cannot directly inject the function into the webpage, but can hold onto a function variable which can be used later to execute the function. One can use execute a function after defining it in the below fashion:

// you cannot define a function in a normal way and use it later browserWindow.ExecuteScript("addnum = function(arg0, arg1) { return (arg0 + arg1); }; "); long result = (long) window.ExecuteScript("return addnum(2,3)");

4) To access elements within frames, user will have to do it themselves using HTML Dom Frame/IFrame objects within the script itself. “ExecuteScript” API will not do the frame switching.

// There is another W3C Schools page embedded inside the original page // We are retreving an inner control inside iframe HtmlImage w3CLogoImage = (HtmlImage)browserWindow.ExecuteScript("return document.getElementsByTagName('iframe')[0].contentDocument.getElementsByTagName('img')[0];");

your JavaScript syntax is incorrect. you are returning an array of elements from script, but trying to cast to a single object type. You should change your script to below. Also, Button is not of type HtmlButton, but it is an HtmlInputButton. this depends on how the HTML page defined the button of course.

Is there any way to add a javascript file on Page using BrowserWindow.ExecuteScript() or any other way in CodedUIProject so that i could write all my functions in a single js file and could include them on page wherever required.

I am using VS2012 and working test automation for a website. Whenever I tried to use browserWindow.ExecuteScript("/script/"), I get access denied error. this was working fine till some days back. Browser is IE11.

Executing following code:

this.BrowserWindow.ExecuteScript("alert('MyPage')");

On debugging, I found out that ExecuteScript() is not debugging any javascript at all.

Please help and let me know if we can use ExecuteScript() method to execute javascript on IE11 or not. If not then what is the alternative in coded UI.

I have found that update kb3025390 which relates to a fix on show dialog forms in IE11 has broken the ExecuteScript function. Have raised this with MS, but having issues getting them to realise the impact and what the actual issue is as they told me was replaced by EVal function which is not related to this. So please raise with MS so we can get it logged numerous times until they take it seriously.

But currently I have removed the kb3025390 and stopped automate windows updates to resolve it.

I am working in coded ui 2012.I have stuggled in BrowserWindow.ExecuteScript method.I am using BrowserWindow.ExecuteScript method,Which is working in under [TestMethod] in [CodedUITest] class but its not working in UIMap.Designer.cs class. Bellow metioned IE version details

Hi, I'm having problems when I run a simple javascript always getting the following error:
"Microsoft.VisualStudio.TestTools.UITest.Extension.UITestJScriptExecutionException: stale element reference: element is not attached to the page document
(Session info: chrome=42.0.2311.135)
(Driver info: chromedriver=2.8.241075,platform=Windows NT 6.3 x86_64)"
For example if I run this line of code:
" HtmlFrame frame = (HtmlFrame)BrowserWindow.ExecuteScript("return document.getElementsByTagName('frame')[1];");"
It will work fine, returning a correct HtmlFrame but when I try to search for another inside frame like:
"HtmlFrame frame2 = (HtmlFrame)BrowserWindow.ExecuteScript("return document.getElementsByTagName('frame')[1].contentWindow.document.getElementsByTagName('frame')[1];");"
,than the above message error will apear.
Can anyone help me with this problem?
Thanks!