【问题标题】:iOS UI Automation wait until web view is ready and renderediOS UI 自动化等到 Web 视图准备好并呈现
【发布时间】:2015-04-13 21:24:06
【问题描述】:

我正在使用 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 完全加载,然后截屏并继续脚本的其余部分?

【问题讨论】:

    标签: javascript ios webview ui-automation ios-ui-automation


    【解决方案1】:

    您可以使用计算样式,放置一些惰性样式属性(我使用剪辑),并且您可以每隔 1 秒检查一次(如果您使用 css 文件,最好用它创建一个类并在元素上使用该类)。

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

    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

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

    希望对你有帮助...

    【讨论】:

      【解决方案2】:

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

      你会做这样的事情来等待 webview 加载最多 10 秒:

      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();
      

      【讨论】:

      • 你改名字了吗?我点击链接时收到 404。
      • Apple 弃用了基于 Javascript 的 UIAutomation 工具,转而支持 XCtest,因此更新了 Illuminator 并改为基于该框架。 Extensions.js 的最新版本应该在这个提交中:github.com/paypal/Illuminator/blob/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多