Move feature detection into its own method: withFeatureCheck

This commit is contained in:
Faisal Salman
2023-04-09 05:34:02 +07:00
parent 05747dba37
commit 16b416d9ea
5 changed files with 91 additions and 37 deletions

View File

@@ -3,42 +3,63 @@ import { test, expect } from '@playwright/test';
import path from 'path';
import url from 'url';
const localHtml = `file://${path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '../')}/dist/ua-parser.html`;
test('read client hints data', async ({ page }) => {
await page.addInitScript(() => {
Object.defineProperty(navigator, 'userAgentData', {
value : {
brands : [],
platform : '',
mobile : false,
getHighEntropyValues : () => {
return Promise.resolve({
brands : [
{
brand : 'Chromium',
version : '110'
},
{
brand : 'Not(A:Brand',
version : '110'
},
{
brand : 'New Browser',
version : '110'
}
],
platform : 'New OS'
});
}
}
await page.addInitScript(() => {
Object.defineProperty(navigator, 'userAgentData', {
value: {
brands: [],
platform: '',
mobile: false,
getHighEntropyValues: () => {
return Promise.resolve({
brands: [
{
brand: 'Chromium',
version: '110'
},
{
brand: 'Not(A:Brand',
version: '110'
},
{
brand: 'New Browser',
version: '110'
}
],
platform: 'New OS'
});
}
}
});
});
});
const dirname = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '../');
await page.goto(`file://${dirname}/dist/ua-parser.html`);
await page.goto(localHtml);
// @ts-ignore
const uap = await page.evaluate(async () => await UAParser().withClientHints());
expect(uap).toHaveProperty('browser.name', 'New Browser');
expect(uap).toHaveProperty('os.name', 'New OS');
// @ts-ignore
const uap = await page.evaluate(async () => await UAParser().withClientHints());
expect(uap).toHaveProperty('browser.name', 'New Browser');
expect(uap).toHaveProperty('os.name', 'New OS');
});
test('detect Brave', async ({ page }) => {
await page.addInitScript(() => {
Object.defineProperty(navigator, 'brave', {
value: {
isBrave: () => true
}
});
});
await page.goto(localHtml);
// @ts-ignore
let uap = await page.evaluate(() => UAParser());
expect(uap).toHaveProperty('browser.name', 'Chrome Headless');
// @ts-ignore
uap = await page.evaluate(() => UAParser().withFeatureCheck());
expect(uap).toHaveProperty('browser.name', 'Brave');
});