Skip to content

Commit

Permalink
fix service-worker in native automation (#8084)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKamaev authored Nov 21, 2023
1 parent 3461df1 commit 52da69c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/native-automation/request-pipeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ const ALL_REQUEST_REQUESTS = { requestStage: 'Response' } as RequestPattern;
const ALL_REQUESTS_DATA = [ALL_REQUEST_REQUESTS, ALL_REQUEST_RESPONSES];

const TARGET_INFO_TYPE = {
iframe: 'iframe',
worker: 'worker',
iframe: 'iframe',
worker: 'worker',
serviceWorker: 'service_worker',
};

export default class NativeAutomationRequestPipeline extends NativeAutomationApiBase {
Expand Down Expand Up @@ -401,10 +402,11 @@ export default class NativeAutomationRequestPipeline extends NativeAutomationApi
// to intercept some requests. We need to use the `sessionId` option
// in continueRequest/continueResponse/fulfillRequest methods
await this._client.Target.on('attachedToTarget', async event => {
const isIFrame = event.targetInfo.type === TARGET_INFO_TYPE.iframe;
const isWorker = event.targetInfo.type === TARGET_INFO_TYPE.worker;
const isIFrame = event.targetInfo.type === TARGET_INFO_TYPE.iframe;
const isWorker = event.targetInfo.type === TARGET_INFO_TYPE.worker;
const isServiceWorker = event.targetInfo.type === TARGET_INFO_TYPE.serviceWorker;

if (!isIFrame && !isWorker)
if (!isIFrame && !isWorker && !isServiceWorker)
return;

await connectionResetGuard(async () => {
Expand Down
38 changes: 38 additions & 0 deletions test/functional/fixtures/regression/gh-8054/pages/index.html
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>
18 changes: 18 additions & 0 deletions test/functional/fixtures/regression/gh-8054/pages/sw.js
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;
})
);
}
});
7 changes: 7 additions & 0 deletions test/functional/fixtures/regression/gh-8054/test.js
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');
});
});
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');
});

0 comments on commit 52da69c

Please sign in to comment.