Search code examples
javascriptioswebviewui-automationios-ui-automation

iOS UI Automation wait until web view is ready and rendered


I am creating an iOS UI Automation javascript using Instruments to automate taking a screenshot for my iOS app. The tool I am using to automate taking a screenshot is Snapshot.

I am using a webview for part of my app and I want to take a screenshot of fully rendered webview before proceeding to the rest of the script. So currently my script looks like:

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

But when it takes the screen shot, the webview was not fully rendered so it's taking an empty screen and go back to the main screen. Is there a way to wait until the webview is fully loaded, then take a screen shot and continue rest of the script?


Solution

  • The Illuminator framework (full disclosure, I wrote it) provides a function called waitForChildExistence() in its Extensions.js that allows you to continuously evaluate a reference to an element until it actually appears.

    You would do something like this to wait up to 10 seconds for the webview to load:

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