2019-11-19 05:18:28 +03:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
2019-12-11 00:21:51 +03:00
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
2019-11-19 05:18:28 +03:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2020-02-06 03:36:36 +03:00
|
|
|
const util = require('util');
|
|
|
|
const os = require('os');
|
|
|
|
const removeFolder = require('rimraf');
|
|
|
|
|
2019-11-19 05:18:28 +03:00
|
|
|
const {FlakinessDashboard} = require('../utils/flakiness-dashboard');
|
|
|
|
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
|
|
|
|
|
2020-04-11 06:31:50 +03:00
|
|
|
let platform = os.platform();
|
|
|
|
|
2019-11-19 05:18:28 +03:00
|
|
|
const utils = module.exports = {
|
2020-07-23 22:42:39 +03:00
|
|
|
mkdtempAsync: util.promisify(fs.mkdtemp),
|
|
|
|
|
|
|
|
removeFolderAsync: util.promisify(removeFolder),
|
|
|
|
|
2019-11-19 05:18:28 +03:00
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
projectRoot: function() {
|
|
|
|
return PROJECT_ROOT;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Page} page
|
|
|
|
* @param {string} frameId
|
|
|
|
* @param {string} url
|
|
|
|
* @return {!Playwright.Frame}
|
|
|
|
*/
|
|
|
|
attachFrame: async function(page, frameId, url) {
|
2020-04-08 05:15:48 +03:00
|
|
|
const handle = await page.evaluateHandle(async ({ frameId, url }) => {
|
2019-11-19 05:18:28 +03:00
|
|
|
const frame = document.createElement('iframe');
|
|
|
|
frame.src = url;
|
|
|
|
frame.id = frameId;
|
|
|
|
document.body.appendChild(frame);
|
|
|
|
await new Promise(x => frame.onload = x);
|
|
|
|
return frame;
|
2020-04-08 05:15:48 +03:00
|
|
|
}, { frameId, url });
|
|
|
|
return handle.asElement().contentFrame();
|
2019-11-19 05:18:28 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Page} page
|
|
|
|
* @param {string} frameId
|
|
|
|
*/
|
|
|
|
detachFrame: async function(page, frameId) {
|
2020-04-08 05:15:48 +03:00
|
|
|
await page.evaluate(frameId => {
|
|
|
|
document.getElementById(frameId).remove();
|
|
|
|
}, frameId);
|
2019-11-19 05:18:28 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Frame} frame
|
|
|
|
* @param {string=} indentation
|
|
|
|
* @return {Array<string>}
|
|
|
|
*/
|
|
|
|
dumpFrames: function(frame, indentation) {
|
|
|
|
indentation = indentation || '';
|
|
|
|
let description = frame.url().replace(/:\d{4}\//, ':<PORT>/');
|
|
|
|
if (frame.name())
|
|
|
|
description += ' (' + frame.name() + ')';
|
|
|
|
const result = [indentation + description];
|
|
|
|
const childFrames = frame.childFrames();
|
|
|
|
childFrames.sort((a, b) => {
|
|
|
|
if (a.url() !== b.url())
|
|
|
|
return a.url() < b.url() ? -1 : 1;
|
|
|
|
return a.name() < b.name() ? -1 : 1;
|
|
|
|
});
|
|
|
|
for (const child of childFrames)
|
|
|
|
result.push(...utils.dumpFrames(child, ' ' + indentation));
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2020-06-24 20:16:54 +03:00
|
|
|
verifyViewport: async (page, width, height) => {
|
|
|
|
expect(page.viewportSize().width).toBe(width);
|
|
|
|
expect(page.viewportSize().height).toBe(height);
|
|
|
|
expect(await page.evaluate('window.innerWidth')).toBe(width);
|
|
|
|
expect(await page.evaluate('window.innerHeight')).toBe(height);
|
|
|
|
},
|
|
|
|
|
2020-07-14 03:47:15 +03:00
|
|
|
registerEngine: async (playwright, name, script, options) => {
|
2020-06-27 02:32:42 +03:00
|
|
|
try {
|
|
|
|
await playwright.selectors.register(name, script, options);
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.message.includes('has been already registered'))
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-19 05:18:28 +03:00
|
|
|
initializeFlakinessDashboardIfNeeded: async function(testRunner) {
|
|
|
|
// Generate testIDs for all tests and verify they don't clash.
|
|
|
|
// This will add |test.testId| for every test.
|
|
|
|
//
|
|
|
|
// NOTE: we do this on CI's so that problems arise on PR trybots.
|
|
|
|
if (process.env.CI)
|
|
|
|
generateTestIDs(testRunner);
|
|
|
|
// FLAKINESS_DASHBOARD_PASSWORD is an encrypted/secured variable.
|
|
|
|
// Encrypted variables get a special treatment in CI's when handling PRs so that
|
|
|
|
// secrets are not leaked to untrusted code.
|
|
|
|
// - AppVeyor DOES NOT decrypt secured variables for PRs
|
|
|
|
// - Travis DOES NOT decrypt encrypted variables for PRs
|
|
|
|
// - Cirrus CI DOES NOT decrypt encrypted variables for PRs *unless* PR is sent
|
|
|
|
// from someone who has WRITE ACCESS to the repo.
|
|
|
|
//
|
|
|
|
// Since we don't want to run flakiness dashboard for PRs on all CIs, we
|
2020-03-07 19:37:47 +03:00
|
|
|
// check existence of FLAKINESS_DASHBOARD_PASSWORD and absence of
|
2019-11-19 05:18:28 +03:00
|
|
|
// CIRRUS_BASE_SHA env variables.
|
|
|
|
if (!process.env.FLAKINESS_DASHBOARD_PASSWORD || process.env.CIRRUS_BASE_SHA)
|
|
|
|
return;
|
|
|
|
const {sha, timestamp} = await FlakinessDashboard.getCommitDetails(__dirname, 'HEAD');
|
|
|
|
const dashboard = new FlakinessDashboard({
|
|
|
|
commit: {
|
|
|
|
sha,
|
|
|
|
timestamp,
|
|
|
|
url: `https://github.com/Microsoft/playwright/commit/${sha}`,
|
|
|
|
},
|
|
|
|
build: {
|
|
|
|
url: process.env.FLAKINESS_DASHBOARD_BUILD_URL,
|
|
|
|
},
|
|
|
|
dashboardRepo: {
|
|
|
|
url: 'https://github.com/aslushnikov/playwright-flakiness-dashboard.git',
|
|
|
|
username: 'playwright-flakiness',
|
|
|
|
email: 'aslushnikov+playwrightflakiness@gmail.com',
|
|
|
|
password: process.env.FLAKINESS_DASHBOARD_PASSWORD,
|
|
|
|
branch: process.env.FLAKINESS_DASHBOARD_NAME,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
testRunner.on('testfinished', test => {
|
|
|
|
// Do not report tests from COVERAGE testsuite.
|
|
|
|
// They don't bring much value to us.
|
2020-04-08 08:04:23 +03:00
|
|
|
if (test.fullName.includes('**API COVERAGE**'))
|
2019-11-19 05:18:28 +03:00
|
|
|
return;
|
|
|
|
const testpath = test.location.filePath.substring(utils.projectRoot().length);
|
|
|
|
const url = `https://github.com/Microsoft/playwright/blob/${sha}/${testpath}#L${test.location.lineNumber}`;
|
|
|
|
dashboard.reportTestResult({
|
|
|
|
testId: test.testId,
|
2020-03-30 07:38:30 +03:00
|
|
|
name: test.location().toString(),
|
|
|
|
description: test.fullName(),
|
2019-11-19 05:18:28 +03:00
|
|
|
url,
|
|
|
|
result: test.result,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
testRunner.on('finished', async({result}) => {
|
|
|
|
dashboard.setBuildResult(result);
|
|
|
|
await dashboard.uploadAndCleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
function generateTestIDs(testRunner) {
|
|
|
|
const testIds = new Map();
|
|
|
|
for (const test of testRunner.tests()) {
|
|
|
|
const testIdComponents = [test.name];
|
|
|
|
for (let suite = test.suite; !!suite.parentSuite; suite = suite.parentSuite)
|
|
|
|
testIdComponents.push(suite.name);
|
|
|
|
testIdComponents.reverse();
|
|
|
|
const testId = testIdComponents.join('>');
|
|
|
|
const clashingTest = testIds.get(testId);
|
|
|
|
if (clashingTest)
|
2020-03-30 07:38:30 +03:00
|
|
|
throw new Error(`Two tests with clashing IDs: ${test.location()} and ${clashingTest.location()}`);
|
2019-11-19 05:18:28 +03:00
|
|
|
testIds.set(testId, test);
|
|
|
|
test.testId = testId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-02-06 03:36:36 +03:00
|
|
|
|
|
|
|
makeUserDataDir: async function() {
|
2020-07-23 22:42:39 +03:00
|
|
|
return await utils.mkdtempAsync(path.join(os.tmpdir(), 'playwright_dev_profile-'));
|
2020-02-06 03:36:36 +03:00
|
|
|
},
|
2020-03-19 21:43:35 +03:00
|
|
|
|
2020-02-06 03:36:36 +03:00
|
|
|
removeUserDataDir: async function(dir) {
|
2020-07-23 22:42:39 +03:00
|
|
|
await utils.removeFolderAsync(dir).catch(e => {});
|
2020-04-02 04:02:43 +03:00
|
|
|
},
|
|
|
|
|
2020-04-11 06:31:50 +03:00
|
|
|
setPlatform(p) {
|
|
|
|
// To support isplaywrightready.
|
|
|
|
platform = p;
|
|
|
|
},
|
2020-04-09 00:17:34 +03:00
|
|
|
|
2020-06-10 04:55:57 +03:00
|
|
|
createTestLogger(dumpLogOnFailure = true, testRun = null, prefix = '') {
|
2020-05-19 05:00:38 +03:00
|
|
|
const colors = [31, 32, 33, 34, 35, 36, 37];
|
|
|
|
let colorIndex = 0;
|
|
|
|
for (let i = 0; i < prefix.length; i++)
|
|
|
|
colorIndex += prefix.charCodeAt(i);
|
|
|
|
const color = colors[colorIndex % colors.length];
|
|
|
|
prefix = prefix ? `\x1b[${color}m[${prefix}]\x1b[0m ` : '';
|
|
|
|
|
|
|
|
const logger = {
|
|
|
|
isEnabled: (name, severity) => {
|
2020-06-10 04:55:57 +03:00
|
|
|
return name.startsWith('browser') || dumpLogOnFailure;
|
2020-05-19 05:00:38 +03:00
|
|
|
},
|
|
|
|
log: (name, severity, message, args) => {
|
|
|
|
if (!testRun)
|
|
|
|
return;
|
2020-05-22 01:13:16 +03:00
|
|
|
if (name.startsWith('browser')) {
|
2020-05-19 05:00:38 +03:00
|
|
|
if (severity === 'warning')
|
|
|
|
testRun.log(`${prefix}\x1b[31m[browser]\x1b[0m ${message}`)
|
|
|
|
else
|
|
|
|
testRun.log(`${prefix}\x1b[33m[browser]\x1b[0m ${message}`)
|
2020-06-10 04:55:57 +03:00
|
|
|
} else if (dumpLogOnFailure) {
|
|
|
|
testRun.log(`${prefix}\x1b[32m[${name}]\x1b[0m ${message}`)
|
2020-05-19 05:00:38 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setTestRun(tr) {
|
|
|
|
if (testRun && testRun.ok())
|
|
|
|
testRun.output().splice(0);
|
|
|
|
testRun = tr;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return logger;
|
|
|
|
},
|
2020-08-05 02:32:10 +03:00
|
|
|
|
2020-08-17 20:33:42 +03:00
|
|
|
expectSSLError(browserName, errorMessage) {
|
2020-08-05 02:32:10 +03:00
|
|
|
if (browserName === 'chromium') {
|
|
|
|
expect(errorMessage).toContain('net::ERR_CERT_AUTHORITY_INVALID');
|
|
|
|
} else if (browserName === 'webkit') {
|
|
|
|
if (platform === 'darwin')
|
|
|
|
expect(errorMessage).toContain('The certificate for this server is invalid');
|
|
|
|
else if (platform === 'win32')
|
|
|
|
expect(errorMessage).toContain('SSL peer certificate or SSH remote key was not OK');
|
|
|
|
else
|
|
|
|
expect(errorMessage).toContain('Unacceptable TLS certificate');
|
|
|
|
} else {
|
|
|
|
expect(errorMessage).toContain('SSL_ERROR_UNKNOWN');
|
|
|
|
}
|
|
|
|
},
|
2020-05-19 05:00:38 +03:00
|
|
|
};
|