Skip to content

Commit

Permalink
test: use isChromium, etc fixtures for browser name sniffing (#3508)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Aug 18, 2020
1 parent b2228a6 commit 510182f
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 203 deletions.
15 changes: 15 additions & 0 deletions test/base.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ declare global {
browserType: BrowserType<Browser>;
browserName: string;
browser: Browser;
isChromium: boolean;
isFirefox: boolean;
isWebKit: boolean;
}
interface FixtureState {
toImpl: (rpcObject: any) => any;
Expand Down Expand Up @@ -185,6 +188,18 @@ registerWorkerFixture('browserName', async ({}, test) => {
await test(browserName);
});

registerWorkerFixture('isChromium', async ({browserName}, test) => {
await test(browserName === 'chromium');
});

registerWorkerFixture('isFirefox', async ({browserName}, test) => {
await test(browserName === 'firefox');
});

registerWorkerFixture('isWebKit', async ({browserName}, test) => {
await test(browserName === 'webkit');
});

registerFixture('httpsServer', async ({httpService}, test) => {
httpService.httpsServer.reset();
await test(httpService.httpsServer);
Expand Down
4 changes: 2 additions & 2 deletions test/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ it('should throw upon second create new page', async function({browser}) {
expect(error.message).toContain('Please use browser.newContext()');
});

it('version should work', async function({browser}) {
it('version should work', async function({browser, isChromium}) {
const version = browser.version();
if (CHROMIUM)
if (isChromium)
expect(version.match(/^\d+\.\d+\.\d+\.\d+$/)).toBeTruthy();
else
expect(version.match(/^\d+\.\d+/)).toBeTruthy();
Expand Down
4 changes: 2 additions & 2 deletions test/browsercontext-basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ it('should close all belonging pages once closing context', async function({brow
expect(context.pages().length).toBe(0);
});

it('should disable javascript', async({browser}) => {
it('should disable javascript', async({browser, isWebKit}) => {
{
const context = await browser.newContext({ javaScriptEnabled: false });
const page = await context.newPage();
await page.goto('data:text/html, <script>var something = "forbidden"</script>');
let error = null;
await page.evaluate('something').catch(e => error = e);
if (WEBKIT)
if (isWebKit)
expect(error.message).toContain('Can\'t find variable: something');
else
expect(error.message).toContain('something is not defined');
Expand Down
8 changes: 4 additions & 4 deletions test/browsertype-basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ it('browserType.executablePath should work', async({browserType}) => {
expect(fs.realpathSync(executablePath)).toBe(executablePath);
});

it('browserType.name should work', async({browserType}) => {
if (WEBKIT)
it('browserType.name should work', async({browserType, isWebKit, isFirefox, isChromium}) => {
if (isWebKit)
expect(browserType.name()).toBe('webkit');
else if (FFOX)
else if (isFirefox)
expect(browserType.name()).toBe('firefox');
else if (CHROMIUM)
else if (isChromium)
expect(browserType.name()).toBe('chromium');
else
throw new Error('Unknown browser');
Expand Down
30 changes: 15 additions & 15 deletions test/click.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,39 +344,39 @@ it('should click the button with deviceScaleFactor set', async({browser, server}
await context.close();
});

it('should click the button with px border with offset', async({page, server}) => {
it('should click the button with px border with offset', async({page, server, isWebKit}) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.$eval('button', button => button.style.borderWidth = '8px');
await page.click('button', { position: { x: 20, y: 10 } });
expect(await page.evaluate('result')).toBe('Clicked');
// Safari reports border-relative offsetX/offsetY.
expect(await page.evaluate('offsetX')).toBe(WEBKIT ? 20 + 8 : 20);
expect(await page.evaluate('offsetY')).toBe(WEBKIT ? 10 + 8 : 10);
expect(await page.evaluate('offsetX')).toBe(isWebKit ? 20 + 8 : 20);
expect(await page.evaluate('offsetY')).toBe(isWebKit ? 10 + 8 : 10);
});

it('should click the button with em border with offset', async({page, server}) => {
it('should click the button with em border with offset', async({page, server, isWebKit}) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.$eval('button', button => button.style.borderWidth = '2em');
await page.$eval('button', button => button.style.fontSize = '12px');
await page.click('button', { position: { x: 20, y: 10 } });
expect(await page.evaluate('result')).toBe('Clicked');
// Safari reports border-relative offsetX/offsetY.
expect(await page.evaluate('offsetX')).toBe(WEBKIT ? 12 * 2 + 20 : 20);
expect(await page.evaluate('offsetY')).toBe(WEBKIT ? 12 * 2 + 10 : 10);
expect(await page.evaluate('offsetX')).toBe(isWebKit ? 12 * 2 + 20 : 20);
expect(await page.evaluate('offsetY')).toBe(isWebKit ? 12 * 2 + 10 : 10);
});

it('should click a very large button with offset', async({page, server}) => {
it('should click a very large button with offset', async({page, server, isWebKit}) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.$eval('button', button => button.style.borderWidth = '8px');
await page.$eval('button', button => button.style.height = button.style.width = '2000px');
await page.click('button', { position: { x: 1900, y: 1910 } });
expect(await page.evaluate(() => window['result'])).toBe('Clicked');
// Safari reports border-relative offsetX/offsetY.
expect(await page.evaluate('offsetX')).toBe(WEBKIT ? 1900 + 8 : 1900);
expect(await page.evaluate('offsetY')).toBe(WEBKIT ? 1910 + 8 : 1910);
expect(await page.evaluate('offsetX')).toBe(isWebKit ? 1900 + 8 : 1900);
expect(await page.evaluate('offsetY')).toBe(isWebKit ? 1910 + 8 : 1910);
});

it('should click a button in scrolling container with offset', async({page, server}) => {
it('should click a button in scrolling container with offset', async({page, server, isWebKit}) => {
await page.goto(server.PREFIX + '/input/button.html');
await page.$eval('button', button => {
const container = document.createElement('div');
Expand All @@ -392,11 +392,11 @@ it('should click a button in scrolling container with offset', async({page, serv
await page.click('button', { position: { x: 1900, y: 1910 } });
expect(await page.evaluate(() => window['result'])).toBe('Clicked');
// Safari reports border-relative offsetX/offsetY.
expect(await page.evaluate('offsetX')).toBe(WEBKIT ? 1900 + 8 : 1900);
expect(await page.evaluate('offsetY')).toBe(WEBKIT ? 1910 + 8 : 1910);
expect(await page.evaluate('offsetX')).toBe(isWebKit ? 1900 + 8 : 1900);
expect(await page.evaluate('offsetY')).toBe(isWebKit ? 1910 + 8 : 1910);
});

it.skip(FFOX)('should click the button with offset with page scale', async({browser, server}) => {
it.skip(FFOX)('should click the button with offset with page scale', async({browser, server, isChromium, isWebKit}) => {
const context = await browser.newContext({ viewport: { width: 400, height: 400 }, isMobile: true });
const page = await context.newPage();
await page.goto(server.PREFIX + '/input/button.html');
Expand All @@ -408,10 +408,10 @@ it.skip(FFOX)('should click the button with offset with page scale', async({brow
expect(await page.evaluate('result')).toBe('Clicked');
const round = x => Math.round(x + 0.01);
let expected = { x: 28, y: 18 }; // 20;10 + 8px of border in each direction
if (WEBKIT) {
if (isWebKit) {
// WebKit rounds up during css -> dip -> css conversion.
expected = { x: 29, y: 19 };
} else if (CHROMIUM && HEADLESS) {
} else if (isChromium && HEADLESS) {
// Headless Chromium rounds down during css -> dip -> css conversion.
expected = { x: 27, y: 18 };
}
Expand Down
4 changes: 2 additions & 2 deletions test/defaultbrowsercontext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ it('should support bypassCSP option', async ({server, launchPersistent}) => {
expect(await page.evaluate('__injected')).toBe(42);
});

it('should support javascriptEnabled option', async ({server, launchPersistent}) => {
it('should support javascriptEnabled option', async ({launchPersistent, isWebKit}) => {
const {page, context} = await launchPersistent({javaScriptEnabled: false});
await page.goto('data:text/html, <script>var something = "forbidden"</script>');
let error = null;
await page.evaluate('something').catch(e => error = e);
if (WEBKIT)
if (isWebKit)
expect(error.message).toContain('Can\'t find variable: something');
else
expect(error.message).toContain('something is not defined');
Expand Down
8 changes: 4 additions & 4 deletions test/elementhandle-select-text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@
*/
import './base.fixture';

it('should select textarea', async ({ page, server }) => {
it('should select textarea', async ({ page, server, isFirefox }) => {
await page.goto(server.PREFIX + '/input/textarea.html');
const textarea = await page.$('textarea');
await textarea.evaluate(textarea => textarea.value = 'some value');
await textarea.selectText();
if (FFOX) {
if (isFirefox) {
expect(await textarea.evaluate(el => el.selectionStart)).toBe(0);
expect(await textarea.evaluate(el => el.selectionEnd)).toBe(10);
} else {
expect(await page.evaluate(() => window.getSelection().toString())).toBe('some value');
}
});

it('should select input', async ({ page, server }) => {
it('should select input', async ({ page, server, isFirefox }) => {
await page.goto(server.PREFIX + '/input/textarea.html');
const input = await page.$('input');
await input.evaluate(input => input.value = 'some value');
await input.selectText();
if (FFOX) {
if (isFirefox) {
expect(await input.evaluate(el => el.selectionStart)).toBe(0);
expect(await input.evaluate(el => el.selectionEnd)).toBe(10);
} else {
Expand Down
16 changes: 8 additions & 8 deletions test/frame-evaluate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ it('should have correct execution contexts', async ({ page, server }) => {
expect(await page.frames()[1].evaluate(() => document.body.textContent.trim())).toBe(`Hi, I'm frame`);
});

function expectContexts(pageImpl, count) {
if (CHROMIUM)
function expectContexts(pageImpl, count, isChromium) {
if (isChromium)
expect(pageImpl._delegate._mainFrameSession._contextIdToContext.size).toBe(count);
else
expect(pageImpl._delegate._contextIdToContext.size).toBe(count);
}

it.skip(WIRE)('should dispose context on navigation', async ({ page, server, toImpl }) => {
it.skip(WIRE)('should dispose context on navigation', async ({ page, server, toImpl, isChromium }) => {
await page.goto(server.PREFIX + '/frames/one-frame.html');
expect(page.frames().length).toBe(2);
expectContexts(toImpl(page), 4);
expectContexts(toImpl(page), 4, isChromium);
await page.goto(server.EMPTY_PAGE);
expectContexts(toImpl(page), 2);
expectContexts(toImpl(page), 2, isChromium);
});

it.skip(WIRE)('should dispose context on cross-origin navigation', async ({ page, server, toImpl }) => {
it.skip(WIRE)('should dispose context on cross-origin navigation', async ({ page, server, toImpl, isChromium }) => {
await page.goto(server.PREFIX + '/frames/one-frame.html');
expect(page.frames().length).toBe(2);
expectContexts(toImpl(page), 4);
expectContexts(toImpl(page), 4, isChromium);
await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
expectContexts(toImpl(page), 2);
expectContexts(toImpl(page), 2, isChromium);
});

it('should execute after cross-site navigation', async ({ page, server }) => {
Expand Down
8 changes: 4 additions & 4 deletions test/keyboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,21 @@ it.skip(!MAC)('should support MacOS shortcuts', async ({page, server}) => {
expect(await page.$eval('textarea', textarea => textarea.value)).toBe('some ');
});

it('should press the meta key', async ({page}) => {
it('should press the meta key', async ({page, isFirefox}) => {
const lastEvent = await captureLastKeydown(page);
await page.keyboard.press('Meta');
const {key, code, metaKey} = await lastEvent.jsonValue();
if (FFOX && !MAC)
if (isFirefox && !MAC)
expect(key).toBe('OS');
else
expect(key).toBe('Meta');

if (FFOX)
if (isFirefox)
expect(code).toBe('OSLeft');
else
expect(code).toBe('MetaLeft');

if (FFOX && !MAC)
if (isFirefox && !MAC)
expect(metaKey).toBe(false);
else
expect(metaKey).toBe(true);
Expand Down
8 changes: 4 additions & 4 deletions test/mouse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ it('should trigger hover state with removed window.Node', async({page, server})
expect(await page.evaluate(() => document.querySelector('button:hover').id)).toBe('button-6');
});

it('should set modifier keys on click', async({page, server}) => {
it('should set modifier keys on click', async({page, server, isFirefox}) => {
await page.goto(server.PREFIX + '/input/scrollable.html');
await page.evaluate(() => document.querySelector('#button-3').addEventListener('mousedown', e => window["lastEvent"] = e, true));
const modifiers = {'Shift': 'shiftKey', 'Control': 'ctrlKey', 'Alt': 'altKey', 'Meta': 'metaKey'};
// In Firefox, the Meta modifier only exists on Mac
if (FFOX && !MAC)
if (isFirefox && !MAC)
delete modifiers['Meta'];
for (const modifier in modifiers) {
await page.keyboard.down(modifier);
Expand All @@ -142,9 +142,9 @@ it('should set modifier keys on click', async({page, server}) => {
}
});

it('should tween mouse movement', async({page, server}) => {
it('should tween mouse movement', async({page, isWebKit}) => {
// The test becomes flaky on WebKit without next line.
if (WEBKIT)
if (isWebKit)
await page.evaluate(() => new Promise(requestAnimationFrame));
await page.mouse.move(100, 100);
await page.evaluate(() => {
Expand Down
8 changes: 4 additions & 4 deletions test/network-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ it('should work for fetch requests', async({page, server}) => {
expect(requests[0].frame()).toBe(page.mainFrame());
});

it('should return headers', async({page, server}) => {
it('should return headers', async({page, server, isChromium, isFirefox, isWebKit}) => {
const response = await page.goto(server.EMPTY_PAGE);
if (CHROMIUM)
if (isChromium)
expect(response.request().headers()['user-agent']).toContain('Chrome');
else if (FFOX)
else if (isFirefox)
expect(response.request().headers()['user-agent']).toContain('Firefox');
else if (WEBKIT)
else if (isWebKit)
expect(response.request().headers()['user-agent']).toContain('WebKit');
});

Expand Down
12 changes: 6 additions & 6 deletions test/page-basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ it('should not be visible in context.pages', async({context}) => {
expect(context.pages()).not.toContain(newPage);
});

it('should run beforeunload if asked for', async({context, server}) => {
it('should run beforeunload if asked for', async({context, server, isChromium, isWebKit}) => {
const newPage = await context.newPage();
await newPage.goto(server.PREFIX + '/beforeunload.html');
// We have to interact with a page so that 'beforeunload' handlers
Expand All @@ -43,9 +43,9 @@ it('should run beforeunload if asked for', async({context, server}) => {
const dialog = await newPage.waitForEvent('dialog');
expect(dialog.type()).toBe('beforeunload');
expect(dialog.defaultValue()).toBe('');
if (CHROMIUM)
if (isChromium)
expect(dialog.message()).toBe('');
else if (WEBKIT)
else if (isWebKit)
expect(dialog.message()).toBe('Leave?');
else
expect(dialog.message()).toBe('This page is asking you to confirm that you want to leave - data you have entered may not be saved.');
Expand Down Expand Up @@ -196,7 +196,7 @@ it('page.frame should respect url', async function({page, server}) {
expect(page.frame({ url: /empty/ }).url()).toBe(server.EMPTY_PAGE);
});

it('should have sane user agent', async ({page}) => {
it('should have sane user agent', async ({page, isChromium, isFirefox}) => {
const userAgent = await page.evaluate(() => navigator.userAgent);
const [
part1,
Expand All @@ -210,7 +210,7 @@ it('should have sane user agent', async ({page}) => {
// Second part in parenthesis is platform - ignore it.

// Third part for Firefox is the last one and encodes engine and browser versions.
if (FFOX) {
if (isFirefox) {
const [engine, browser] = part3.split(' ');
expect(engine.startsWith('Gecko')).toBe(true);
expect(browser.startsWith('Firefox')).toBe(true);
Expand All @@ -224,7 +224,7 @@ it('should have sane user agent', async ({page}) => {
// 5th part encodes real browser name and engine version.
const [engine, browser] = part5.split(' ');
expect(browser.startsWith('Safari/')).toBe(true);
if (CHROMIUM)
if (isChromium)
expect(engine.includes('Chrome/')).toBe(true);
else
expect(engine.startsWith('Version/')).toBe(true);
Expand Down
Loading

0 comments on commit 510182f

Please sign in to comment.