Skip to content

Commit 6de1ed7

Browse files
authored
fix: fixed incorrect Page URL visited when running tests in parallel in one file with Role (#6782)
* test: added test Should run tests concurrently with Role * fix: added keeping redirect URL in the Role for each test * test: fixed test * test: fixed test * fix: added possibility use redirectUrl like string or object * test: fixed test * test: added condition for running test Should run tests concurrently with Role * refactor: simplified * test: renamed timeLine to testInfo * test: simplified
1 parent 5596a58 commit 6de1ed7

File tree

11 files changed

+96
-32
lines changed

11 files changed

+96
-32
lines changed

src/role/role.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import nanoid from 'nanoid';
66
import TestRun from '../test-run';
77
import TestCafeErrorList from '../errors/error-list';
88

9+
export interface RedirectUrl {
10+
[testId: string]: string;
11+
}
12+
913
export default class Role extends EventEmitter {
1014
public id: string;
1115
public phase: RolePhase;
1216
public loginUrl: string | null;
13-
public redirectUrl: string | null;
17+
public redirectUrl: RedirectUrl | string | null;
1418
public _initFn: Function | null;
1519
public opts: RoleOptions;
1620
public initErr: null | Error | TestCafeErrorList;
@@ -114,7 +118,14 @@ export default class Role extends EventEmitter {
114118
}
115119

116120
public async setCurrentUrlAsRedirectUrl (testRun: TestRun): Promise<void> {
117-
this.redirectUrl = await testRun.getCurrentUrl();
121+
const currentUrl = await testRun.getCurrentUrl();
122+
123+
if (this.opts.preserveUrl)
124+
this.redirectUrl = currentUrl;
125+
else {
126+
this.redirectUrl = this.redirectUrl || {};
127+
(this.redirectUrl as RedirectUrl)[testRun.test.id] = currentUrl;
128+
}
118129

119130
await testRun.compilerService?.updateRoleProperty({
120131
roleId: this.id,

src/test-run/bookmark.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { CurrentIframeNotFoundError, CurrentIframeIsNotLoadedError } from '../errors/test-run';
1414
import TestRun from './index';
1515
import { ExecuteClientFunctionCommand, ExecuteSelectorCommand } from './commands/observation';
16-
import Role from '../role/role';
16+
import Role, { RedirectUrl } from '../role/role';
1717
import { DEFAULT_SPEED_VALUE } from '../configuration/default-values';
1818
import BrowserConsoleMessages from './browser-console-messages';
1919
import { CommandBase } from './commands/base';
@@ -158,7 +158,11 @@ export default class TestRunBookmark {
158158

159159
const preserveUrl = this.role.opts.preserveUrl;
160160

161-
await this._restorePage(this.role.redirectUrl as string, stateSnapshot);
161+
const redirectUrl = preserveUrl
162+
? this.role.redirectUrl as string
163+
: (this.role.redirectUrl as RedirectUrl)[this.testRun.test.id];
164+
165+
await this._restorePage(redirectUrl, stateSnapshot);
162166

163167
if (!preserveUrl)
164168
await this._restoreWorkingFrame();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<h1>First page</h1>
9+
</body>
10+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<h1>Second page</h1>
9+
</body>
10+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const FileStorage = require('../../utils/file-storage');
2+
3+
module.exports = new FileStorage('test-info.json', __dirname);

test/functional/fixtures/concurrency/test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { expect } = require('chai');
33
const isCI = require('is-ci');
44
const config = require('../../config');
55
const { createReporter } = require('../../utils/reporter');
6-
const timeline = require('./timeline');
6+
const testInfo = require('./test-info');
77

88

99
if (config.useLocalBrowsers) {
@@ -82,20 +82,20 @@ if (config.useLocalBrowsers) {
8282
});
8383

8484
afterEach(() => {
85-
timeline.delete();
85+
testInfo.delete();
8686
});
8787

8888
it('Should run tests sequentially if concurrency = 1', function () {
8989
return run('chrome:headless --no-sandbox', 1, './testcafe-fixtures/sequential-test.js')
9090
.then(() => {
91-
expect(timeline.getData()).eql(['long started', 'long finished', 'short started', 'short finished']);
91+
expect(testInfo.getData()).eql(['long started', 'long finished', 'short started', 'short finished']);
9292
});
9393
});
9494

9595
it('Should run tests concurrently if concurrency > 1', function () {
9696
return run('chrome:headless --no-sandbox', 2, './testcafe-fixtures/concurrent-test.js')
9797
.then(() => {
98-
expect(timeline.getData()).eql(['test started', 'test started', 'short finished', 'long finished']);
98+
expect(testInfo.getData()).eql(['test started', 'test started', 'short finished', 'long finished']);
9999
});
100100
});
101101

@@ -104,7 +104,7 @@ if (config.useLocalBrowsers) {
104104
it('Should run tests concurrently in different browser kinds', function () {
105105
return run(['chrome:headless --no-sandbox', 'chrome:headless --no-sandbox --user-agent="TestAgent"'], 2, './testcafe-fixtures/multibrowser-concurrent-test.js')
106106
.then(() => {
107-
const timelineData = timeline.getData();
107+
const timelineData = testInfo.getData();
108108

109109
expect(Object.keys(timelineData).length).gt(1);
110110

@@ -114,6 +114,15 @@ if (config.useLocalBrowsers) {
114114
});
115115
}
116116

117+
if (!config.proxyless) {
118+
it('Should run tests concurrently with Role', function () {
119+
return run('chrome:headless --no-sandbox', 2, './testcafe-fixtures/role-test.js')
120+
.then(() => {
121+
expect(testInfo.getData()).eql(['/fixtures/concurrency/pages/first-page.html', '/fixtures/concurrency/pages/second-page.html']);
122+
});
123+
});
124+
}
125+
117126
it('Should report fixture start correctly if second fixture finishes before first', function () {
118127
return run('chrome:headless --no-sandbox', 2, ['./testcafe-fixtures/multifixture-test-a.js', './testcafe-fixtures/multifixture-test-b.js'], customReporter)
119128
.then(failedCount => {
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import timeline from '../timeline';
1+
import testInfo from '../test-info';
22

33
fixture `Concurrent`
44
.page`../pages/index.html`
55
.after(() => {
6-
timeline.save();
7-
timeline.clear();
6+
testInfo.save();
7+
testInfo.clear();
88
});
99

1010
test('Long test', async t => {
11-
timeline.add('test started');
11+
testInfo.add('test started');
1212

1313
await t.wait(10000);
1414

15-
timeline.add('long finished');
15+
testInfo.add('long finished');
1616
});
1717

1818
test('Short test', async t => {
19-
timeline.add('test started');
19+
testInfo.add('test started');
2020

2121
await t.wait(1000);
2222

23-
timeline.add('short finished');
23+
testInfo.add('short finished');
2424
});

test/functional/fixtures/concurrency/testcafe-fixtures/multibrowser-concurrent-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import timeline from '../timeline';
1+
import testInfo from '../test-info';
22
import { ClientFunction } from 'testcafe';
33

44
const getUserAgent = ClientFunction(() => navigator.userAgent);
@@ -13,9 +13,9 @@ fixture `Concurrent`
1313
data[t.ctx.userAgent] = [];
1414
})
1515
.after(() => {
16-
timeline.setData(data);
17-
timeline.save();
18-
timeline.clear();
16+
testInfo.setData(data);
17+
testInfo.save();
18+
testInfo.clear();
1919
});
2020

2121
test('Long test', async t => {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Role } from 'testcafe';
2+
import testInfo from '../test-info';
3+
4+
const DEMO_ROLE = Role('http://localhost:3000/fixtures/concurrency/pages/index.html', async () => {
5+
});
6+
7+
fixture`F1`
8+
.beforeEach(async t => {
9+
await t.useRole(DEMO_ROLE);
10+
})
11+
.after(() => {
12+
testInfo.save();
13+
testInfo.clear();
14+
});
15+
16+
test('T1', async (t) => {
17+
testInfo.add(await t.eval(() => window.location.pathname));
18+
}).page('http://localhost:3000/fixtures/concurrency/pages/first-page.html');
19+
20+
test('T2', async (t) => {
21+
testInfo.add(await t.eval(() => window.location.pathname));
22+
}).page('http://localhost:3000/fixtures/concurrency/pages/second-page.html');
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import timeline from '../timeline';
1+
import testInfo from '../test-info';
22

33
fixture `Sequential`
44
.page`../pages/index.html`
55
.after(() => {
6-
timeline.save();
7-
timeline.clear();
6+
testInfo.save();
7+
testInfo.clear();
88
});
99

1010
test('Long test', async t => {
11-
timeline.add('long started');
11+
testInfo.add('long started');
1212

1313
await t.wait(10000);
1414

15-
timeline.add('long finished');
15+
testInfo.add('long finished');
1616
});
1717

1818
test('Short test', async t => {
19-
timeline.add('short started');
19+
testInfo.add('short started');
2020

2121
await t.wait(1000);
2222

23-
timeline.add('short finished');
23+
testInfo.add('short finished');
2424
});

0 commit comments

Comments
 (0)