From 52da69ccaf643f319ba8723be43043537778dce3 Mon Sep 17 00:00:00 2001 From: AlexKamaev Date: Tue, 21 Nov 2023 16:36:02 +0400 Subject: [PATCH] fix service-worker in native automation (#8084) --- .../request-pipeline/index.ts | 12 +++--- .../regression/gh-8054/pages/index.html | 38 +++++++++++++++++++ .../fixtures/regression/gh-8054/pages/sw.js | 18 +++++++++ .../fixtures/regression/gh-8054/test.js | 7 ++++ .../gh-8054/testcafe-fixtures/index.js | 13 +++++++ 5 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 test/functional/fixtures/regression/gh-8054/pages/index.html create mode 100644 test/functional/fixtures/regression/gh-8054/pages/sw.js create mode 100644 test/functional/fixtures/regression/gh-8054/test.js create mode 100644 test/functional/fixtures/regression/gh-8054/testcafe-fixtures/index.js diff --git a/src/native-automation/request-pipeline/index.ts b/src/native-automation/request-pipeline/index.ts index a0529b340db..b30ed06a87b 100644 --- a/src/native-automation/request-pipeline/index.ts +++ b/src/native-automation/request-pipeline/index.ts @@ -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 { @@ -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 () => { diff --git a/test/functional/fixtures/regression/gh-8054/pages/index.html b/test/functional/fixtures/regression/gh-8054/pages/index.html new file mode 100644 index 00000000000..736a7fe18cf --- /dev/null +++ b/test/functional/fixtures/regression/gh-8054/pages/index.html @@ -0,0 +1,38 @@ + + + + + gh-8054 + + +

Service worker

+ + + + diff --git a/test/functional/fixtures/regression/gh-8054/pages/sw.js b/test/functional/fixtures/regression/gh-8054/pages/sw.js new file mode 100644 index 00000000000..736e2119827 --- /dev/null +++ b/test/functional/fixtures/regression/gh-8054/pages/sw.js @@ -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; + }) + ); + } +}); diff --git a/test/functional/fixtures/regression/gh-8054/test.js b/test/functional/fixtures/regression/gh-8054/test.js new file mode 100644 index 00000000000..9a28eac0c17 --- /dev/null +++ b/test/functional/fixtures/regression/gh-8054/test.js @@ -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'); + }); +}); diff --git a/test/functional/fixtures/regression/gh-8054/testcafe-fixtures/index.js b/test/functional/fixtures/regression/gh-8054/testcafe-fixtures/index.js new file mode 100644 index 00000000000..f8cc4ea807d --- /dev/null +++ b/test/functional/fixtures/regression/gh-8054/testcafe-fixtures/index.js @@ -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'); +});