|
| 1 | +# RFC 89: `testdriver` Add an `execute_script` method. |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +Add an `execute_script` method to testdriver that takes the source |
| 6 | +text of the script, an async argument, and the context id to execute |
| 7 | +it it. This follows WebDriver semantics, and the returned promise is |
| 8 | +resolved once the script has a result. |
| 9 | + |
| 10 | +## Details |
| 11 | + |
| 12 | +Sometimes a test window wants to check the status of something in a |
| 13 | +remote window. It's possible to do this in a number of ways, for |
| 14 | +example by posting messages from the remote window to the test |
| 15 | +window. However in cross-origin cases messge passing on the js side |
| 16 | +can be difficult or impossible. In addition when tests are defined in |
| 17 | +the test window, it's useful to avoid putting complex logic in other |
| 18 | +browsing contexts because these won't have the same error handling |
| 19 | +properties as the test window. |
| 20 | + |
| 21 | +A simple way to solve these problems is to provide a mechansim to |
| 22 | +execute script in a different context. This isn't possible using |
| 23 | +content APIs alone, but is possible in WebDriver, and we can reuse |
| 24 | +that to define a `execute_script` function that works across contexts: |
| 25 | + |
| 26 | +``` |
| 27 | +execute_script(body, async, context) |
| 28 | +``` |
| 29 | + |
| 30 | +`body` is a function body for the script to execute. `async` controls |
| 31 | +whether we return the return value of the function directly, or |
| 32 | +require a callback on the remote side to be called with the return |
| 33 | +value; when it's false we use the [Execute |
| 34 | +Script](https://w3c.github.io/webdriver/#execute-script) semantics and |
| 35 | +use the return value of the function directly, when it's true we use |
| 36 | +the [Execute Async |
| 37 | +Script](https://w3c.github.io/webdriver/#execute-async-script) |
| 38 | +semantics in which the final argument passed to the implicit wrapper |
| 39 | +function is a callback, and the call returns with the value this |
| 40 | +function is called with. `context` is an object that can be resolved |
| 41 | +as a testdriver context id to identify the remote context (see [RFC |
| 42 | +88](https://github.com/web-platform-tests/rfcs/pull/88). The return |
| 43 | +value is a promise that resolves to the value of the script. |
| 44 | + |
| 45 | +For example: |
| 46 | + |
| 47 | +``` |
| 48 | +<iframe src="child.html"></iframe> |
| 49 | +<script> |
| 50 | +setup({single_test: true}); |
| 51 | +onload = { |
| 52 | + let value = test_driver.execute_script("return document.body.textContent", false, iframe.contentWindow); |
| 53 | + assert_equals(value, "PASS"); |
| 54 | + done(); |
| 55 | +} |
| 56 | +</script> |
| 57 | +``` |
| 58 | + |
| 59 | +## Risks |
| 60 | + |
| 61 | +The async scripting API from testdriver is a bit akward. Maybe we |
| 62 | +should consider automatically adding a local variable called e.g. |
| 63 | +`callback` rather than requring each author to do the `let callback = |
| 64 | +arguments[arguments.length - 1]` dance manually. |
| 65 | + |
| 66 | +## References |
| 67 | + |
| 68 | +[PR 29803](https://github.com/web-platform-tests/wpt/pull/29803) |
| 69 | +contains a prototype implementation of this. |
0 commit comments