Skip to content

Commit

Permalink
Add a check to wait for the resulting client ID (#2210)
Browse files Browse the repository at this point in the history
* Add a check to wait for the resulting client ID

* Fix some tests.

* Explicitly switch windows.

* Explicitly fail if we can't get a window

* Add additional debug logging to test

* Removing debugging code

* Skip flaky test in Safari
  • Loading branch information
philipwalton authored and jeffposnick committed Sep 26, 2019
1 parent bfd5c71 commit 5fc46f0
Show file tree
Hide file tree
Showing 16 changed files with 522 additions and 80 deletions.
20 changes: 20 additions & 0 deletions infra/testing/server/templates/integration.html.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<!--
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
-->
<html>
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
<script>
{{ script | safe }}
</script>
</head>
<body>
{{ body }}
</body>
</html>
37 changes: 33 additions & 4 deletions infra/testing/webdriver/getLastWindowHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,45 @@


// Store local references of these globals.
const {webdriver} = global.__workbox;
const {server, webdriver} = global.__workbox;

const testServerOrigin = server.getAddress();

/**
* Gets the window handle of the last openned tab.
* Gets the window handle of the last opened tab.
*
* @return {string}
*/
const getLastWindowHandle = async () => {
const allHandles = await webdriver.getAllWindowHandles();
return allHandles[allHandles.length - 1];
let lastWindowHandle;

// Save the handle so that we can switch back before returning.
const currentWindowHandle = await webdriver.getWindowHandle();

const allWindowHandles = await webdriver.getAllWindowHandles();
// reverse() the list so that we will iterate through the last one first.
allWindowHandles.reverse();

for (const handle of allWindowHandles) {
await webdriver.switchTo().window(handle);
const currentUrl = await webdriver.getCurrentUrl();
if (currentUrl.startsWith(testServerOrigin)) {
lastWindowHandle = handle;
break;
} else {
// Used for debugging failing tests with unexpected windows openning.
// eslint-disable-next-line no-console
console.log(`Unexpected window opened: ${currentUrl}`);
}
}

await webdriver.switchTo().window(currentWindowHandle);
if (lastWindowHandle) {
return lastWindowHandle;
}

// If we can't find anything, treat that as a fatal error.
throw new Error(`Unable to a window with origin ${testServerOrigin}.`);
};

module.exports = {getLastWindowHandle};
13 changes: 11 additions & 2 deletions infra/testing/webdriver/windowLoaded.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ const {executeAsyncAndCatch} = require('./executeAsyncAndCatch');
const windowLoaded = async () => {
// Wait for the window to load, so the `Workbox` global is available.
await executeAsyncAndCatch(async (cb) => {
const loaded = () => {
if (!window.Workbox) {
const error = new Error('Workbox not yet loaded...');
cb({error: error.stack});
} else {
cb();
}
};

try {
if (document.readyState === 'complete') {
cb();
loaded();
} else {
addEventListener('load', () => cb());
addEventListener('load', () => loaded());
}
} catch (error) {
cb({error: error.stack});
Expand Down
Loading

0 comments on commit 5fc46f0

Please sign in to comment.