【问题标题】:Simple Selenium Test Gives XHR ERROR : 404简单的硒测试给出 XHR 错误:404
【发布时间】:2011-04-23 21:19:26
【问题描述】:

我正在使用 selenium-rc 和 C# 来测试我的 asp.net mvc2 应用程序。目前我正在做的就是用 selenium RC 打开主页。

当我运行测试时,我看到 selenium 遥控器在浏览器中打开,我看到我的应用程序在浏览器中正确打开,但 selenium 显示 404 错误并中止测试。

为了使这项工作正常进行,我将测试缩减为一个简单的控制台应用程序,并更改了默认的 selenium 端口,这是我的代码:

static void Main(string[] args)
    {
        try
        {
            var _selenium = new DefaultSelenium("localhost", 1234, "*chrome", "http://localhost:6666");
            _selenium.Start();
            _selenium.Open("/");

            try
            {
                _selenium.Stop();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        Console.WriteLine("Done.");
        Console.ReadKey();

    }

失败并出现错误“XHR ERROR: URL = /localhost:6666/ Response_Code = 404 Error_Message = Not Found on session 5f2e59798ae74e7cb741d50cae7e817a”

如果您查看正在运行的 selenium 控制台,您会看到

15:53:16.238 INFO - Allocated session 5f2e59798ae74e7cb741d50cae7e817a for http://localhost:6666, launching...
15:53:16.265 INFO - Preparing Firefox profile...
15:53:18.325 INFO - Launching Firefox...
15:53:20.977 INFO - Got result: OK,5f2e59798ae74e7cb741d50cae7e817a on session 5f2e59798ae74e7cb741d50cae7e817a
15:53:20.980 INFO - Command request: open[/, ] on session 5f2e59798ae74e7cb741d50cae7e817a
15:53:22.654 INFO - Got result: XHR ERROR: URL = http://localhost:6666/ Response_Code = 404 Error_Message = Not Found on session 5f2e59798ae74e7cb741d50cae7e817a

我尝试过的有趣的事情是:

将网站更改为“google” - 然后我的测试工作
将站点更改为“google”端口 80 然后它不起作用。
将我的网站从 cassini 移到 iis 7 - 没有用
使我的网站成为 iis 7 上的默认网站,例如 http://localhost/ - 没用
我使用 fiddler 来观看会话 - 触发的所有请求都成功返回。
我尝试在启动 RC 服务器时传递 -avoidProxy 和 -ensureCleanSession。没有任何明显的区别。

如果我重新配置 selenium 以使用默认端口,我会一遍又一遍地重复以下异常(每 10 秒一次/每次不同的 sessionid)。一旦我更改了默认端口,就不会发生这种情况,但我已将其包括在内以防万一。

16:26:38.019 WARN - POST /selenium-server/driver/?seleniumStart=true&localFrameAddress=top&seleniumWindowName=&uniqueId=sel_48694&sessionId=a87b8d6a9e444a5485a5044ef6370e2d&counterToMakeURsUniqueAndSoStopPageCachingInTheBrowser=1286810798016&sequenceNumber=4115 HTTP/1.1
java.lang.RuntimeException: sessionId a87b8d6a9e444a5485a5044ef6370e2d doesn't exist; perhaps this session was already stopped?
    at org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(FrameGroupCommandQueueSet.java:220)
    at org.openqa.selenium.server.SeleniumDriverResourceHandler.handleBrowserResponse(SeleniumDriverResourceHandler.java:165)
    at org.openqa.selenium.server.SeleniumDriverResourceHandler.handle(SeleniumDriverResourceHandler.java:131)
    at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1530)
    at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1482)
    at org.openqa.jetty.http.HttpServer.service(HttpServer.java:909)
    at org.openqa.jetty.http.HttpConnection.service(HttpConnection.java:820)
    at org.openqa.jetty.http.HttpConnection.handleNext(HttpConnection.java:986)
    at org.openqa.jetty.http.HttpConnection.handle(HttpConnection.java:837)
    at org.openqa.jetty.http.SocketListener.handleConnection(SocketListener.java:245)
    at org.openqa.jetty.util.ThreadedServer.handle(ThreadedServer.java:357)
    at org.openqa.jetty.util.ThreadPool$PoolThread.run(ThreadPool.java:534)

有人能解释一下吗?

【问题讨论】:

  • 当您将它与普通端口一起使用时,您是在运行测试时遇到这些错误还是在没有运行任何测试的情况下出现这些错误?

标签: c# asp.net-mvc-2 selenium selenium-rc


【解决方案1】:

我正在使用 C#,并且与 selenium.open 方法有类似的问题。我已经参考了他在上面的答案中发布的线程 haroonzone。并在我的代码中进行了以下更改

声明selenium

private DefaultSelenium selenium;

而不是

private ISelenium selenium;

并调用open方法为

    selenium.Processor.DoCommand("open", new String[] { YOUR_URL, "true" });

而不是

    selenium.Open("YOUR_URL");

另外,你可能想打电话

selenium.SetTimeout("600000");

DoCommand 之前

【讨论】:

    【解决方案2】:

    您使用的是什么版本的 Selenium Server?我们在 Selenium Server 2.0a2 中遇到了这个问题。它在更高版本的 Selenium 中已修复,因此如果您下载最新版本的 Selenium 服务器并对其运行此测试,那么您将不会收到 XHR 异常。但是,如果您无法更新 selenium 版本,那么您可以执行以下操作。代码 sn-p 在 Java 中,您可以在 C# 中使用类似的代码。

    问题出在 selenium.open 方法上。您可以在以下 URL 上找到更多信息。 http://code.google.com/p/selenium/issues/detail?id=408

    selenium = new DefaultSelenium("localhost", port, browserString, url) {
            public void open(String url) {
                commandProcessor.doCommand("open", new String[] {url,"true"});
            }
        };
    

    【讨论】:

    • 我会试试这个并回复你
    • 那不编译 - 你确定那不是 java 什么的吗?
    • 我尝试创建一个从 DefaultSelenium 派生的新类,但 open 方法不是虚拟的
    • 我还没有尝试将它添加到我们的框架中,因为我们使用的是 selenium 网格 startSession 方法而不是 DefaultSelenium,另一种解决方法是使用 try catch 块包装 open 方法,例如 try{ selenium.open(url ); }catch (SeleniumException se){ if (se.getMessage().contains("XHR Error"){ selenium.waitForPageToLoad("30000"); } }
    猜你喜欢
    • 1970-01-01
    • 2022-08-10
    • 2021-11-09
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多