Monday, January 09, 2012

I was asked to do some compliance analysis against a product. Some research had been performed for me and I was to take the page source and submit it to a online tool called The Ruby Accessibility Analysis Kit or Raakt for short. We chose the online submission because we're primarily a Java shop and writing Ruby code would take longer than just submitting to the site and parsing the results.

I spent a couple days coding up Java/Selenium and was able to output to CSV and nicely report issues but it worried me that this was a personal web site I was using for business purposes. I get up today and the URL comes up with "500 Internal Server Error." Ugh... Now I need to figure out how to call Raakt from Java.

I take a look at the Ruby script that is include on the Raakt site to try and see how I can pass things in and get them out of the script. In the original script it just uses the "puts" command to output to the console. I had already coded up a nice CSV output and wanted to get the result back into Java land. I tried various things but finally read something that "use of return is permissisble but unnecessary" in Ruby. So I tried it and lo and behold I get back a result from the script. I'm certain there's a better way but at the moment I'm just trying to get things rolling.

require 'rubygems'
require 'raakt'

# Set up the RAAKT test and pass in source html
raakttest = Raakt::Test.new(page)

#Run all checks and return them
result = raakttest.all
return result

After a little more Googling I find out that there's such a thing as JRuby and it supports Java integration in the direction that I want. I find that the ScriptingContainer needs to know where JRuby home is (after much head banging). Then I'm able to give the script my page source and get back the result. The only lame part is that it comes back as Object. Maybe I'll tokenize the issues that come back to pop them into my CSV as separate lines but I'm running out of billable hours. :)

public static Object validate(String pageSource) {
    ScriptingContainer container = new ScriptingContainer();
    container.setHomeDirectory("/Library/Frameworks/JRuby.framework/Versions/Current");
    container.put("page", pageSource);
    container.put("result", "");
    Object result = container.runScriptlet(PathType.RELATIVE, "./analyze.rb");
    return (result);

0 comments: