【问题标题】:Selenium's Open function waits until what?Selenium 的 Open 函数等到什么?
【发布时间】:2023-03-20 03:45:01
【问题描述】:

当我在 Selenium 中调用 Open("[some URL]") 方法时,它会等到所有资源(图像、CSS、JS)都下载完毕。但是它怎么知道浏览器请求资源完成了呢?

这是我的驱动程序代码(用 C# 编写)的 sn-p:

DefaultSelenium selenium = new DefaultSelenium(
    "localhost", 4444, "*iexplore", "http://www.stackoverflow.com");

// open my profile page
selenium.Open("http://stackoverflow.com/users/111934/jeff-meatball-yang");

以上网址仅供参考。如果我在 setTimeout() 主体 onload 事件处理程序中触发了一个很长的延迟包裹的 AJAX 调用,Selenium 实际上不会在继续之前等待该 AJAX 调用......我必须手动告诉Selenium 使用 WaitForPageToLoad(timeout) 等待。

那么 Selenium 如何检测正常页面(没有 AJAX)何时完成加载?

【问题讨论】:

    标签: unit-testing selenium


    【解决方案1】:

    我相信它会等待 location.href 可用,这意味着如果您在此之后运行 AJAX,您还需要使用 waitForCondition。

    selenium.open("http://www.example.com/");
    selenium.waitForCondition("var value = selenium.getText('id=pageLoadStatus'); value == 'Loaded'", "60000");
    

    以下是来自 Selenium 核心的当前页面加载检测代码:

    this.recordPageLoad = function(elementOrWindow) {
        LOG.debug("Page load detected");
        try {
            if (elementOrWindow.location && elementOrWindow.location.href) {
                LOG.debug("Page load location=" + elementOrWindow.location.href);
            } else if (elementOrWindow.contentWindow && elementOrWindow.contentWindow.location && elementOrWindow.contentWindow.location.href) {
                LOG.debug("Page load location=" + elementOrWindow.contentWindow.location.href);
            } else {
                LOG.debug("Page load location unknown, current window location=" + this.getCurrentWindow(true).location);
            }
        } catch (e) {
            LOG.error("Caught an exception attempting to log location; this should get noticed soon!");
            LOG.exception(e);
            self.pageLoadError = e;
            return;
        }
        self.newPageLoaded = true;
    };
    

    【讨论】:

    【解决方案2】:

    @Dave Hunt,you're correct that recordPageLoad is called,但它仅记录页面加载位置 (location.href)(如果可用)并设置 self.newPageLoaded = trueisNewPageLoaded 返回的值,因此,doOpen

    isNewPageLoadedselenium-api.js 中被_isNewPageLoaded makePageLoadCondition doOpen 调用。并且,doOpen 在 Selenium IDE 运行 open 命令后被调用。

    相关的痕迹是:

    doOpen 调用(移动到selenium-browserbot.jsopenLocation => getCurrentWindow => _modifyWindow => modifySeparateTestWindowToDetectPageLoads(根据上面的评论,等待“通过不断轮询直到文档更改并且已完全加载") => pollForLoad => getReadyState

    getReadyState 返回document.readyState (rs),而pollForLoad 等到rs == 'complete',即,直到页面完全加载,图像和所有内容。

    Ace Ventura 解释得比我还好。


    附: @Dave Hunt,感谢您的 Selenium IDE Flow Control script on GitHub。它派上用场了! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-31
      • 2023-01-27
      • 2021-12-16
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多