Java Selenium RC as configured with Python objects
Chalk it up to diving into the deep end without a life ring, but it took a bit more work to get Java to use Python objects than it did the other way. I did eventually cobble together a solution based on various fragments dribbled around the interweb. There is a tonne of examples on how to use Java in Python, but there seems to be a hole for the inverse. Sure, there are Simple and Efficient Jython Object Factories, but to me they are neither simple, nor efficient. Simple and efficient would be importing a module and things Just Work&tm;.
Here is a simple JUnit test which checks whether a link is available to a certain user. Note how the url the Selenium server connects to is build from information in the python cf.environment dictionary. Likewise, the username and password come from cf.users.
<pre lang="python">import org.python.util.PythonInterpreter;
import org.python.core.*;
import junit.framework.*;
import com.thoughtworks.selenium.*;
import java.lang.System;
import java.lang.String;
public class ro_tmp_dsp_mnge_0002 extends TestCase {
private Selenium browser;
public void setUp() throws Exception {
// get ourselves the config_file object
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import ro.config_file");
interpreter.exec("cf = ro.config_file.config_file()");
// connect to the selenium proxy
interpreter.exec("env = cf.environment");
interpreter.exec("url = '%s://%s:%s' % (env['protocol'], env['host'], env['port'])");
PyObject pyUrl = interpreter.get("url");
String url = (String)pyUrl.__tojava__(String.class);
browser = new DefaultSelenium("localhost", 4444, "*chrome", url);
browser.start();
// get our user info
interpreter.exec("user = cf.users['setupuser']['username']");
PyObject pyUser = interpreter.get("user");
String username = (String)pyUser.__tojava__(String.class);
interpreter.exec("password = cf.users['setupuser']['password']");
PyObject pyPassword = interpreter.get("password");
String password = (String)pyPassword.__tojava__(String.class);
// login
browser.open("/Web/myProduct");
browser.selectFrame("LoginFrame");
browser.type("Login.Token1", username);
browser.type("Login.Token2", password);
browser.click("realLogon");
}
protected void tearDown() throws Exception {
browser.stop();
}
public void testTabNotAvailableToSetupUser() throws Throwable {
assertFalse(browser.isElementPresent("link=Manage"));
}
}
Now a Java programmer can add selenium based tests to the metaframework without having to know python (other than to parse and process the config file — which is painfully easy).