blob: 188efd648376503c2fd71468f2c07e9891370753 [file] [log] [blame] [view]
Kent Tamura59ffb022018-11-27 05:30:561# Web Tests with Manual Fallback
pwnall59aadcb2017-01-26 23:27:212
3Some Blink features cannot be automatically tested using the Web Platform. Prime
4examples are the APIs that require
Mustaq Ahmed3a40f9f22022-10-11 19:16:175[user activation](https://html.spec.whatwg.org/multipage/interaction.html#tracking-user-activation)
6(also known as _a user gesture_), such as [Fullscreen](https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API).
pwnall59aadcb2017-01-26 23:27:217Automated tests for these Blink features must rely on special APIs, which are
8only exposed in testing environments, and are therefore not available in a
9normal browser session.
10
11A popular pattern used in these tests is to rely on the user to perform some
12manual steps in order to run the test case in a normal browser session. These
13tests are effectively
Philip Jägenstedt3a3d5b82018-05-31 15:25:3514[manual tests](https://web-platform-tests.org/writing-tests/manual.html), with
pwnall59aadcb2017-01-26 23:27:2115additional JavaScript code that automatically performs the desired manual steps,
16when loaded in an environment that exposes the needed testing APIs.
17
18## Motivation
19
Kent Tamura59ffb022018-11-27 05:30:5620Web tests that degrade to manual tests in the absence of testing APIs have
pwnall59aadcb2017-01-26 23:27:2121the following benefits.
22
23* The manual test component can be debugged in a normal browser session, using
24 the rich [developer tools](https://developer.chrome.com/devtools). Tests
25 without a manual fallback can only be debugged in the test runner.
26* The manual tests can run in other browsers, making it easy to check whether
27 our behavior matches other browsers.
Kent Tamura59ffb022018-11-27 05:30:5628* The web tests can form the basis for manual tests that are contributed to
foolipeda32ab2017-02-16 19:21:5829 [web-platform-tests](./web_platform_tests.md).
pwnall59aadcb2017-01-26 23:27:2130
31Therefore, the desirability of adding a manual fallback to a test heavily
32depends on whether the feature under test is a Web Platform feature or a
33Blink-only feature, and on the developer's working style. The benefits above
34should be weighed against the added design effort needed to build a manual test,
35and the size and complexity introduced by the manual fallback.
36
37## Development Tips
38
Kent Tamura59ffb022018-11-27 05:30:5639A natural workflow for writing a web test that gracefully degrades to a
pwnall59aadcb2017-01-26 23:27:2140manual test is to first develop the manual test in a browser, and then add code
41that feature-checks for testing APIs, and uses them to automate the test's
42manual steps.
43
44Manual tests should minimize the chance of user error. This implies keeping the
45manual steps to a minimum, and having simple and clear instructions that
46describe all the configuration changes and user gestures that match the effect
47of the Blink-specific APIs used by the test.
48
49## Example
50
51Below is an example of a fairly minimal test that uses a Blink-Specific API
52(`window.eventSender`), and gracefully degrades to a manual test.
53
54```html
55<!doctype html>
56<meta charset="utf-8">
57<title>DOM: Event.isTrusted for UI events</title>
58<link rel="help" href="https://dom.spec.whatwg.org/#dom-event-istrusted">
59<link rel="help" href="https://dom.spec.whatwg.org/#constructing-events">
60<meta name="assert"
61 content="Event.isTrusted is true for events generated by user interaction">
62<script src="../../resources/testharness.js"></script>
63<script src="../../resources/testharnessreport.js"></script>
64
65<p>Please click on the button below.</p>
66<button>Click Me!</button>
67
68<script>
69'use strict';
70
71setup({ explicit_timeout: true });
72
73promise_test(() => {
74 const button = document.querySelector('button');
75 return new Promise((resolve, reject) => {
76 const button = document.querySelector('button');
77 button.addEventListener('click', (event) => {
78 resolve(event);
79 });
80
81 if (window.eventSender) {
82 eventSender.mouseMoveTo(button.offsetLeft, button.offsetTop);
83 eventSender.mouseDown();
84 eventSender.mouseUp();
85 }
86 }).then((clickEvent) => {
87 assert_true(clickEvent.isTrusted);
88 });
89
90}, 'Click generated by user interaction');
91
92</script>
93```
94
95The test exhibits the following desirable features:
96
97* It has a second specification URL (`<link rel="help">`), because the paragraph
98 that documents the tested feature (referenced by the primary URL) is not very
99 informative on its own.
100* It links to the
101 [WHATWG Living Standard](https://wiki.whatwg.org/wiki/FAQ#What_does_.22Living_Standard.22_mean.3F),
102 rather than to a frozen version of the specification.
103* It contains clear instructions for manually triggering the test conditions.
104 The test starts with a paragraph (`<p>`) that tells the tester exactly what to
105 do, and the `<button>` that needs to be clicked is clearly labeled.
106* It disables the timeout mechanism built into `testharness.js` by calling
107 `setup({ explicit_timeout: true });`
108* It checks for the presence of the Blink-specific testing APIs
109 (`window.eventSender`) before invoking them. The test does not automatically
110 fail when the APIs are not present.
111* It uses [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)
112 to separate the test setup from the assertions. This is particularly helpful
113 for manual tests that depend on a sequence of events to occur, as Promises
114 offer a composable way to express waiting for asynchronous events that avoids
115 [callback hell](http://stackabuse.com/avoiding-callback-hell-in-node-js/).
116
117Notice that the test is pretty heavy compared to a minimal JavaScript test that
118does not rely on testing APIs. Only use testing APIs when the desired testing
119conditions cannot be set up using Web Platform APIs.