I am looking for the simplest way to execute a javascript function within a coded step.
I have been looking at possible Ajax solutions, as well as System.Web.UI possibilities, but I'm wondering what Telerik recommends for doing this specific task in Test Studio.
I also have seen http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/write-tests-in-code/advanced-topics/javascript/invoking-javascript.aspx but that seems to be just about invoking it if the function already exists on the page. I am looking to execute a new javascript function, client side.
Thanks!
11 Answers, 1 is accepted
I am sorry our documentation is not more clear about this, but you can actually put any JavaScript you want in the InvokeScript call. Thus in reality code like this:
Actions.InvokeScript("Test3();")
The JavaScript engine is literally running the JavaScript "Test3();" which is faithly doing as instructed, find the method Test3 and running it.
I hope this helps.
Cody
Telerik
Reserve your seat today!
In the documentation it says: "
// Assume our page has JavaScript method Test1() & Test2() & Test3()
"I have a javascript method that is executed through a bookmark.
It's pretty straight forward. You can invoke any random JavaScript using Test Studio code like this:
Actions.InvokeScript("alert('test')")
This will actually cause the Alert dialog box to be displayed by the browser. Or you can call any JavaScript method that is already loaded in the browser i.e. contained in the HTML loaded by the browser using code like this:
Actions.InvokeScript("MyJavascriptFunc('parm1', 'parm2')")
Cody
Telerik
Reserve your seat today!
Hi cody,
Why is this form not working? I'm getting all kind of exceptions?
Actions.InvokeScript("window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {alert('Error occured:' + errorMsg); return false;}");
Or is test studio now able to run a function?
Test Studio runs the JavaScript contained in Actions.InvokeScript just like you had opened the browsers developer window and entered the JavaScript directly into the console window as shown in the attached screenshot. I put your entire line of code into a test and ran it. I did not experience any problems, as shown in the second attached screenshot, like you explained. Can you show me what exceptions you are getting? Also does the same JavaScript work when you enter it into the browsers console window?
Regards,
Cody
Telerik
Also I throw an exception on purpose in the test to see if it gets caught.
this is my test code:
var actions = Manager.Current.ActiveBrowser.Actions;
string
jsRetValue = actions.InvokeScript(
"window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {alert('Error occured:' + errorMsg); return false;}"
);
Actions.AnnotateMessage(jsRetValue);
Log.WriteLine(jsRetValue);
Below is the test result and you see the output is strange, even though i'm suppose to get a (false) as a return.
Overall Result: Pass
------------------------------------------------------------
'12/1/2015 3:03:25 PM' - Executing test: 'WebTest', path: 'JavaScript Handler\WebTest.tstest.'
'12/1/2015 3:03:25 PM' - Using .Net Runtime version: '4.0.30319.42000' for test execution. Build version is '2015.3.1015.0'.
'12/1/2015 3:03:25 PM' - Starting execution....
'12/1/2015 3:03:27 PM' - Detected custom code in test. Locating test assembly: TestProject1.dll.
'12/1/2015 3:03:27 PM' - Assembly Found: C:\Project\Telerik Coded UI\AutomatingQA\bin\TestProject1.dll
'12/1/2015 3:03:27 PM' - Loading code class: 'TestProject1.WebTest'.
------------------------------------------------------------
------------------------------------------------------------
'12/1/2015 3:03:27 PM' - Using 'InternetExplorer' version '11.0' as default browser.
'12/1/2015 3:03:29 PM' - 'Pass' : 1. [WebTest_CodedStep] : Navigate to : 'mywebsite.'
'12/1/2015 3:03:29 PM' - 'Pass' : 2. Enter text 'ittest2' in 'UserNameText'
'12/1/2015 3:03:29 PM' - 'Pass' : 3. Keyboard (KeyPress) - Tab (1 times) on 'UserNameText'
'12/1/2015 3:03:30 PM' - 'Pass' : 4. Enter text 'test' in 'PasswordPassword'
'12/1/2015 3:03:30 PM' - 'Pass' : 5. Keyboard (KeyPress) - Enter (1 times) on 'PasswordPassword'
'12/1/2015 3:03:30 PM' - 'Pass' : 6. [WebTest_CodedStep1] : Enter text 'test' in 'PasswordPassword'
'12/1/2015 3:03:32 PM' - 'Pass' : 7. Click 'UtilityLink'
'12/1/2015 3:03:33 PM' - 'Pass' : 8. Click 'InvoiceDataLink'
'12/1/2015 3:03:34 PM' - 'Pass' : 9. Click 'Div'
'12/1/2015 3:03:34 PM' - 'Pass' : 10. Enter text '0' in 'UsagePeriodFromText'
'12/1/2015 3:03:34 PM' - 'Pass' : 11. Keyboard (KeyPress) - Enter (1 times) on 'UsagePeriodFromText'
'12/1/2015 3:03:34 PM' - 'Pass' : 12. Enter text '0' in 'UsagePeriodFromText'
'12/1/2015 3:03:35 PM' - 'Pass' : 13. Click 'GetDataButtonButton'
'12/1/2015 3:03:35 PM' - 'Pass' : 14. Select 'ByValue' option 'Electric' on 'DlEnergyTypesSelect'
'12/1/2015 3:03:35 PM' - LOG: System.__ComObject ************************************************
'12/1/2015 3:03:35 PM' - 'Pass' : 15. [WebTest_CodedStep2] : New Coded Step
'12/1/2015 3:03:35 PM' - 'Pass' : 16. [WebTest_CodedStep3] : Select 'ByValue' option 'mmbtu' on 'DlTypesSelect'
'12/1/2015 3:03:35 PM' - 'Pass' : 17. Click 'Div'
------------------------------------------------------------
'12/1/2015 3:03:35 PM' - Overall Result: Pass
'12/1/2015 3:03:35 PM' - Duration: [0 min: 8 sec: 141 msec]
------------------------------------------------------------
'12/1/2015 3:03:36 PM' - Test completed!
...i'm trying to catch any and every javascript error that occurs...
I'm sorry but Test Studio cannot catch JavaScript errors. The JavaScript that you are invoking doesn't throw an error. It only creates a function object and what you see in the log is the string representation of that object.
...when it tries to execute the line i added for you above i get this error...
I don't see any error being thrown at all. The test log looks perfectly normal to me. I see the test passed which is also expected.
Regards,
Cody
Telerik
I'm sorry but I don't quite understand what you mean by "Global on the test"? You can only run JavaScript in a browser means it must be the current active browser. Test Studio itself doesn't run the JavaScript it just sends JavaScript to the browser for execution.
Can you clarify which mean by "global on the test"? What sort of behavior are you looking for?
Regards,
Cody
Telerik
Excellent! Thank you for the update.
Regards,
Cody
Telerik