views:

570

answers:

2

My Application has a lot of calculation being done in JavaScript according to how and when the user acts on the application. The project prints out valuable information (through console calls) as to how this calculation is going on, and so we can easily spot any NaNs creeping in.

We are planning to integrate Selenium (RC with python) to test or project, but if we could get the console output messages in the python test case, we can identify any NaNs or even any miscalculations.

So, is there a way that Selenium can absorb these outputs (preferably in a console-less environment)?

If not, I would like to know if I can divert the console calls, may be by rebinding the console variable to something else, so that selenium can get that output and notify the python side. Or if not console, is there any other way that I can achieve this.

I know selenium has commands like waitForElementPresent etc., but I don't want to show these intermediate calculations on the application, or is it the only way?

Any help appreciated.

Thank you.

+4  A: 

There is GetEval() call that returns the result of a JavaScript call to the page. If you have the JavaScript on the page then you can do something like

self.assertEqual(selenium.GetEval("this.browserbot.getUserWindow().functionUnderTest().isNaN();"),"false","There was a NaN detected")

The browserbot access allows you to call the javascript functions on the page and get the result. The isNaN() will return false if you get a decent result

AutomatedTester
Thanks for the response, I checked the documentation of `getEval`. It is very helpful. So, I can't use all the stuff printed out through console calls scattered in the application?
Shrikant Sharat
Console calls in your code for JavaScript is seen as bad practise because of the way the different browsers react to it. http://stackoverflow.com/questions/915385/javascript-best-practice-handling-firebug-specific-code
AutomatedTester
You can save all that info in an array inside document.window and then retrieve all of them using getEval and getUserWindow()
Santi
@AutomatedTester: I think console calls are a really wonderful way to debug, but I too am against having them in production code. We have a python build script that removes console calls and minifies all .js files and then deploys to our production server, so console calls are very helpful to us. Thanks for the link btw.@Santi: I thought of that the moment I saw the above answer, but felt like it is a dirty way of doing a simple thing. But I think I'll go with it if there are no other answers :)
Shrikant Sharat
Yeah, it sounds dirty. But you must be aware that you're trying to get javascript client backend info with a tool that's made to see and test what users see in the front end.
Santi
@sharat87 if this is going to be used regularly for JavaScript testing then I would recommend a tool designed for testing JavaScript specifically like Js-Test-Driver. I have been using it for a couple months and have even contributed to it because I think that it is a good JavaScript testing tool. It allows you to test different browsers all at the same time, unlike JSUnit , with just 1 click!
AutomatedTester
@Santi: That's true. Guess I misinterpreted the usage of selenium :)@AutomatedTester: Awesome, that test driver is totally cool, I think I'll give it a try. Can you do user-interaction testing too? Thanks.
Shrikant Sharat
+1  A: 

If you are purely testing that the JavaScript functions are performing the correct calculations with the given inputs, I would suggest separating your JavaScript from your page and use a JavaScript testing framework to test the functionality. Testing low level code using Selenium is a lot of unnecessary overhead. If you're going against the fully rendered page, this would require your application to be running to a server, which should not be a dependency of testing raw JavaScript.

We recently converted our application from using jsUnit to use YUI Test and it has been promising so far. We run about 150 tests in both FireFox and IE in less than three minutes. Our testing still isn't ideal - we still test a lot of JavaScript the hard way using Selenium. However, moving some of the UI tests to YUI Test has saved us a lot of time in our Continuous Integration environment.

Josh
Thanks for the response, I'll definitely check out YUI.
Shrikant Sharat