我一直在尝试大部分时间来启用Selenium RC上的JQuery定位器使用我在interwebs上发现的各种建议,但没有多少运气。我已经按照包含在该线程使JQuery的定位器的建议:获取硒RC空null sessionId异常试图启用JQuery AddLocationStrategy
How do I add a JQuery locators to Selenium Remote Control
我打补丁的TestRunner文件的建议,我应用了相同的修复到RemoteRunner文件。我还修补了相应的* .hta文件。我还将缩小的jquery.min.js文件添加到JAR文件的lib目录中。
我也尝试保持服务器JAR完好无损,并使用user-extensions.js文件(其中包含jquery.min.js)。但是这也没有奏效。
在任何情况下,我得到以下运行时错误:
19:10:50.174 ERROR - Exception running 'addLocationStrategy 'command on session null java.lang.NullPointerException: sessionId should not be null; has this session been started yet?
我的配置是:
的Win7 64位
IIS
硒服务器1.0.3
Firefox
C#
我发现了两种风格的JavaScript用于调用.Ad dLocationStrategy()。下面是我实现的:
[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 INFO - Started [email protected]
19:10:50.139 INFO - Checking Resource aliases
19:10:50.151 INFO - Command request: addLocationStrategy[jquery,
var loc = locator;
...(echoes rest of Javascript)...
}] on session null
19:14:09.796 ERROR - Exception running 'addLocationStrategy 'command on session null java.lang.NullPointerException: sessionId should not be null; has this session been started yet?
at org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(FrameGroupCommandQueueSet.java:216)
at org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(SeleniumCoreCommand.java:34)
所以,它玩了之后这我意识到我需要调用“selenium.Start()”之前调用“selenium.AddLocationStrategy(...)” – 2010-12-18 15:57:46