-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix service-worker in native automation (#8084)
- Loading branch information
1 parent
3461df1
commit 52da69c
Showing
5 changed files
with
83 additions
and
5 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
38 changes: 38 additions & 0 deletions
38
test/functional/fixtures/regression/gh-8054/pages/index.html
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,38 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>gh-8054</title> | ||
</head> | ||
<body> | ||
<h1>Service worker</h1> | ||
<button>click me</button> | ||
<script> | ||
const registerServiceWorker = async () => { | ||
if ("serviceWorker" in navigator) { | ||
try { | ||
const serviceWorkerUrl = '/fixtures/regression/gh-8054/pages/sw.js'; | ||
|
||
await navigator.serviceWorker.register(serviceWorkerUrl, { | ||
scope: "/fixtures/regression/gh-8054/pages/", | ||
}); | ||
} catch (err) { | ||
} | ||
|
||
navigator.serviceWorker.addEventListener('message', | ||
(event) => { | ||
if (event.data && event.data.result === 'success') { | ||
document.querySelector('h1').innerHTML = 'Success'; | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
registerServiceWorker(); | ||
|
||
document.querySelector('button').addEventListener('click', () => { | ||
fetch('/?test' + Date.now()); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
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,18 @@ | ||
self.addEventListener('fetch', event => { | ||
if (event.request.url.indexOf('?test') > -1) { | ||
event.respondWith( | ||
fetch('http://localhost:3000/fixtures/regression/gh-8054/pages/index.html') | ||
.then(response => { | ||
self.clients.matchAll().then((clients) => { | ||
clients.forEach((client) => { | ||
client.postMessage({ | ||
result: 'success', | ||
}); | ||
}); | ||
}); | ||
|
||
return response; | ||
}) | ||
); | ||
} | ||
}); |
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,7 @@ | ||
const { onlyInNativeAutomation } = require('../../../utils/skip-in'); | ||
|
||
describe('Should not ignore request from service worker (GH-8054)', function () { | ||
onlyInNativeAutomation('Should not ignore request from service worker (GH-8054)', function () { | ||
return runTests('./testcafe-fixtures/index.js'); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
test/functional/fixtures/regression/gh-8054/testcafe-fixtures/index.js
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,13 @@ | ||
import { Selector } from 'testcafe'; | ||
|
||
fixture `GH-8054 - Should not ignore request from service worker` | ||
.page `http://localhost:3000/fixtures/regression/gh-8054/pages/index.html`; | ||
|
||
test(`Recreate invisible element and click`, async t => { | ||
// NOTE: we need this line for the test. For some reason | ||
// service worker is not registered on first loading | ||
await t.eval(() => window.location.reload()); | ||
|
||
await t.click('button'); | ||
await t.expect(Selector('h1').innerText).eql('Success'); | ||
}); |