尝试启用 JQuery AddLocationStrategy 时出现 Selenium RC null sessionId 异常

发布于 2024-10-08 09:04:42 字数 3708 浏览 0 评论 0原文

我一天中的大部分时间都在尝试使用我在互联网上找到的各种建议来启用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 上运行“addLocationStrategy”命令时出现异常 java.lang.NullPointerException:sessionId 不应为 null;这个会议已经开始了吗?

我的配置是:

Win7 64位
IIS
硒-服务器-1.0.3
火狐浏览器
C#

我发现有两种 JavaScript 风格可以调用 .AddLocationStrategy()。这是我的实现:

[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 的其余部分相呼应)...
}] 会话为空
19:14:09.796 错误 - 在会话 null 上运行“addLocationStrategy”命令时出现异常 java.lang.NullPointerException:sessionId 不应为 null;这个会议已经开始了吗?
在 org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(FrameGroupCommandQueueSet.java:216)
在 org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(SeleniumCoreCommand.java:34)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念满溢 2024-10-15 09:04:42

Sessionid null 通常意味着 selenium 对象尚未被传递。尝试传递该对象,它会起作用。

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

彩虹直至黑白 2024-10-15 09:04:42

事实证明,我需要在调用“selenium.AddLocationStrategy(...)”之前调用“selenium.Start()”,这是修改后的代码:

  [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;
   }
}

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;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文