【问题标题】:How to detect when a web service has completed using Selenium Webdriver如何使用 Selenium Webdriver 检测 Web 服务何时完成
【发布时间】:2016-07-20 01:26:53
【问题描述】:

我正在尝试进行一些智能验证,以确定网页文档何时完成加载,同时通过 Selenium WebDriver 执行自动化。

将 IJavaScriptExecutor.ExecuteScript() 与 WebDriverWait() 一起使用已被证明非常有用。

我首先在等待文档告诉我它已加载:

.ExecuteScript("return document.readyState == 'complete'");

接下来我将等待所有 AJAX 库(如 jQuery 和 Dojo)告诉我它们处于非活动状态:

.ExecuteScript("return jQuery.active == '0'");
.ExecuteScript("return Ajax.activeRequestCount == '0'");
.ExecuteScript("return dojo.io.XMLHTTPTransport.inFlight.length == '0'");

此时我仍然注意到在 ajax 检查开始之前正在加载的东西,仍在运行(屏幕上的微调器以用数据行填充表格等)。我追踪了其中一个,并在下面放了一个粗略的调用堆栈。

除了等待元素存在之外,还有什么方法可以智能地判断此 (.asmx) Web 服务何时完成?它来自 ASP.NET 应用程序并通过生成的代理类调用 Web 方法。我还可以看到访问 Sys.Net 的客户端代码是否有助于或可用于进行某些检测。

// Target Web Page

// called from a script tag at the ending body tag
$("#@fillArea").bind({
    "select_node.jstree": function (e, data) {
        setTimeout(function() {
            FillList();
        }, 1);

        return true;
    }
});

    // web service called by FillList()
    Abc.Api.MyService.Fill(listParams, onFillSuccess, onFailure, param);

        // web service code for Fill()
        [WebService(Namespace = "http://abc.alphabet.com/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1]
        [ScriptService]
        public class ListService
        {
            [WebMethod(EnableSession = true)]
            [ScriptMethod(UseHttpGet = true)]
            public string Fill(ListParams listParams)
            {
                // get database data
                // return html markup
            }
        }

已编辑:一种解决方案发现了如何检测 Microsoft AJAX 库的 AJAX 流量

在 document.ready == 'complete' 之后立即插入以下用于为其连接事件的代码

((IJavaScriptExecutor)driver).ExecuteScript
(
    @"MsftAjaxRequestCount = 0;

    Sys.Net.WebRequestManager.add_invokingRequest
    (
        function onInvoke(sender, args)
        {
            MsftAjaxRequestCount++;
        }
     );

    Sys.Net.WebRequestManager.add_completedRequest
    (
        function onComplete(sender, args)
        {
            MsftAjaxRequestCount--;
        }
    );"
);

以下代码用于检查该新变量的值是否与其他 AJAX 库的其他检查一起插入

.ExecuteScript("return MsftAjaxRequestCount == '0'");

这行得通。但是有轻微的比赛条件。有时事件连接较晚并且事件未触发,或者导致值变为-1。这是因为 Selenium Webdriver 的 GotoUrl() 在 document.readyState 完成之前不会返回。

GotoUrl() 可以在完成之前异步返回以提前连接事件吗?对此有何其他想法?

【问题讨论】:

    标签: ajax asp.net-mvc web-services selenium-webdriver


    【解决方案1】:

    OP 回答了大部分 OP 问题。答案会因所使用的 AJAX 库而异。

    要从 GotoUrl() 以不同的速度返回(document.readyState == 加载或交互或完成),可以使用 Selenium 页面加载策略。 “normal”在“complete”时返回,“eager”在“interactive”时返回,“none”立即返回。

    使用 C# 中的 FireFox 驱动程序的示例如下所示。

    var profile = new FireFoxProfile();
    profile.SetPreference("webdriver.load.strategy", "eager");
    
    var driver = new FireFoxDriver(profile);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      相关资源
      最近更新 更多