iOS UI 自动化等待 web 视图准备好并呈现

iOS UI Automation wait until web view is ready and rendered

我正在使用 Instruments 创建一个 iOS UI 自动化 javascript 来自动为我的 iOS 应用截取屏幕截图。我用来自动截屏的工具是 Snapshot.

我正在为我的应用程序的一部分使用 webview,我想在继续执行脚本的其余部分之前截取完全呈现的 webview 的屏幕截图。 所以目前我的脚本看起来像:

var target = UIATarget.localTarget();
target.frontMostApp().mainWindow().scrollViews()[0].webViews()[0].links()[0].tap();
// take screen shot here
target.frontMostApp().navigationBar().leftButton().tap();

但是当它拍摄屏幕截图时,webview 没有完全呈现,所以它拍摄了一个空白屏幕并返回到主屏幕。 有没有办法等到 webview 完全加载,然后截屏并继续脚本的其余部分?

你可以使用computed style,放一些inert style 属性(我用的是clip),你可以间隔1秒检查(如果你使用css文件最好创建一个class 并在元素上使用 class).

我使用下面的函数来计算样式。

function getStyle (el, prop) {
    try {
        if (getComputedStyle !== undefined) {
            return getComputedStyle(el, null).getPropertyValue(prop);
        }
        else {
            return el.currentStyle[prop];
        }
    } catch (e) {
        return el.currentStyle[prop];
    }
}

定时器你可以试试this

请注意计算样式可能因浏览器而异。

希望对您有所帮助...

Illuminator 框架(完全公开,我写的)在其 Extensions.js 中提供了一个名为 waitForChildExistence() 的函数,它允许您不断评估对元素的引用,直到它实际出现。

您可以像这样等待 10 秒来加载 webview:

var webView = target.frontMostApp().mainWindow().scrollViews()[0].webViews()[0];
webView.tap();

webView.waitForChildExistence(10, true, "a specific link in the web view", function(wb) {
    // the argument wb will contain the reference to our var webView
    return wb.links()["the link you are waiting for"];
});

// take screen shot here
target.frontMostApp().navigationBar().leftButton().tap();