Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

test: add test to validate user-agent sanity (#2887)

Andrey Lushnikovc3ac0371e609
diff --git a/test/page.spec.js b/test/page.spec.js
index 419dd64cc5..5a929d2452 100644
--- a/test/page.spec.js
+++ b/test/page.spec.js
@@ -1292,6 +1292,42 @@ describe('Page.frame', function() {
});
});
+describe('user-agent sanity', function() {
+ it('should be a sane user agent', async ({page}) => {
+ const userAgent = await page.evaluate(() => navigator.userAgent);
+ const [
+ part1,
+ part2,
+ part3,
+ part4,
+ part5,
+ ] = userAgent.split(/[()]/).map(part => part.trim());
+ // First part is always "Mozilla/5.0"
+ expect(part1).toBe('Mozilla/5.0');
+ // Second part in parenthesis is platform - ignore it.
+
+ // Third part for Firefox is the last one and encodes engine and browser versions.
+ if (FFOX) {
+ const [engine, browser] = part3.split(' ');
+ expect(engine.startsWith('Gecko')).toBe(true);
+ expect(browser.startsWith('Firefox')).toBe(true);
+ expect(part4).toBe(undefined);
+ expect(part5).toBe(undefined);
+ return;
+ }
+ // For both CHROMIUM and WEBKIT, third part is the AppleWebKit version.
+ expect(part3.startsWith('AppleWebKit/')).toBe(true);
+ expect(part4).toBe('KHTML, like Gecko');
+ // 5th part encodes real browser name and engine version.
+ const [engine, browser] = part5.split(' ');
+ expect(browser.startsWith('Safari/')).toBe(true);
+ if (CHROMIUM)
+ expect(engine.includes('Chrome/')).toBe(true);
+ else
+ expect(engine.startsWith('Version/')).toBe(true);
+ });
+});
+
describe('Page api coverage', function() {
it('Page.press should work', async({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html');