views:

45

answers:

2

Hi,

I constantly find myself rewriting the same lines of code in the Firebug console to test my application. The application uses UI so much that I don't think unit tests are the solution here? How could I quickly run lines of JavaScript code without typing them again and again?

+1  A: 

To run JavaScript code you have to type it. Or save it somewhere.

Luca Matteis
Yeah, but you can save it *in a bookmark* : http://stackoverflow.com/questions/2347353/javascript-development-bookmarking-lines-of-code/2347377#2347377
T.J. Crowder
+3  A: 

You can use bookmarklets to do exactly what you're talking about: Running JavaScript in the context of the current window from a bookmark. Bookmarklets need to be fairly short and can be a pain to write (because they're javascript URLs, and so need to be URL-encoded), but you can use a bookmarklet to bootstrap an external script (written normally) into the page and then just call functions in that script, which minimises how much actual bookmarklet code you have to write. More about doing that (including an example) in this answer here on SO.

But in terms of testing: Presumably the UI is underpinned by logic code, it's worth having unit tests for that. There are also unit testing frameworks specifically for web applications that you can use for testing UI features; see this SO question/answer for more. The one you seem to hear about most right now is Selenium.

A couple of others have mentioned GreaseMonkey, and in FF that's certainly an option. The nice thing about bookmarklets is that they work in a wide range of browsers.

T.J. Crowder
You can do something like J.Resig done for profiling JS code (http://ejohn.org/blog/deep-profiling-jquery-apps). This way you can run all tests you need automatically or using simple trigger function, which you can same as a bookmarklet.
NilColor
Really useful and time saving information there!
rFactor
@Tower: Cool, glad that helped.
T.J. Crowder