Skip to content

Commit

Permalink
fix: fixed hanging in NA if url has hash (#7698)
Browse files Browse the repository at this point in the history
<!--
Thank you for your contribution.

Before making a PR, please read our contributing guidelines at

https://github.com/DevExpress/testcafe/blob/master/CONTRIBUTING.md#code-contribution

We recommend creating a *draft* PR, so that you can mark it as 'ready
for review' when you are done.
-->
[closes #7652]

## Purpose
Fix hanging

## Approach
Force page reloading page between tests in NA if URL has a hash.

## References
#7652

## Pre-Merge TODO
- [x] Write tests for your proposed changes
- [ ] Make sure that existing tests do not fail
  • Loading branch information
Aleksey28 committed May 16, 2023
1 parent 89479a2 commit 9808995
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/client/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 onlyHashChanged = hashIndex !== -1 && command.url.slice(0, hashIndex) === LOCATION_HREF.slice(0, hashIndex);

document.location = command.url;

if (opts.nativeAutomation && onlyHashChanged)
document.location.reload();
}

function nativeAutomationCheckRedirecting ({ result }) {
Expand All @@ -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 };
}
Expand Down
18 changes: 15 additions & 3 deletions src/client/driver/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -1535,7 +1541,13 @@ export default class Driver extends serviceUtils.EventEmitter {
storages.clear();
storages.lock();

browser.redirect(command, hammerhead.createNativeXHR, this.communicationUrls.openFileProtocolUrl);
const redirectOpts = {
createXHR: hammerhead.createNativeXHR,
openFileProtocolUrl: this.communicationUrls.openFileProtocolUrl,
nativeAutomation: this.options.nativeAutomation,
};

browser.redirect(command, redirectOpts);
}
else {
this.contextStorage.setItem(TEST_DONE_SENT_FLAG, false);
Expand Down
4 changes: 4 additions & 0 deletions test/functional/fixtures/driver/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Original file line number Diff line number Diff line change
@@ -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 () => {
});

0 comments on commit 9808995

Please sign in to comment.