This is a migrated thread and some comments may be shown as answers.

Script Error Test

5 Answers 136 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 01 Dec 2010, 07:44 PM
Is there a test for javascript errors I can run?

Just to see if the browser page has any errors...and hopefully capture them to the log

5 Answers, 1 is accepted

Sort by
0
Stoich
Telerik team
answered on 02 Dec 2010, 05:32 PM
Hi Steve,  
   we don't have it as a built-in feature and it would make a great Feature Request so I'm going to log it.

Anyway I managed to come up with a way to do though it has some drawbacks.

First off: WebUI Test Studio can handle Javascript error dialogs (the kind that pop-up when you double click on a JavaScript error icon in the bottom left corner). However, there's no way to actually record the clicking of that icon and I couldn't find a hotkey or any other recordable action that will make a JavaScript error pop-up. 
This leaves us with only one option: we change IE's setting so that EVERY JavaScript error is a pop-up.
Here's how you can do that:
http://www.testingreflections.com/node/view/4009 - this works for IE7 and 8.
You can find similar articles on how to change the same setting for the other browsers.

Keep in mind that using this setting is generally a bad idea - your "normal" tests might start failing left and right. I recommend you set aside a separate Virtual Image with the all your browser set to allow JavaScript error pop-ups.

Ok, now comes the really tricky part: WebUI Test Studio's feature that handles unexpected dialogs doesn't work with this dialog. Here's why: you start loading (Navigating to) a page and mid-way through the navigation a JavaScript Error pop-up appears. This means that JavaScripts that were set to execute onLoad failed. Now the browser is waiting for you to handle the pop-up so it can continue loading the page. In practice you're stuck mid-step: WebUI TS isn't watching for an Unexpected Dialog because it thinks it's still loading a page. 

There's is a way out of it - you set a relatively short timeout for your NavigateTo step and you also set it "ContinueOnFailure". Now, when you get the pop-up, WebUI TS will stop trying to load the page at some point and will declare the step to be a failure. If there's no pop-up then execution will continue normally.

Immediately after this step you have to put 5 more steps:
1) Connect to Dialog
2) Click on Copy Error To Clipboard
3) Click Cancel
4) Disconnect from Dialog
5) Paste the content of the Clipboard to the Log

Step 1 through 4 you can just record. Go to some page that always throws an exception and record the way you handle it. You probably want to set these steps to Continue On Failure too since they will fail if there's no JavaScript Error (hence no Dialog to handle).  Here's a page that throws JavaScript Errors every time, use it to record the dialog:
http://www.technologyquestions.com/technology/internet-explorer/110332-javascript-error-every-page.html

For the fifth step you can reuse my code:
[CodedStep(@"Log JavaScript Exception")]
public void WebAiiTest2_CodedStep()
{
    String idat = null;
    Exception threadEx = null;
    Thread staThread = new Thread(new ThreadStart(
        delegate
        {
            try
            {
                idat = System.Windows.Clipboard.GetText();
            }
            catch (Exception ex)
            {
                threadEx = ex;
            }
        }));
  
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start();
    staThread.Join();
    Log.WriteLine("This is your JavaScript Exception:"+idat);
}

You will have to add PresentationCore.dll to your references and also add a using System.Threading;
in order to get it to compile.

So you take those 5 steps and put them after the navigateTo step that takes you the page that you want to test for JS errors and you're set to go. 

It would be a great idea to have this whole thing as a separate test and just add it with "Test as Step" when you need it. Keep in mind that it will ALWAYS fail:
  - no JS errors --> Dialog Handling Fails;
  -has JS errors --> the timeout fails;

I've attached a completely-working example test. You can even reuse it without changing anything at all except the URL to which it navigates (and tests). And you have to add a reference to PresentationCore.dll before it will work. 

I realize you might not succeed in getting all of this to work on the first try - don't give up. I managed to get it working without too much trouble and so can you - we can help if you get stuck.

Please let us know how it goes!

Best wishes,
Stoich
the Telerik team
Interested in Agile Testing?

Tune in Wednesday, December 1st for a very special Webinar focused on Agile Testing. Click here to register for free!
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 02 Dec 2010, 06:17 PM
Wow, that is one HARDCORE answer, thank you :)

I am to also very nervous about turning on the JS popups...i'm pretty sure there's a script error or 10 in some other pages so I'd rather not have them start failing (even though the script errors dont seem to affect anything)

I would be VERY VERY interested in the PITS issue link though if you guys use that for this feature :)

Steve
0
Cody
Telerik team
answered on 03 Dec 2010, 12:45 AM
Hi Steve,

Currently the WebUI development team has not started using the PITS system yet. We log feature requests in our own internal logging system. This is a carry over from the system we were using before PITS existed on the Telerik website.

I also reviewed Stoich's proposed solution. I too would be very leery of turning on the JS popups that way. Plus another killer is that the test will be marked failed no matter what. I think the solution is interesting from an academic point of view, but I don't think it's really useful as part of a test suite.

Your request is one we get from time to time. Unfortunately we just do not have a good solution to this problem currently.

Greetings,
Cody
the Telerik team
Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 03 Dec 2010, 03:44 AM
Hey Cody,
  I would respectfully disagree...it's entirely possible to get a script error in IE and not in Firefox while still maintaining apparent full functionality.  I literally just had the issue which prompted the ticket.  Like the jQuery all worked fine, but the MS clientside validations were all wonky...We can run cross browser tests, and this is a cross browser issue (obviously potentially sometimes).

  It's a tiny yellow icon in the bottom left...I mean that's the very definition of what an automated testing suite is for.  It's not something I can test for with a standard unit test....

Steve
0
Cody
Telerik team
answered on 03 Dec 2010, 07:25 PM
Hi Steve,

I am not certain what part(s) you are disagreeing with, but by all means please feel free to do so as much as you like. You won't offend me in any way at all disagreeing with any of my statements.

Regarding the ability to test for JavaScript error's, I most certainly agree this would be a very useful feature for automated web application testing! i was only pointing out that today we do not have a good solution for how to detect these JavaScript errors (such as the triangle in the status bar). While Stoich's approach could work, I am concerned how well it would work when you try to implement it as part of a test suite. It sounds very unstable on the surface. And the worst part is WebUI will always consider the test as having failed one way or another. It requires a human interpreting the log to tell whether or not a JavaScript error actually occurred (or some sort of automated parsing of the log after the fact).

All the best,
Cody
the Telerik team
Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
Tags
General Discussions
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Stoich
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Cody
Telerik team
Share this question
or