[webkit-dev] Waiting for an event in layout test...

Ryosuke Niwa rniwa at webkit.org
Thu May 30 23:01:48 PDT 2019

I’m gonna give you a game changing function:

function listenForEventOnce(target, name, timeout) {
    return new Promise((resolve, reject) => {
        const timer = timeout ? setTimeout(reject, timeout) : null;
        target.addEventListener(name, () => {
            if (timer)
        }, {once: true});

You can then write a test like this:
await listenForEventOnce(document.body, 'load');
// do stuff after load event.

await listenForEventOnce(document.querySelector('input'), 'focus');
await listenForEventOnce(visualViewport, 'scroll', 5000);
// After the input element is focused, then the visual viewport scrolled or
5 seconds has passed.

- R. Niwa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20190530/65536ddf/attachment.html>

More information about the webkit-dev mailing list