-
Notifications
You must be signed in to change notification settings - Fork 830
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25f982b
commit 9dc95c5
Showing
12 changed files
with
179 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
|
||
// Store local references of these globals. | ||
const {webdriver} = global.__workbox; | ||
|
||
/** | ||
* Executes the passed function (and args) async and logs any errors that | ||
* occur. Errors are assumed to be passed to the callback as an object | ||
* with the `error` property. | ||
* | ||
* @param {...*} args | ||
* @return {*} | ||
*/ | ||
const executeAsyncAndCatch = async (...args) => { | ||
const result = await webdriver.executeAsyncScript(...args); | ||
|
||
if (result && result.error) { | ||
console.error(result.error); | ||
throw new Error('Error executing async script'); | ||
} | ||
return result; | ||
}; | ||
|
||
module.exports = {executeAsyncAndCatch}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
|
||
// Store local references of these globals. | ||
const {webdriver} = global.__workbox; | ||
|
||
/** | ||
* Gets the window handle of the last openned tab. | ||
* | ||
* @return {string} | ||
*/ | ||
const getLastWindowHandle = async () => { | ||
const allHandles = await webdriver.getAllWindowHandles(); | ||
return allHandles[allHandles.length - 1]; | ||
}; | ||
|
||
module.exports = {getLastWindowHandle}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
|
||
const {getLastWindowHandle} = require('./getLastWindowHandle'); | ||
|
||
// Store local references of these globals. | ||
const {webdriver} = global.__workbox; | ||
|
||
/** | ||
* Opens a new window for the passed URL. If no URL is passed, a blank page | ||
* is opened. | ||
* | ||
* @param {string} url | ||
* @param {Object} options | ||
* @return {string} | ||
*/ | ||
const openNewTab = async (url) => { | ||
await webdriver.executeAsyncScript((url, cb) => { | ||
window.open(url); | ||
cb(); | ||
}, url); | ||
|
||
const lastHandle = await getLastWindowHandle(); | ||
await webdriver.switchTo().window(lastHandle); | ||
|
||
// Return the handle of the window that was just opened. | ||
return lastHandle; | ||
}; | ||
|
||
module.exports = {openNewTab}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
|
||
const {executeAsyncAndCatch} = require('./executeAsyncAndCatch'); | ||
|
||
/** | ||
* Unregisters any active SWs so the next page load can start clean. | ||
* Note: a new page load is needed before controlling SWs stop being active. | ||
*/ | ||
const unregisterAllSws = async () => { | ||
await executeAsyncAndCatch(async (cb) => { | ||
try { | ||
const regs = await navigator.serviceWorker.getRegistrations(); | ||
for (const reg of regs) { | ||
await reg.unregister(); | ||
} | ||
cb(); | ||
} catch (error) { | ||
cb({error: error.stack}); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = {unregisterAllSws}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
|
||
const {executeAsyncAndCatch} = require('./executeAsyncAndCatch'); | ||
|
||
/** | ||
* Waits for the current window to load if it's not already loaded. | ||
*/ | ||
const windowLoaded = async () => { | ||
// Wait for the window to load, so the `Workbox` global is available. | ||
await executeAsyncAndCatch(async (cb) => { | ||
try { | ||
if (document.readyState === 'complete') { | ||
cb(); | ||
} else { | ||
addEventListener('load', () => cb()); | ||
} | ||
} catch (error) { | ||
cb({error: error.stack}); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = {windowLoaded}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters