This is a migrated thread and some comments may be shown as answers.
Executing javascript in coded step
11 Answers 97 Views
This is a migrated thread and some comments may be shown as answers.
Jake
Top achievements
Rank 1
Jake asked on 11 Jun 2013, 06:52 PM
Hello,

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

Sort by
0
Cody
Telerik team
answered on 13 Jun 2013, 08:08 PM
Hello Jake,

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.

Regards,
Cody
Telerik
Free summer webinars on advanced web automation tactics hosted by Jim Holmes & Adam Goucher.
Reserve your seat today!
0
Jake
Top achievements
Rank 1
answered on 25 Jun 2013, 03:21 PM
Can you show me an example of executing client side javascript? I can not create methods inside of the page itself.
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.
0
Cody
Telerik team
answered on 25 Jun 2013, 06:08 PM
Hi Jake,

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')") 

Regards,
Cody
Telerik
Free summer webinars on advanced web automation tactics hosted by Jim Holmes & Adam Goucher.
Reserve your seat today!
0
Kiroloss
Top achievements
Rank 1
answered on 01 Dec 2015, 07:28 PM

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?

0
Cody
Telerik team
answered on 01 Dec 2015, 07:57 PM
Hi Kiroloss,

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
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Kiroloss
Top achievements
Rank 1
answered on 01 Dec 2015, 08:08 PM
Below is a test result of a Coded ui testing. Just to be clear im using Test Studio to do come automated testing and i'm trying to catch any and every javascript error that occurs and doe it once in my testing. You see when it tries to execute the line i added for you above i get this error, while trying to test my application. Below is one result.

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!
0
Cody
Telerik team
answered on 01 Dec 2015, 09:07 PM
Hello Kiroloss,

...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
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Kiroloss
Top achievements
Rank 1
answered on 01 Dec 2015, 09:10 PM
I actually figure the issue. the function i created, actually do catch javascript errors. The issue is it has to be on the current active browser. So i had to click on the page i wanted to test than add this function and it works. Is there anyway it can be global on the test?
0
Cody
Telerik team
answered on 01 Dec 2015, 09:28 PM
Hello Kiroloss,

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
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Kiroloss
Top achievements
Rank 1
answered on 01 Dec 2015, 09:39 PM
It's okay Cody, i was confusing. I found my answer on your form. Thanks. 
0
Cody
Telerik team
answered on 01 Dec 2015, 10:22 PM
Hello,

Excellent! Thank you for the update.

Regards,
Cody
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Asked by
Jake
Top achievements
Rank 1
Answers by
Cody
Telerik team
Jake
Top achievements
Rank 1
Kiroloss
Top achievements
Rank 1
Share this question
or