Skip to content

Commit

Permalink
test: add missing tests (#965)
Browse files Browse the repository at this point in the history
Chromium tests on Linux now call all our API methods and events at least once!
  • Loading branch information
aslushnikov authored Feb 13, 2020
1 parent 1d84f38 commit bfaf191
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class Page extends platform.EventEmitter {
}

async $wait(selector: string, options?: types.TimeoutOptions & { visibility?: types.Visibility }): Promise<dom.ElementHandle<Element> | null> {
return this.waitForSelector(selector, options);
return this.mainFrame().$wait(selector, options);
}

evaluateHandle: types.EvaluateHandle = async (pageFunction, ...args) => {
Expand Down
16 changes: 8 additions & 8 deletions test/features/permissions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,39 @@ module.exports.describe = function({testRunner, expect, WEBKIT}) {
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;

// Permissions API is not implemented in WebKit (see https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API)
describe('Permissions', function() {
describe.skip(WEBKIT)('Permissions', function() {
function getPermission(page, name) {
return page.evaluate(name => navigator.permissions.query({name}).then(result => result.state), name);
}

it.skip(WEBKIT)('should be prompt by default', async({page, server, context}) => {
it('should be prompt by default', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'geolocation')).toBe('prompt');
});
it.skip(WEBKIT)('should deny permission when not listed', async({page, server, context}) => {
it('should deny permission when not listed', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
await context.setPermissions(server.EMPTY_PAGE, []);
expect(await getPermission(page, 'geolocation')).toBe('denied');
});
it.skip(WEBKIT)('should fail when bad permission is given', async({page, server, context}) => {
it('should fail when bad permission is given', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
let error = {};
await context.setPermissions(server.EMPTY_PAGE, ['foo']).catch(e => error = e);
expect(error.message).toBe('Unknown permission: foo');
});
it.skip(WEBKIT)('should grant permission when listed', async({page, server, context}) => {
it('should grant permission when listed', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
await context.setPermissions(server.EMPTY_PAGE, ['geolocation']);
expect(await getPermission(page, 'geolocation')).toBe('granted');
});
it.skip(WEBKIT)('should reset permissions', async({page, server, context}) => {
it('should reset permissions', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
await context.setPermissions(server.EMPTY_PAGE, ['geolocation']);
expect(await getPermission(page, 'geolocation')).toBe('granted');
await context.clearPermissions();
expect(await getPermission(page, 'geolocation')).toBe('prompt');
});
it.skip(WEBKIT)('should trigger permission onchange', async({page, server, context}) => {
it('should trigger permission onchange', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
await page.evaluate(() => {
window['events'] = [];
Expand All @@ -78,7 +78,7 @@ module.exports.describe = function({testRunner, expect, WEBKIT}) {
await context.clearPermissions();
expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted', 'prompt']);
});
it.skip(WEBKIT)('should isolate permissions between browser contexs', async({page, server, context, newContext}) => {
it('should isolate permissions between browser contexs', async({page, server, context, newContext}) => {
await page.goto(server.EMPTY_PAGE);
const otherContext = await newContext();
const otherPage = await otherContext.newPage();
Expand Down
17 changes: 14 additions & 3 deletions test/launcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
await playwright.launch(options).catch(e => waitError = e);
expect(waitError.message).toContain('Failed to launch');
});
});

describe('Playwright.launchPersistent', function() {
it('should have default URL when launching browser', async function() {
const userDataDir = await makeUserDataDir();
const browserContext = await playwright.launchPersistent(userDataDir, defaultBrowserOptions);
Expand All @@ -68,12 +71,20 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
await browserContext.close();
await removeUserDataDir(userDataDir);
});
});

describe('Playwright.launchServer', function() {
it('should return child_process instance', async () => {
const userDataDir = await makeUserDataDir();
const browserServer = await playwright.launchServer(userDataDir, defaultBrowserOptions);
const browserServer = await playwright.launchServer(defaultBrowserOptions);
expect(browserServer.process().pid).toBeGreaterThan(0);
await browserServer.close();
await removeUserDataDir(userDataDir);
});
it('should fire close event', async () => {
const browserServer = await playwright.launchServer(defaultBrowserOptions);
await Promise.all([
utils.waitEvent(browserServer, 'close'),
browserServer.close(),
]);
});
});

Expand Down
6 changes: 5 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,22 @@ beforeEach(async({server, httpsServer}) => {
const products = ['WebKit', 'Firefox', 'Chromium'];
let product;
let events;
let missingCoverage;
if (process.env.BROWSER === 'firefox') {
product = 'Firefox';
events = {
...require('../lib/events').Events,
...require('../lib/chromium/events').Events,
};
missingCoverage = ['browserContext.setGeolocation', 'elementHandle.scrollIntoViewIfNeeded', 'page.setOfflineMode'];
} else if (process.env.BROWSER === 'webkit') {
product = 'WebKit';
events = require('../lib/events').Events;
missingCoverage = ['browserContext.clearPermissions'];
} else {
product = 'Chromium';
events = require('../lib/events').Events;
missingCoverage = [];
}

describe(product, () => {
Expand All @@ -108,7 +112,7 @@ describe(product, () => {
return;
filteredApi[name] = api[name];
});
utils.recordAPICoverage(testRunner, filteredApi, events);
utils.recordAPICoverage(testRunner, filteredApi, events, missingCoverage);
}
});

Expand Down
10 changes: 8 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,24 @@ const utils = module.exports = {
return promisified;
},

recordAPICoverage: function(testRunner, api, events) {
recordAPICoverage: function(testRunner, api, events, ignoredMethodsArray = []) {
const coverage = new Map();
const ignoredMethods = new Set(ignoredMethodsArray);
for (const [className, classType] of Object.entries(api))
traceAPICoverage(coverage, events, className, classType);
const focus = testRunner.hasFocusedTestsOrSuites();
(focus ? testRunner.fdescribe : testRunner.describe)(COVERAGE_TESTSUITE_NAME, () => {
(focus ? testRunner.fit : testRunner.it)('should call all API methods', () => {
const missingMethods = [];
const extraIgnoredMethods = [];
for (const method of coverage.keys()) {
if (!coverage.get(method))
if (!coverage.get(method) && !ignoredMethods.has(method))
missingMethods.push(method);
else if (coverage.get(method) && ignoredMethods.has(method))
extraIgnoredMethods.push(method);
}
if (extraIgnoredMethods.length)
throw new Error('Certain API Methods are called and should not be ignored: ' + extraIgnoredMethods.join(', '));
if (missingMethods.length)
throw new Error('Certain API Methods are not called: ' + missingMethods.join(', '));
});
Expand Down

0 comments on commit bfaf191

Please sign in to comment.