Skip to content

Commit ed16cb4

Browse files
authored
Browsers with "userProfile" option in Native Automation (closes #7925) (#7958)
We decided to not support the `userProfile` option in Native Automation mode. Native Automation mode clears all cookies and local storages between test runs. If we use `:userProfile` the Native Automation mode will clear all cookies in current user profile.
1 parent 0bdd78a commit ed16cb4

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

src/errors/runtime/templates.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/errors/types.js

+1
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,5 @@ export const RUNTIME_ERRORS = {
189189
cannotReadConfigFile: 'E1083',
190190
cannotParseConfigFile: 'E1084',
191191
cannotRunLegacyTestsInNativeAutomationMode: 'E1085',
192+
setUserProfileInNativeAutomation: 'E1086',
192193
};

src/runner/bootstrapper.ts

+17
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ export default class Bootstrapper {
186186
});
187187
}
188188

189+
private getBrowsersWithUserProfileEnabled (browserInfos: BrowserInfo[]): BrowserInfo[] {
190+
return browserInfos.filter(browserInfo => (browserInfo.browserOption as any)?.userProfile);
191+
}
192+
189193
private _disableNativeAutomationIfNecessary (remotes: BrowserConnection[], automated: BrowserInfo[]): void {
190194
// NOTE: CDP API allows connecting only for the local browser. So, the 'remote' browser cannot be run in the 'nativeAutomation' mode.
191195
// However, sometimes in tests or TestCafe Studio Recorder, we use the 'remote' browser connection as a local one.
@@ -203,6 +207,8 @@ export default class Bootstrapper {
203207

204208
this._disableNativeAutomationIfNecessary(remotes, automated);
205209

210+
this._validateUserProfileOptionInNativeAutomation(automated);
211+
206212
await this._setupProxy();
207213

208214
let browserConnections = this._createAutomatedConnections(automated);
@@ -218,6 +224,17 @@ export default class Bootstrapper {
218224
return BrowserSet.from(browserConnections, this._getBrowserSetOptions());
219225
}
220226

227+
protected _validateUserProfileOptionInNativeAutomation (automated: BrowserInfo[]): void {
228+
const isNativeAutomation = !this.configuration.getOption(OPTION_NAMES.disableNativeAutomation);
229+
const browsersWithUserProfile = this.getBrowsersWithUserProfileEnabled(automated);
230+
231+
if (isNativeAutomation && browsersWithUserProfile.length) {
232+
const browsers = browsersWithUserProfile.map(b => b.alias).join(', ');
233+
234+
throw new GeneralError(RUNTIME_ERRORS.setUserProfileInNativeAutomation, browsers);
235+
}
236+
}
237+
221238
private async _filterTests (tests: Test[], predicate: FilterFunction): Promise<Test[]> {
222239
return asyncFilter(tests, test => {
223240
const testFixture = test.fixture as Fixture;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>gh-7925</title>
6+
</head>
7+
<body>
8+
</body>
9+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const path = require('path');
2+
const { expect } = require('chai');
3+
const createTestCafe = require('../../../../../lib');
4+
const { onlyInNativeAutomation } = require('../../../utils/skip-in');
5+
6+
const EXPECTED_ERROR = 'The "userProfile" option is enabled for the following browsers: "chrome:userProfile".\n' +
7+
'The "userProfile" option is not supported in the Native Automation mode.\n' +
8+
'Use the "disable native automation" option or remove "userProfile" option to continue.';
9+
10+
let testcafe = null;
11+
let error = null;
12+
13+
function runTests (browsers) {
14+
error = null;
15+
16+
return createTestCafe('127.0.0.1', 1335, 1336)
17+
.then(tc => {
18+
testcafe = tc;
19+
})
20+
.then(() => {
21+
return testcafe.createRunner()
22+
.browsers(browsers)
23+
.src(path.join(__dirname, './testcafe-fixtures/index.js'))
24+
.run();
25+
})
26+
.catch(err => {
27+
error = err;
28+
})
29+
.finally(() => {
30+
return testcafe.close();
31+
});
32+
}
33+
34+
describe('[Regression](GH-7925) Browsers with "userProfile" option in Native Automation', function () {
35+
onlyInNativeAutomation('chrome:userProfile', function () {
36+
return runTests(['chrome:userProfile'])
37+
.then(() => {
38+
expect(error.message).contains(EXPECTED_ERROR);
39+
});
40+
});
41+
42+
onlyInNativeAutomation('chrome:userProfile + firefox', function () {
43+
return runTests(['chrome:userProfile', 'firefox'])
44+
.then(() => {
45+
expect(error).eql(null);
46+
});
47+
});
48+
});
49+
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fixture `GH-7925 - Browsers with "userProfile" option in Native Automation`
2+
.page `http://localhost:3000/fixtures/regression/gh-7925/pages/index.html`;
3+
4+
test('dummy', async () => {
5+
});
6+

test/server/bootstrapper-test.js

+30
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,35 @@ describe('Bootstrapper', () => {
160160
expect(bootstrapper.configuration._mergedOptions).eql({ disableNativeAutomation: true });
161161
bootstrapper.configuration.clear();
162162
});
163+
164+
it('Should throw an error if browser is opened with the "userProfile" option in the Native Automation mode', async function () {
165+
try {
166+
bootstrapper.browsers = [{
167+
alias: 'chrome',
168+
browserOption: { userProfile: true },
169+
provider: {
170+
isLocalBrowser: () => true,
171+
supportNativeAutomation: () => true,
172+
},
173+
}, {
174+
alias: 'edge',
175+
browserOption: { userProfile: true },
176+
provider: {
177+
isLocalBrowser: () => true,
178+
supportNativeAutomation: () => true,
179+
},
180+
}];
181+
182+
await bootstrapper.createRunnableConfiguration();
183+
184+
throw new Error('Promise rejection expected');
185+
}
186+
catch (err) {
187+
expect(err.message).eql('The "userProfile" option is enabled for the following browsers: "chrome, edge".\n' +
188+
'The "userProfile" option is not supported in the Native Automation mode.\n' +
189+
'Use the "disable native automation" option or remove "userProfile" option to continue.'
190+
);
191+
}
192+
});
163193
});
164194
});

0 commit comments

Comments
 (0)