The Power of the letter J

If I come close to be a ‘programmer’ in any language, it is in Python. Actually, I teach a course in it so I suppose I am one. These days I do most of my coding in Jython which has all the simplicity and power of Python but lets me reuse 12 years worth of Java code when I don’t feel like re-inventing the wheel.

So, by choosing Jython I get: Python and Java in one bundle.

Part of my job is to automate testing where I think it will be of future benefit (or where it lets be be lazy at some point in the future :)). This automation often takes the form of Selenium RC scripts so I can make better use of oracles, logging, temp files, etc. The common paradigm for writing Selenium RC scripts is through the languages *unit framework.

So, by choosing Jython I get: Selenium scripts using Python’s unittest module and Java’s JUnit in one bundle.

Jython needs at least version 1.4 of Java in order to function, but works with 1.5 and 1.6. At one point I had myself convinced that I needed to run the same version of Java in my metaframework as our application was running, but I have overcome this bit of nonesense. By doing that I allowed myself to start running with 1.6 which means (a lot of things, but most importantly) I can make use of JSR-233: Scripting for the Java Platform.

JSR-233 provides hooks for (currently) 25 scripting engines to be accessed from within your native Java (Jython) code. This is incredibly trick. Want Groovy? Sure. How about Awk? It’s there too. In my context however, what I want is Ruby which I can do via JRuby.

Let’s catch up again. By choosing Jython I get: Selenium scripts using Python’s unittest module, Java’s JUnit and Ruby’s unit::test module in one bundle.

Now we most certainly have Selenium scripts using Python’s unittest module, Java’s JUnit and Ruby’s unit::test module in one bundle. Which means there is now very little reason for developers to not only produce unit tests for there application / business logic, but Selenium ones for the web / presentation tier as well.

For those wanting the Jython code which will initialize and execute a JRuby script, here it is.

import javax.script.ScriptContext
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.ScriptException
 
m = javax.script.ScriptEngineManager()
r = m.getEngineByName("jruby")
rs = open("c:\\\\temp\\\\my_selenium_file.rb", "r")
y = r.eval(rs.read())
rs.close()

This is of course, a very simple example, but it works. Oh the power of the letter J. Python code executed in a Java interpretor which then creates an instance of the Ruby interpretor and runs Selenium commands written in Ruby.

Post a Comment

Your email is never published nor shared. Required fields are marked *