| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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-05 16:36:36 -08:00
										 |  |  | const util = require('util'); | 
					
						
							|  |  |  | const os = require('os'); | 
					
						
							|  |  |  | const removeFolder = require('rimraf'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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-10 20:31:50 -07:00
										 |  |  | let platform = os.platform(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | const utils = module.exports = { | 
					
						
							| 
									
										
										
										
											2020-07-23 12:42:39 -07:00
										 |  |  |   mkdtempAsync: util.promisify(fs.mkdtemp), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   removeFolderAsync: util.promisify(removeFolder), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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-07 19:15:48 -07:00
										 |  |  |     const handle = await page.evaluateHandle(async ({ frameId, url }) => { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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-07 19:15:48 -07:00
										 |  |  |     }, { frameId, url }); | 
					
						
							|  |  |  |     return handle.asElement().contentFrame(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @param {!Page} page | 
					
						
							|  |  |  |    * @param {string} frameId | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   detachFrame: async function(page, frameId) { | 
					
						
							| 
									
										
										
										
											2020-04-07 19:15:48 -07:00
										 |  |  |     await page.evaluate(frameId => { | 
					
						
							|  |  |  |       document.getElementById(frameId).remove(); | 
					
						
							|  |  |  |     }, frameId); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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 10:16:54 -07: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-13 17:47:15 -07:00
										 |  |  |   registerEngine: async (playwright, name, script, options) => { | 
					
						
							| 
									
										
										
										
											2020-06-26 16:32:42 -07:00
										 |  |  |     try { | 
					
						
							|  |  |  |       await playwright.selectors.register(name, script, options); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       if (!e.message.includes('has been already registered')) | 
					
						
							|  |  |  |         throw e; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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 22:07:47 +05:30
										 |  |  |     // check existence of FLAKINESS_DASHBOARD_PASSWORD and absence of
 | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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-07 22:04:23 -07:00
										 |  |  |       if (test.fullName.includes('**API COVERAGE**')) | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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-29 21:38:30 -07:00
										 |  |  |         name: test.location().toString(), | 
					
						
							|  |  |  |         description: test.fullName(), | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08: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-29 21:38:30 -07:00
										 |  |  |           throw new Error(`Two tests with clashing IDs: ${test.location()} and ${clashingTest.location()}`); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         testIds.set(testId, test); | 
					
						
							|  |  |  |         test.testId = testId; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   makeUserDataDir: async function() { | 
					
						
							| 
									
										
										
										
											2020-07-23 12:42:39 -07:00
										 |  |  |     return await utils.mkdtempAsync(path.join(os.tmpdir(), 'playwright_dev_profile-')); | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-03-19 11:43:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |   removeUserDataDir: async function(dir) { | 
					
						
							| 
									
										
										
										
											2020-07-23 12:42:39 -07:00
										 |  |  |     await utils.removeFolderAsync(dir).catch(e => {}); | 
					
						
							| 
									
										
										
										
											2020-04-01 18:02:43 -07:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 14:17:34 -07:00
										 |  |  |   testOptions(browserType) { | 
					
						
							|  |  |  |     const GOLDEN_DIR = path.join(__dirname, 'golden-' + browserType.name()); | 
					
						
							|  |  |  |     const OUTPUT_DIR = path.join(__dirname, 'output-' + browserType.name()); | 
					
						
							| 
									
										
										
										
											2020-07-01 19:17:27 -07:00
										 |  |  |     const ASSETS_DIR = path.join(__dirname, 'assets'); | 
					
						
							| 
									
										
										
										
											2020-04-08 14:17:34 -07:00
										 |  |  |     return { | 
					
						
							|  |  |  |       FFOX: browserType.name() === 'firefox', | 
					
						
							|  |  |  |       WEBKIT: browserType.name() === 'webkit', | 
					
						
							|  |  |  |       CHROMIUM: browserType.name() === 'chromium', | 
					
						
							| 
									
										
										
										
											2020-04-10 20:31:50 -07:00
										 |  |  |       MAC: platform === 'darwin', | 
					
						
							|  |  |  |       LINUX: platform === 'linux', | 
					
						
							|  |  |  |       WIN: platform === 'win32', | 
					
						
							| 
									
										
										
										
											2020-04-08 14:17:34 -07:00
										 |  |  |       browserType, | 
					
						
							|  |  |  |       GOLDEN_DIR, | 
					
						
							|  |  |  |       OUTPUT_DIR, | 
					
						
							| 
									
										
										
										
											2020-07-01 19:17:27 -07:00
										 |  |  |       ASSETS_DIR, | 
					
						
							| 
									
										
										
										
											2020-07-13 15:00:20 -07:00
										 |  |  |       USES_HOOKS: process.env.PWCHANNEL === 'wire', | 
					
						
							| 
									
										
										
										
											2020-06-30 10:55:11 -07:00
										 |  |  |       CHANNEL: !!process.env.PWCHANNEL, | 
					
						
							| 
									
										
										
										
											2020-07-10 14:26:49 -07:00
										 |  |  |       HEADLESS: !!valueFromEnv('HEADLESS', true), | 
					
						
							| 
									
										
										
										
											2020-04-08 14:17:34 -07:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-04-10 20:31:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   setPlatform(p) { | 
					
						
							|  |  |  |     // To support isplaywrightready.
 | 
					
						
							|  |  |  |     platform = p; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-04-08 14:17:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-09 18:55:57 -07:00
										 |  |  |   createTestLogger(dumpLogOnFailure = true, testRun = null, prefix = '') { | 
					
						
							| 
									
										
										
										
											2020-05-18 19:00:38 -07: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-09 18:55:57 -07:00
										 |  |  |         return name.startsWith('browser') || dumpLogOnFailure; | 
					
						
							| 
									
										
										
										
											2020-05-18 19:00:38 -07:00
										 |  |  |       }, | 
					
						
							|  |  |  |       log: (name, severity, message, args) => { | 
					
						
							|  |  |  |         if (!testRun) | 
					
						
							|  |  |  |           return; | 
					
						
							| 
									
										
										
										
											2020-05-21 15:13:16 -07:00
										 |  |  |         if (name.startsWith('browser')) { | 
					
						
							| 
									
										
										
										
											2020-05-18 19:00:38 -07: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-09 18:55:57 -07:00
										 |  |  |         } else if (dumpLogOnFailure) { | 
					
						
							|  |  |  |           testRun.log(`${prefix}\x1b[32m[${name}]\x1b[0m ${message}`) | 
					
						
							| 
									
										
										
										
											2020-05-18 19:00:38 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       setTestRun(tr) { | 
					
						
							|  |  |  |         if (testRun && testRun.ok()) | 
					
						
							|  |  |  |           testRun.output().splice(0); | 
					
						
							|  |  |  |         testRun = tr; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     return logger; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-07-10 14:26:49 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | function valueFromEnv(name, defaultValue) { | 
					
						
							|  |  |  |   if (!(name in process.env)) | 
					
						
							|  |  |  |     return defaultValue; | 
					
						
							|  |  |  |   return JSON.parse(process.env[name]); | 
					
						
							|  |  |  | } |