我按照本指南https://developer.mozilla.org/en/Java_in_Firefox_Extensions使我能够访问firefox插件中的java类(我实际上是将它们添加到selenium ide的用户扩展中)。
上面的指南一切都很好,直到我的java代码尝试初始化导入的对象......
因此,如果我导入jtidy尝试清理html,一旦我初始化它,我就会得到一个java.lang.reflect.InvocationTargetException异常。有趣的是,我可以在javascript中初始化jtidy类,然后通过它,直到我尝试解析dom。我认为这一切都与权限有关,但我对Live Connect的工作还不够,知道从哪里开始。
我的代码如下:
的Javascript
PageBot.prototype.locateElementByLabelText = function(labelText, inDocument) {
//var pageTitle = inDocument.getElementsByTagName('title').item(0).nodeName;
var pageTitle = inDocument.title;
LOG.warn("Title = "+ pageTitle);
var pageSource = inDocument.getElementsByTagName('html')[0].innerHTML;
LOG.warn("I have the page source");
var xpathValue = getXpathUsingJavaFnc(labelText, pageTitle, pageSource);
LOG.warn("Xpath = "+xpathValue);
return this.locateElementByXPath(xpathValue, inDocument);
};
Selenium.prototype.doLoadProject = function(locator, paramString) {
LOG.warn("starting to load Load Project "+locator + " " + paramString);
try{
var result = inst.initialiseProject(locator, Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('AUTOMATED_HOME')+"/");
}catch(e){
LOG.warn(e.message);
}
LOG.warn("finished Load Project");
LOG.warn(result);
};
var basePath = Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('AUTOMATED_HOME');
var fullPath = 'file:///' + basePath + '/ScenarioFramework-jar-with-dependencies.jar';
LOG.warn("full path = "+fullPath);
var extensionUrl = "file:///" + basePath +'/javaFirefoxExtensionUtils.jar';
var classLoaderJarpath = extensionUrl;
var myJarpath = fullPath;
//var testTidy = "file:///" + basePath +'/jtidy-r938.jar';
var urlArray = [];
urlArray[0] = new java.net.URL(myJarpath);
urlArray[1] = new java.net.URL(classLoaderJarpath);
urlArray[2] = new java.net.URL(testTidy);
var cl = java.net.URLClassLoader.newInstance(urlArray);
policyAdd(cl, urlArray);
var classToLoad = "scenario.framework.main.SeleniumRunner";
var aClass = cl.loadClass(classToLoad);
//var aClass = java.lang.Class.forName(classToLoad, true, cl);
var inst = aClass.newInstance();
LOG.warn("finished loading add Java");
function getXpathUsingJavaFnc(eleName, pageTitle, pageSource) {
LOG.warn("Testing getXpath");
try{
var xpathValue = inst.findElementUsingName(eleName, pageTitle, pageSource);
}catch(e){
//var jvm = Components.classes["@mozilla.org/oji/jvm-mgr;1"].getService(Components.interfaces.nsIJVMManager);
//jvm.showJavaConsole();
logExc(e);
}
return xpathValue;
};
function policyAdd (loader, urls) {
try {
var str = 'edu.mit.simile.javaFirefoxExtensionUtils.URLSetPolicy';
var policyClass = java.lang.Class.forName(
str,
true,
loader
);
var policy = policyClass.newInstance();
policy.setOuterPolicy(java.security.Policy.getPolicy());
java.security.Policy.setPolicy(policy);
policy.addPermission(new java.security.AllPermission());
for (var j=0; j < urls.length; j++) {
policy.addURL(urls[j]);
}
java.lang.System.setSecurityManager(null);
var t = java.lang.Thread.currentThread().setContextClassLoader(cl);
}catch(e) {
logExc(e);
}
}
function logExc(e) {
try {
while(e != null) {
LOG.warn(e);
e = e.getCause();
}
LOG.warn(netscape.javascript.JSUtil.getStackTrace(e));
} catch(exp) {
LOG.warn("exception while logging exception");
LOG.warn(exp);
LOG.warn("exception being logged");
LOG.warn(e);
}
}
爪哇:
public static String findElementUsingName(String Name, String pageTitle, String html){
org.w3c.tidy.Tidy tidy = new org.w3c.tidy.Tidy();
return "//input[@id='password']";
}
答案 0 :(得分:0)
每次编译后重启Firefox