Skip to content

Commit

Permalink
should restore cookie correctly when using useRole with the preserveU…
Browse files Browse the repository at this point in the history
…rl option (closes #2282) (#2285)

* should restore cookie correctly when using useRole with the preserveUrl option (closes #2282)

* changes after request
  • Loading branch information
AlexKamaev committed Apr 11, 2018
1 parent 87ff50b commit f773c21
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/test-run/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ export default class TestRunBookmark {
}
}

async _restorePage (url, storages) {
var navigateCommand = new NavigateToCommand({ url, storages });
async _restorePage (url, stateSnapshot) {
var navigateCommand = new NavigateToCommand({ url, stateSnapshot });

await this.testRun.executeCommand(navigateCommand);
}

async restore (callsite, storages) {
async restore (callsite, stateSnapshot) {
var prevPhase = this.testRun.phase;

this.testRun.phase = TEST_RUN_PHASE.inBookmarkRestore;
Expand All @@ -107,7 +107,7 @@ export default class TestRunBookmark {
var preserveUrl = this.role.opts.preserveUrl;
var url = preserveUrl ? this.role.url : this.url;

await this._restorePage(url, JSON.stringify(storages));
await this._restorePage(url, JSON.stringify(stateSnapshot));

if (!preserveUrl)
await this._restoreWorkingFrame();
Expand Down
8 changes: 4 additions & 4 deletions src/test-run/commands/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,17 @@ export class NavigateToCommand extends Assignable {
constructor (obj) {
super(obj);

this.type = TYPE.navigateTo;
this.url = null;
this.storages = null;
this.type = TYPE.navigateTo;
this.url = null;
this.stateSnapshot = null;

this._assignFrom(obj, true);
}

_getAssignableProperties () {
return [
{ name: 'url', type: urlArgument, required: true },
{ name: 'storages', type: nullableStringArgument }
{ name: 'stateSnapshot', type: nullableStringArgument }
];
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/test-run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ export default class TestRun extends Session {

const command = this.currentDriverTask.command;

if (command.type === COMMAND_TYPE.navigateTo && command.storages)
this.useStateSnapshot({ storages: JSON.parse(command.storages) });
if (command.type === COMMAND_TYPE.navigateTo && command.stateSnapshot)
this.useStateSnapshot(JSON.parse(command.stateSnapshot));

return command;
}
Expand Down Expand Up @@ -532,7 +532,7 @@ export default class TestRun extends Session {

this.currentRoleId = role.id;

await bookmark.restore(callsite, stateSnapshot ? stateSnapshot.storages : null);
await bookmark.restore(callsite, stateSnapshot);

this.disableDebugBreakpoints = false;
}
Expand Down
5 changes: 5 additions & 0 deletions test/functional/fixtures/regression/gh-2282/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('[Regression](GH-2282)', function () {
it('Cookies should be restored correctly when User Roles with the preserveUrl option are used', function () {
return runTests('testcafe-fixtures/index.js');
});
});
27 changes: 27 additions & 0 deletions test/functional/fixtures/regression/gh-2282/pages/authorized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
window.onload = function () {
if (!document.cookie || document.cookie === 'auth=false')
location.href = './login.html';
else {
var result = document.getElementById('result');

result.innerHTML = 'logged'
}
}

function logout () {
document.cookie = 'auth=false';
location.href = './login.html';
}
</script>
</head>
<body>
<div id="result">no access</div>
<div style="border: 1px solid black; width: 200px;" onclick="logout()">Reset cookie</div>
</body>
</html>
21 changes: 21 additions & 0 deletions test/functional/fixtures/regression/gh-2282/pages/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
function login () {
document.cookie = 'auth=true';
location.href = './authorized.html';
}

window.onload = function () {
if (document.cookie === 'auth=true')
location.href = './authorized.html';
}
</script>
</head>
<body>
<div id="login" style="border: 1px solid black; width: 200px;" onclick="login()">Click me to log in</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Selector, Role } from 'testcafe';

const role = Role('http://localhost:3000/fixtures/regression/gh-2282/pages/login.html', async t => {
await t.click(Selector('#login'));
}, { preserveUrl: true });


fixture `GH-2282 - Cookies should be restored correctly when User Roles with the preserveUrl option are used`
.beforeEach(async t => {
await t.useRole(role);
});

test('Login and save cookies in the role', async t => {
await t.expect(Selector('#result').textContent).contains('logged');
});

test('Restore cookies from the role', async t => {
await t.expect(Selector('#result').textContent).contains('logged');
});


12 changes: 6 additions & 6 deletions test/server/test-run-commands-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,17 +758,17 @@ describe('Test run commands', function () {

it('Should create NavigateToCommand from object', function () {
var commandObj = {
type: TYPE.navigateTo,
url: 'localhost',
storages: 'storages'
type: TYPE.navigateTo,
url: 'localhost',
stateSnapshot: 'stateSnapshot'
};

var command = createCommand(commandObj);

expect(JSON.parse(JSON.stringify(command))).eql({
type: TYPE.navigateTo,
url: 'localhost',
storages: 'storages'
type: TYPE.navigateTo,
url: 'localhost',
stateSnapshot: 'stateSnapshot'
});
});

Expand Down

0 comments on commit f773c21

Please sign in to comment.