Monday, November 1, 2010

HTMLUNIT and Ruby...

I'll use this blog to chronicle the setup and use of HTMLUNIT. HtmlUnit is a "GUI-Less browser for Java programs. More info can be found at http://htmlunit.sourceforge.net/

The first thing I did was a clean install of Ubuntu 10.10. I performed the standard updates after the first boot, so nothing special there.

Next we'll want to install ruby and jruby. We can use 'apt-get' for that. So run:
'sudo apt-get install ruby' which will install Ruby version 1.8.7. You can verify the installation by typing 'ruby -v'.

Now do the same for JRuby. JRuby allows for the implementation of Java using Ruby. To install JRuby type 'sudo apt-get install jruby'. This will give you version 1.5 of JRuby. You can verify the installation by typing 'jruby -v'.

Thats all we need as far as packages go. HTMLUnit is written in Java, so we do need Java installed for it to work. When you install Ubuntu you *should* get version 1.6.x version of Java. You can verify this by typing 'java -version'. If you do not have Java you should be able to run 'sudo apt-get install java' to install it.

Now lets get the latest version of HTMLUnit.

>Created htmlunit.rb which includes require statements for HTMLUnit jar files and class paths to HTMLUnit class paths that looks like this:

Couple of things to note here. We added the first require statement for htmlunit.rb. This give us access to the htmlunit JARs and Classes. Second, notice we added a require statement for 'test/unit'. This gives us access to Rubys unit testing framework. Also note our method name, 'test_main'. Method names using Rubys native test framework must begin with 'test'.

The actual test looks like this:


To run the test you just issue the command 'jruby test.rb'
Heres a looks a my directory structure:


Using Fedora?
If you are using Fedora rather than Ubuntu, there are few more hoops you need to jump through. I ran through this setup using a clean install of Fedora 13.

>install java su -c 'yum install java'
>install ruby su -c 'yum install ruby'
>install jruby; download tar(binary), and install to /usr/lib
>create symlink in /usr/bin pointing to /usr/lib/jruby/bin/jruby; su -c 'ln -s /usr/lib/jruby-1.6.0.RC2/bin/jruby jruby'

>

No comments:

Post a Comment