我一天中的大部分时间都在尝试使用我在互联网上找到的各种建议在 Selenium RC 上启用 JQuery 定位器,但运气不佳。我已遵循此线程中包含的启用 JQuery 定位器的建议:
如何将 JQuery 定位器添加到 Selenium 远程控制
我按照建议修补了 TestRunner 文件,并对 RemoteRunner 文件应用了相同的修复程序。我还修补了各自的 *.hta 文件。我还将缩小的 jquery.min.js 文件添加到 JAR 文件的 lib 目录中。
我还尝试保持服务器 JAR 完整并使用 user-extensions.js 文件(其中包含 jquery.min.js)。但这也不起作用。
在所有情况下,我都会收到以下运行时错误:
19:10:50.174 错误 - 在会话 null java.lang.NullPointerException 上运行“addLocationStrategy”命令的异常:sessionId 不应为 null;本次会议开始了吗?
我的配置是:
Win7 64 位
IIS
selenium-server-1.0.3
Firefox
C#
我发现了两种用于调用 .AddLocationStrategy() 的 JavaScript。这是我的实现:
[SetUp]
public void SetupTest()
{
selenium = SeleniumUtils.GetSeleniumWithJQueryStrategy("localhost", 4444, "*firefox", "http://localhost:55023");
selenium.Start();
sbVerificationErrors = new StringBuilder();
}
这是我的实用程序类
public static class SeleniumUtils
{
public static ISelenium GetSeleniumWithJQueryStrategy(string serverHost, int serverPort, string browserString, string browserURL)
{
ISelenium selenium = new DefaultSelenium(serverHost, serverPort, browserString, browserURL);
selenium.AddLocationStrategy("jquery", GetJQueryLocationStrategy2());
return selenium;
}
public static string GetJQueryLocationStrategy2()
{
string r = @"
var loc = locator;
var attr = null;
var isattr = false;
var inx = locator.lastIndexOf('@');
if (inx != -1)
{
loc = locator.substring(0, inx);
attr = locator.substring(inx + 1);
isattr = true;
}
var selectors = loc.split('<');
var found = $(inDocument);
for (var i = 0; i < selectors.length; i++)
{
if (i > 0)
{
found = $(found.parents()[0]);
}
if (jQuery.trim(selectors[i]) != '')
{
found = found.find(selectors[i]);
}
}
if (found.length > 0)
{
if (isattr)
{
return found[0].getAttributeNode(attr);
}
else
{
return found[0];
}
}
else
{
return null;
}";
return r;
}
public static string GetJQueryLocationStrategy()
{
string r = @"
var loc = locator;
var attr = null;
var isattr = false;
var inx = locator.lastIndexOf('@');
if (inx != -1)
{
loc = locator.substring(0, inx);
attr = locator.substring(inx +1);
isattr = true;
}
var found = jQuery(inDocument).find(loc);
if (found.length >= 1)
{
if (isattr)
{
return found[0].getAttribute(attr);
}
else
{
return found[0];
}
}
else
{
return null;
}";
return r;
}
}
此处调用失败:
19:10:13.297 信息 - 开始 org.openqa.jetty.jetty.Server@2747ee05
19:10:50.139 信息 - 检查资源别名
19:10:50.151 信息 - 命令请求:addLocationStrategy [jquery,
var loc = 定位器;
...(与 Javascript 的其余部分相呼应)...
}] 会话 null
19:14:09.796 错误 - 在会话 null java.lang.NullPointerException 上运行“addLocationStrategy”命令的异常:sessionId 不应为 null;本次会议开始了吗?
在 org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(FrameGroupCommandQueueSet.java:216)
在 org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(SeleniumCoreCommand.java:34)