Skip to content

Commit c3ac037

Browse files
authored
test: add test to validate user-agent sanity (#2887)
1 parent b93e099 commit c3ac037

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

test/page.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,42 @@ describe('Page.frame', function() {
12921292
});
12931293
});
12941294

1295+
describe('user-agent sanity', function() {
1296+
it('should be a sane user agent', async ({page}) => {
1297+
const userAgent = await page.evaluate(() => navigator.userAgent);
1298+
const [
1299+
part1,
1300+
part2,
1301+
part3,
1302+
part4,
1303+
part5,
1304+
] = userAgent.split(/[()]/).map(part => part.trim());
1305+
// First part is always "Mozilla/5.0"
1306+
expect(part1).toBe('Mozilla/5.0');
1307+
// Second part in parenthesis is platform - ignore it.
1308+
1309+
// Third part for Firefox is the last one and encodes engine and browser versions.
1310+
if (FFOX) {
1311+
const [engine, browser] = part3.split(' ');
1312+
expect(engine.startsWith('Gecko')).toBe(true);
1313+
expect(browser.startsWith('Firefox')).toBe(true);
1314+
expect(part4).toBe(undefined);
1315+
expect(part5).toBe(undefined);
1316+
return;
1317+
}
1318+
// For both CHROMIUM and WEBKIT, third part is the AppleWebKit version.
1319+
expect(part3.startsWith('AppleWebKit/')).toBe(true);
1320+
expect(part4).toBe('KHTML, like Gecko');
1321+
// 5th part encodes real browser name and engine version.
1322+
const [engine, browser] = part5.split(' ');
1323+
expect(browser.startsWith('Safari/')).toBe(true);
1324+
if (CHROMIUM)
1325+
expect(engine.includes('Chrome/')).toBe(true);
1326+
else
1327+
expect(engine.startsWith('Version/')).toBe(true);
1328+
});
1329+
});
1330+
12951331
describe('Page api coverage', function() {
12961332
it('Page.press should work', async({page, server}) => {
12971333
await page.goto(server.PREFIX + '/input/textarea.html');

0 commit comments

Comments
 (0)