63 ms·
Javascript + DOM in your ruby, the simple way
- xutopia 17y agoThis means I can test all the Javascript that my rails applications use without opening a browser!
- steveklabnik 17y agoYou can run cucumber + selenium. I'm pretty sure selenium can run in headless mode (haven't turned it on yet, too fun to watch!)
- jbarnette 17y agoThis is useful for tightening the feedback loop. Using something like Autotest gives me near-instant feedback on changes I'm making to client-side libraries. I'll continue to periodically run my tests inside a "real" browser, but I think this sort of headless testing is a real win. [Disclaimer: I wrote Johnson, so I'm not exactly a disinterested bystander. Edited for repetition.]
- hugs 17y agoBut then you won't know if all the JavaScript that your rails applications use will actually work in a browser. Some browser testing is still needed.
- WalterSear 17y agoJavascript + DOM, in -my- ruby? It's more common than you think.
- augustl 17y agoUh, what? _Simple_? I'd say this is a over-complification of something as simple as HTTP request.
- Groxx 17y agoSo, which browser's quirks does this emulate? And what version of HTML does it support? (seriously. and what user agent does it report? developing cross-browser code is often important) I can see how this could be extremely useful, but it does strike me as yet another source for unique bugs in your JS-manipulating-DOM code, and it's potentially even harder to find out why they're happening.
- FooBarWidget 17y agoI think it simulates a perfect browser. But that's fine. I see unit testing as the main use case for this library. If you want to test browser compatibility, then just simulate their quirks by mocking and stubbing the relevant APIs.
- mynyml 17y agoIf you use a client-side js framework like JQuery or Prototype, the cross browser compatibility isn't an issue since it's taken care of for you. The user agent it reports as is irrelevant - it's not a full http client. You can use other ruby tools to fetch a page if you want, and then feed harmony the document. As others pointed out, testing is probably the main use case, but it offers other obvious possibilities; for instance, talkerapp.com needed a way to easily validate js syntax for their plugins.
- Groxx 17y agoNothing is irrelevant for a testing platform. For instance, how does it handle a canvas tag? It'd be useful to be able to dump out frames as images. A user agent string could be used by javascript to do any number of things, so I would hope it's modifiable. As to the libraries to make things cross-browser compatible, knowing which browser it tries to emulate would be essential to knowing which lines of code would be executed through this tool. If you can't test the IE-specific code, what's the use if you're developing for IE?
- prodigal_erik 17y agoCool, sounds like it could be a good workaround if you forgot to start with a version of your documents that works without js.
- jrockway 17y agoI like this a lot. 98% of Javascript code people write does not need to be tested in the actual browser -- but it does need testing. Traditionally, people say "oh it's Javascript, who cares" and then their site only works by coincidence (or more frequently, just doesn't work). Making it easy to write proper unit tests will save people the stress of testing algorithms in the browser (either manually, or by loading a page with Test.More tests), or more likely, encourage them to actually think about their Javascript code as code. You know, that stuff you test, refactor, etc. (FWIW, a coworker and I did this a few years ago with Rhino and a custom-hacked mock DOM. Doing it with a real DOM, like what an XML parser gives you for your actual page, is much better! Hopefully that's what this library allows.)
- thinkbohemian 17y agoAnyone tried pairing this with rspec? I could see using this to make some more robust ajax tests.
- stanley 17y agoI've been doing quite a bit through Ruby+Watir and overall it gets the job done. A headless alternative would be great though.
- gregwebs 17y agoThere is also celerity which wraps a JVM implementation. Culerity allows you to run your tested app with normal C Ruby.
- ThinkWriteMute 17y agoSemi-related: How do I write Ruby and poop out Javascript?
- zach 17y agoThe trite answer is to_json. The more substantial answer is to use a Javascript generator like RJS.