Changes between Version 3 and Version 4 of Writing Layout Tests to test iOS UI features


Ignore:
Timestamp:
Sep 30, 2015 3:23:53 PM (9 years ago)
Author:
Simon Fraser
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Writing Layout Tests to test iOS UI features

    v3 v4  
    88
    99= A simple example =
     10
     11{{{
     12
     13<script>
     14function runTest()
     15{
     16    testRunner.waitUntilDone();
     17    testRunner.runUIScript("(function() { return uiController.zoomScale; })()", function(result) {
     18        console.log("Got zoom scale " + result);
     19    });
     20}
     21</script>
     22<body onload="runTest()"></body>
     23
     24}}}
     25
     26= Some more detail =
    1027
    1128Let's say we want to write a test that reads back the zoom scale of the WKWebView's UIScrollView (e.g. to test viewport scale).
     
    5168...
    5269</script>
    53 
    5470<script>
    55     function getUIScript()
    56     {
     71    function getUIScript() {
    5772        return document.getElementById('ui-script').text;
    5873    }
     
    6176        ...
    6277    });
     78</script>
    6379}}}
     80
     81So, what script can you run in the UI process? This UI-side script runs in a separate JS context that has no access to the DOM, no timers, no requestAnimationFrame, no XHR etc. What it can do is run vanilla JavaScript, and access properties and functions on [https://trac.webkit.org/browser/trunk/Tools/WebKitTestRunner/UIScriptContext/Bindings/UIScriptController.idl UIScriptController], which is exposed as uiController. Here's a simple example, using function-like syntax for the inline script, that returns zoom scale:
     82
     83{{{
     84<script>
     85    testRunner.runUIScript("(function() { return uiController.zoomScale; })()", function(result) {
     86        console.log("Got zoom scale " + result);
     87    });
     88</script>
     89}}}
     90
     91Because testRunner.runUIScript() is asynchronous, the caller has to supply a callback function. The string result returned by the UI process script is passed to this callback function as an argument. You can use this to return simple bits of data as strings, or use JSON.stringify() in the UI script, and JSON.parse() in the test script to get more complex objects across the process boundary.