I've been trying most of the day to enable JQuery locators on Selenium RC using various suggestions I've found on the interwebs but without much luck. I've followed the suggestions contained in this thread for enabling JQuery locators:
How do I add a JQuery locators to Selenium Remote Control
I patched the TestRunner file as suggested, and I applied the same fix to the RemoteRunner file. I also patched the respective *.hta files. I also added the minified jquery.min.js file to the lib directory in the JAR file.
I've also tried keeping the server JAR intact and using a user-extensions.js file (which contains jquery.min.js). But this didn't work, either.
In all cases, I'm getting the following runtime error:
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?
My configuration is:
Win7 64-bit
IIS
selenium-server-1.0.3
Firefox
C#
I found two flavors of JavaScript for the call to .AddLocationStrategy(). Here's my implementation:
[SetUp]
public void SetupTest()
{
selenium = SeleniumUtils.GetSeleniumWithJQueryStrategy("localhost", 4444, "*firefox", "http://localhost:55023");
selenium.Start();
sbVerificationErrors = new StringBuilder();
}
And here's my Utility class
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;
}
}
The call fails here:
19:10:13.297 INFO - Started org.openqa.jetty.jetty.Server@2747ee05
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)