1

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)

Community
  • 1
  • 1
Armchair Bronco
  • 2,367
  • 4
  • 31
  • 44
  • So, it after playing around with this I realized that I need to call "selenium.Start()" prior to calling "selenium.AddLocationStrategy(...)" – Armchair Bronco Dec 18 '10 at 15:57

2 Answers2

0

Sessionid null usually means selenium object hasn't been passed. Try to pass the object, it will work.

Rajasankar
  • 928
  • 1
  • 19
  • 41
  • Can you be more specific? Which method are you referring to? There are no overloads for .AddLocationStrategy(). The only other possibility I see is to use the overload for .DefaultSelenium(). – Armchair Bronco Dec 18 '10 at 15:43
0

It turns out that I need to call 'selenium.Start()' prior to calling 'selenium.AddLocationStrategy(...)' Here's the modified code:

  [SetUp]
  public void SetupTest()
  {
     selenium = SeleniumUtils.GetSeleniumWithJQueryStrategy("localhost", 4444, "*firefox", "http://localhost:55023");
     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);
      // Need to call .Start() before calling .AddLocationStrategy()
      selenium.Start();
      selenium.AddLocationStrategy("jquery", GetJQueryLocationStrategy());

      return selenium;
   }
}
Armchair Bronco
  • 2,367
  • 4
  • 31
  • 44