【问题标题】:Getting Selenium RC null sessionId exception trying to enable JQuery AddLocationStrategy获取 Selenium RC null sessionId 异常尝试启用 JQuery AddLocationStrategy
【发布时间】:2011-05-27 11:20:46
【问题描述】:

我大部分时间都在尝试使用我在互联网上找到的各种建议来启用 Selenium RC 上的 JQuery 定位器,但运气不佳。我已遵循此线程中包含的启用 JQuery 定位器的建议:

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 错误 - 在会话 null 上运行“addLocationStrategy”命令时出现异常 java.lang.NullPointerException: sessionId 不应该为空;本次会议开始了吗?

我的配置是:

Win7 64位
IIS
selenium-server-1.0.3
火狐
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 上运行“addLocationStrategy”命令的异常 java.lang.NullPointerException: sessionId 不应该为空;本次会议开始了吗?
在 org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(FrameGroupCommandQueueSet.java:216)
在 org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(SeleniumCoreCommand.java:34)

【问题讨论】:

  • 所以,在玩了这个之后,我意识到我需要在调用“selenium.AddLocationStrategy(...)”之前调用“selenium.Start()”

标签: jquery selenium user-extensions.js


【解决方案1】:

原来我需要在调用“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;
   }
}

【讨论】:

    【解决方案2】:

    Sessionid null 通常意味着 selenium 对象没有被传递。尝试传递对象,它将起作用。

    【讨论】:

    • 你能说得更具体点吗?你指的是哪种方法? .AddLocationStrategy() 没有重载。我看到的唯一另一种可能性是使用 .DefaultSelenium() 的重载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2013-02-18
    • 2012-06-02
    • 1970-01-01
    相关资源
    最近更新 更多