From 4745e325bfbc633439dd5703e1ff5722b0125c2b Mon Sep 17 00:00:00 2001 From: Popov Aleksey Date: Fri, 12 May 2023 10:29:28 +0300 Subject: [PATCH 1/4] test: add test Should force page reload if url has # in Native Automation --- test/functional/fixtures/driver/test.js | 6 +++++- .../driver/testcafe-fixtures/page-url-with-hash.js | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/functional/fixtures/driver/testcafe-fixtures/page-url-with-hash.js diff --git a/test/functional/fixtures/driver/test.js b/test/functional/fixtures/driver/test.js index 6b691c26cd8..c263caecece 100644 --- a/test/functional/fixtures/driver/test.js +++ b/test/functional/fixtures/driver/test.js @@ -30,7 +30,7 @@ describe('TestRun - Driver protocol', function () { return runTests('./testcafe-fixtures/prevent-real-action-test.js', 'Perform native click'); }); - describe('Regression', function () { + describe.only('Regression', function () { it('Should allow mixed execution order (GH-564)', function () { return runTests('./testcafe-fixtures/driver-test.js', 'Mixed execution order'); }); @@ -47,5 +47,9 @@ describe('TestRun - Driver protocol', function () { onlyInNativeAutomation('Should clear out the localStorage and sessionStorage for multiple domains(native automation)', function () { return runTests('./testcafe-fixtures/clear-and-lock-storages-native-automation-part-2.js'); }); + + it('Should force page reload if url has # in Native Automation', function () { + return runTests('./testcafe-fixtures/page-url-with-hash.js'); + }); }); }); diff --git a/test/functional/fixtures/driver/testcafe-fixtures/page-url-with-hash.js b/test/functional/fixtures/driver/testcafe-fixtures/page-url-with-hash.js new file mode 100644 index 00000000000..64ba0c5ddc1 --- /dev/null +++ b/test/functional/fixtures/driver/testcafe-fixtures/page-url-with-hash.js @@ -0,0 +1,13 @@ +fixture `Driver`; + +test.page('http://localhost:3000/fixtures/driver/pages/empty-page.html/#/')('Test 1', async () => { +}); + +test.page('http://localhost:3000/fixtures/driver/pages/empty-page.html/#/')('Test 2', async () => { +}); + +test.page('http://localhost:3000/fixtures/driver/pages/empty-page.html/#/test')('Test 3', async () => { +}); + +test.page('http://localhost:3000/fixtures/driver/pages/empty-page.html/#/')('Test 4', async () => { +}); From ab0eed0e1f35d10b755cd48ad8ac919dd4107c84 Mon Sep 17 00:00:00 2001 From: Popov Aleksey Date: Fri, 12 May 2023 10:29:52 +0300 Subject: [PATCH 2/4] fix: fixed hanging in NA if url has hash --- src/client/browser/index.js | 20 +++++++++++++++----- src/client/driver/driver.js | 8 +++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/client/browser/index.js b/src/client/browser/index.js index 16e0a5803f6..5b6b19649d2 100644 --- a/src/client/browser/index.js +++ b/src/client/browser/index.js @@ -104,17 +104,27 @@ export function stopInitScriptExecution () { allowInitScriptExecution = false; } -export function redirectUsingCdp (command, createXHR, openFileProtocolUrl) { +export function redirectUsingCdp (command, { openFileProtocolUrl, createXHR }) { sendXHR(openFileProtocolUrl, createXHR, { method: 'POST', data: JSON.stringify({ url: command.url }) }); //eslint-disable-line no-restricted-globals } -export function redirect (command, createXHR, openFileProtocolUrl) { +export function redirect (command, opts) { stopInitScriptExecution(); if (isFileProtocol(command.url)) - redirectUsingCdp(command, createXHR, openFileProtocolUrl); + redirectUsingCdp(command, opts); else - document.location = command.url; + setLocation(command, opts); +} + +function setLocation (command, opts) { + const hashIndex = command.url.indexOf('#'); + const sameUrlBeforeHash = hashIndex !== -1 && command.url.slice(0, hashIndex) === LOCATION_HREF.slice(0, hashIndex); + + document.location = command.url; + + if (opts.nativeAutomation && sameUrlBeforeHash) + document.location.reload(); } function nativeAutomationCheckRedirecting ({ result }) { @@ -136,7 +146,7 @@ async function getStatus ({ statusUrl, openFileProtocolUrl }, createXHR, { manua const redirecting = nativeAutomation ? nativeAutomationCheckRedirecting({ result }) : regularCheckRedirecting(result); if (redirecting && !manualRedirect) - redirect(result, createXHR, openFileProtocolUrl); + redirect(result, { createXHR, openFileProtocolUrl, nativeAutomation }); return { command: result, redirecting }; } diff --git a/src/client/driver/driver.js b/src/client/driver/driver.js index 32c058fe225..152356fd8d3 100644 --- a/src/client/driver/driver.js +++ b/src/client/driver/driver.js @@ -1535,7 +1535,13 @@ export default class Driver extends serviceUtils.EventEmitter { storages.clear(); storages.lock(); - browser.redirect(command, hammerhead.createNativeXHR, this.communicationUrls.openFileProtocolUrl); + const opts = { + createXHR: hammerhead.createNativeXHR, + openFileProtocolUrl: this.communicationUrls.openFileProtocolUrl, + nativeAutomation: this.options.nativeAutomation, + }; + + browser.redirect(command, opts); } else { this.contextStorage.setItem(TEST_DONE_SENT_FLAG, false); From 71bee6cf7e8f1f1756b283efcf133a357680b695 Mon Sep 17 00:00:00 2001 From: Popov Aleksey Date: Fri, 12 May 2023 10:51:46 +0300 Subject: [PATCH 3/4] fix: review fixes --- src/client/browser/index.js | 6 +++--- test/functional/fixtures/driver/test.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/client/browser/index.js b/src/client/browser/index.js index 5b6b19649d2..f3fd13c4737 100644 --- a/src/client/browser/index.js +++ b/src/client/browser/index.js @@ -118,12 +118,12 @@ export function redirect (command, opts) { } function setLocation (command, opts) { - const hashIndex = command.url.indexOf('#'); - const sameUrlBeforeHash = hashIndex !== -1 && command.url.slice(0, hashIndex) === LOCATION_HREF.slice(0, hashIndex); + const hashIndex = command.url.indexOf('#'); + const onlyHashChanged = hashIndex !== -1 && command.url.slice(0, hashIndex) === LOCATION_HREF.slice(0, hashIndex); document.location = command.url; - if (opts.nativeAutomation && sameUrlBeforeHash) + if (opts.nativeAutomation && onlyHashChanged) document.location.reload(); } diff --git a/test/functional/fixtures/driver/test.js b/test/functional/fixtures/driver/test.js index c263caecece..eca6f2ccd32 100644 --- a/test/functional/fixtures/driver/test.js +++ b/test/functional/fixtures/driver/test.js @@ -30,7 +30,7 @@ describe('TestRun - Driver protocol', function () { return runTests('./testcafe-fixtures/prevent-real-action-test.js', 'Perform native click'); }); - describe.only('Regression', function () { + describe('Regression', function () { it('Should allow mixed execution order (GH-564)', function () { return runTests('./testcafe-fixtures/driver-test.js', 'Mixed execution order'); }); From d5ecc342f2e888b2e50cb5b7d92e37b0a9e1cdd2 Mon Sep 17 00:00:00 2001 From: Popov Aleksey Date: Fri, 12 May 2023 17:13:48 +0300 Subject: [PATCH 4/4] fix: fixed --- src/client/driver/driver.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/client/driver/driver.js b/src/client/driver/driver.js index 152356fd8d3..7a20f9b50e2 100644 --- a/src/client/driver/driver.js +++ b/src/client/driver/driver.js @@ -1268,8 +1268,14 @@ export default class Driver extends serviceUtils.EventEmitter { _onNavigateToCommand (command) { this.contextStorage.setItem(this.COMMAND_EXECUTING_FLAG, true); - if (this.options.nativeAutomation && isFileProtocol(command.url)) - browser.redirectUsingCdp(command, hammerhead.createNativeXHR, this.communicationUrls.openFileProtocolUrl); + if (this.options.nativeAutomation && isFileProtocol(command.url)) { + const redirectOpts = { + createXHR: hammerhead.createNativeXHR, + openFileProtocolUrl: this.communicationUrls.openFileProtocolUrl, + }; + + browser.redirectUsingCdp(command, redirectOpts); + } else { executeNavigateToCommand(command) .then(driverStatus => { @@ -1535,13 +1541,13 @@ export default class Driver extends serviceUtils.EventEmitter { storages.clear(); storages.lock(); - const opts = { + const redirectOpts = { createXHR: hammerhead.createNativeXHR, openFileProtocolUrl: this.communicationUrls.openFileProtocolUrl, nativeAutomation: this.options.nativeAutomation, }; - browser.redirect(command, opts); + browser.redirect(command, redirectOpts); } else { this.contextStorage.setItem(TEST_DONE_SENT_FLAG, false);