torsdag 21 februari 2008

HTMLUnit!

First of all, I just wanna say : You guys at HTMLUnit rock!

HTMLunit is a framework written in Java that simulates a client application to a your webserver, i.e. a browser. This one does that by going to a site, parsing through all the site and executing all javascript it comes across. And it works to! We use a lot of ajax on our site, and everything seems to work. It even handles all the jQuery stuff very well. The guys who developed this framework must really have put a lot of work in reproduction all of them browser bugs out there that jQuery ,among other, tries to solve. Respect.

That beeing said, lets complain a little. But not about htmlunit, but about Java. That damn language is so verbose. I want to execute some javascript and get the result to run my tests. No big deal, but in Java i have to type something like :
(new WebClient (
new URL("http://www.google.com"))).
getPage().executeJavaScriptResult("1+1").
getScriptResult()

where as in Ruby, this would probably be something like:
Web::Client::Page.
new("http://google.com").
js("1+1").result

This ain't a problem if you sit on eclipse, IntelliJ or something. But the thing is, I don't. And then you have to wrap all of it in classes, main etc. I started doing this but then I quickly converted everything to Jruby, or rather I just started programming in ruby instead and let jruby handle the gap between the lanugages.

The blog Scraping Dynamic Websites Using JRuby and HtmlUnit helped me getting started. But I didn't get the tarball there to work. Instead I downloaded the newest version of HMTLUnit from sourgeforge. After a couple of minutes I was up and running.

It first I tried to just use HTMLunit as a javascript runner, and use JSSpec as the testframework. But a) I didn't get it to function like I expected/ wanted to. b) There are some serious state-charing-related issues that has to be solved. So therefore I just hooked it up with rspec under jruby. It gave me a java-horror-stacktrace but then it just worked!

I was running javascript tests from the console, and it was some pretty advanced stuff. 'Did that element fadeout?, was that element updated? Did I get that response from the server'. It worked, but I wanna give a warning about how slow it was. It was about 1 sec / test. So if you have 1100 tests as we have on the backend....

Now it's just to hooked it all up with CI and we're ready to go.

I'll soon be back with a hands-on tutorial on how to set ut everything. 'til then. Bye!

10 kommentarer:

Anonym sa...

Sorry that the download from Scraping Dynamic Websites with JRuby did not work from you. What sort of problems were you having? I'd love to know so I can clear up the instructions.

Gareth Doutch sa...

You might like to try Jython as well.

from com.gargoylesoftware.htmlunit import WebClient

wc = WebClient()
page = wc.getPage('http://www.google.com')

theneubeck sa...

@rob : there was a java-versioning problem. Seems like you needed java 1.6 which doesn't seem to be released for the Mac OS X (which I use) yet. Your code was helpful though!

I don't have all the files left on my drive, so I can't reproduce it now. Hope my answer is helpful anyway.

theneubeck sa...

@gareth: Jython is a real nice project which I hadn't find time to test out yet.

The problem here is however that we do all our stuff in ruby, so I figured that you don't wanna change your frame of mind to perform testing.

Fredrik S sa...

Does it have midi support?

Unknown sa...

@schmilblick: Well actually we were discussing midi support for the SoundCloud-website yesterday.

So soon you'll have it your way. Jruby most possible has a midi interface. I really have to check that out.

rasputnik sa...

Take a look at Celerity. It's fab.

maddy sa...

Hi check this out. it might be helpful for you. Thanks!
Job Requirements

Anonym sa...

It's too complex and very wide for me. I'll look forward to your subsequent post and try to get the cling of it.
Also visit: Medisoft software
Prefered software by most medical professionals

Anonym sa...

This is not the first of your posts I've read, and you never cease to amaze me. Thank you, and I look forward to reading more.

Please visit:
billing company medical
Recognized by many medical physicians