| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-14 14:48:05 -08:00
										 |  |  | import type { JSONReport, JSONReportSuite } from '@playwright/test/src/reporters/json'; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | import * as fs from 'fs'; | 
					
						
							|  |  |  | import * as os from 'os'; | 
					
						
							| 
									
										
										
										
											2021-10-14 14:48:05 -08:00
										 |  |  | import * as path from 'path'; | 
					
						
							| 
									
										
										
										
											2022-02-23 14:17:37 -07:00
										 |  |  | import { PNG } from 'pngjs'; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | import rimraf from 'rimraf'; | 
					
						
							|  |  |  | import { promisify } from 'util'; | 
					
						
							| 
									
										
										
										
											2022-04-06 13:57:14 -08:00
										 |  |  | import type { CommonFixtures } from '../config/commonFixtures'; | 
					
						
							|  |  |  | import { commonFixtures } from '../config/commonFixtures'; | 
					
						
							|  |  |  | import type { ServerFixtures, ServerWorkerOptions } from '../config/serverFixtures'; | 
					
						
							|  |  |  | import { serverFixtures } from '../config/serverFixtures'; | 
					
						
							|  |  |  | import type { TestInfo } from './stable-test-runner'; | 
					
						
							|  |  |  | import { test as base } from './stable-test-runner'; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const removeFolderAsync = promisify(rimraf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type RunResult = { | 
					
						
							|  |  |  |   exitCode: number, | 
					
						
							|  |  |  |   output: string, | 
					
						
							|  |  |  |   passed: number, | 
					
						
							|  |  |  |   failed: number, | 
					
						
							|  |  |  |   flaky: number, | 
					
						
							|  |  |  |   skipped: number, | 
					
						
							| 
									
										
										
										
											2021-07-08 17:16:36 -07:00
										 |  |  |   report: JSONReport, | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   results: any[], | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type TSCResult = { | 
					
						
							|  |  |  |   output: string; | 
					
						
							|  |  |  |   exitCode: number; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Files = { [key: string]: string | Buffer }; | 
					
						
							|  |  |  | type Params = { [key: string]: string | number | boolean | string[] }; | 
					
						
							|  |  |  | type Env = { [key: string]: string | number | boolean | undefined }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function writeFiles(testInfo: TestInfo, files: Files) { | 
					
						
							|  |  |  |   const baseDir = testInfo.outputPath(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const headerJS = `
 | 
					
						
							| 
									
										
										
										
											2021-10-11 10:52:17 -04:00
										 |  |  |     const pwt = require('@playwright/test'); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   `;
 | 
					
						
							|  |  |  |   const headerTS = `
 | 
					
						
							| 
									
										
										
										
											2021-10-11 10:52:17 -04:00
										 |  |  |     import * as pwt from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   `;
 | 
					
						
							| 
									
										
										
										
											2021-11-24 12:42:48 -08:00
										 |  |  |   const headerESM = `
 | 
					
						
							| 
									
										
										
										
											2021-10-11 10:52:17 -04:00
										 |  |  |     import * as pwt from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2021-06-29 15:28:41 -07:00
										 |  |  |   `;
 | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const hasConfig = Object.keys(files).some(name => name.includes('.config.')); | 
					
						
							|  |  |  |   if (!hasConfig) { | 
					
						
							|  |  |  |     files = { | 
					
						
							|  |  |  |       ...files, | 
					
						
							|  |  |  |       'playwright.config.ts': `
 | 
					
						
							|  |  |  |         module.exports = { projects: [ {} ] }; | 
					
						
							|  |  |  |       `,
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-03-23 17:05:49 -06:00
										 |  |  |   if (!Object.keys(files).some(name => name.includes('package.json'))) { | 
					
						
							|  |  |  |     files = { | 
					
						
							|  |  |  |       ...files, | 
					
						
							|  |  |  |       'package.json': `{ "name": "test-project" }`, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   await Promise.all(Object.keys(files).map(async name => { | 
					
						
							|  |  |  |     const fullName = path.join(baseDir, name); | 
					
						
							|  |  |  |     await fs.promises.mkdir(path.dirname(fullName), { recursive: true }); | 
					
						
							| 
									
										
										
										
											2021-06-29 15:28:41 -07:00
										 |  |  |     const isTypeScriptSourceFile = name.endsWith('.ts') && !name.endsWith('.d.ts'); | 
					
						
							| 
									
										
										
										
											2021-11-24 12:42:48 -08:00
										 |  |  |     const isJSModule = name.endsWith('.mjs') || name.includes('esm'); | 
					
						
							|  |  |  |     const header = isTypeScriptSourceFile ? headerTS : (isJSModule ? headerESM : headerJS); | 
					
						
							| 
									
										
										
										
											2021-07-12 11:59:58 -05:00
										 |  |  |     if (typeof files[name] === 'string' && files[name].includes('//@no-header')) { | 
					
						
							|  |  |  |       await fs.promises.writeFile(fullName, files[name]); | 
					
						
							|  |  |  |     } else if (/(spec|test)\.(js|ts|mjs)$/.test(name)) { | 
					
						
							| 
									
										
										
										
											2021-06-06 22:07:07 -07:00
										 |  |  |       const fileHeader = header + 'const { expect } = pwt;\n'; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |       await fs.promises.writeFile(fullName, fileHeader + files[name]); | 
					
						
							|  |  |  |     } else if (/\.(js|ts)$/.test(name) && !name.endsWith('d.ts')) { | 
					
						
							|  |  |  |       await fs.promises.writeFile(fullName, header + files[name]); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       await fs.promises.writeFile(fullName, files[name]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return baseDir; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 21:07:40 +02:00
										 |  |  | const cliEntrypoint = path.join(__dirname, '../../packages/playwright-core/cli.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 16:24:48 -07:00
										 |  |  | async function runPlaywrightTest(childProcess: CommonFixtures['childProcess'], baseDir: string, params: any, env: Env, options: RunOptions): Promise<RunResult> { | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   const paramList = []; | 
					
						
							|  |  |  |   for (const key of Object.keys(params)) { | 
					
						
							|  |  |  |     for (const value of Array.isArray(params[key]) ? params[key] : [params[key]]) { | 
					
						
							|  |  |  |       const k = key.startsWith('-') ? key : '--' + key; | 
					
						
							|  |  |  |       paramList.push(params[key] === true ? `${k}` : `${k}=${value}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   const outputDir = path.join(baseDir, 'test-results'); | 
					
						
							|  |  |  |   const reportFile = path.join(outputDir, 'report.json'); | 
					
						
							| 
									
										
										
										
											2021-10-11 21:07:40 +02:00
										 |  |  |   const args = ['node', cliEntrypoint, 'test']; | 
					
						
							| 
									
										
										
										
											2021-09-20 17:17:12 -07:00
										 |  |  |   if (!options.usesCustomOutputDir) | 
					
						
							| 
									
										
										
										
											2021-06-23 11:08:35 +02:00
										 |  |  |     args.push('--output=' + outputDir); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   args.push( | 
					
						
							|  |  |  |       '--reporter=dot,json', | 
					
						
							|  |  |  |       '--workers=2', | 
					
						
							|  |  |  |       ...paramList | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2021-09-20 17:17:12 -07:00
										 |  |  |   if (options.additionalArgs) | 
					
						
							|  |  |  |     args.push(...options.additionalArgs); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), 'playwright-test-cache-')); | 
					
						
							| 
									
										
										
										
											2021-09-21 16:24:48 -07:00
										 |  |  |   const testProcess = childProcess({ | 
					
						
							| 
									
										
										
										
											2021-09-20 17:17:12 -07:00
										 |  |  |     command: args, | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |     env: { | 
					
						
							|  |  |  |       ...process.env, | 
					
						
							|  |  |  |       PLAYWRIGHT_JSON_OUTPUT_NAME: reportFile, | 
					
						
							|  |  |  |       PWTEST_CACHE_DIR: cacheDir, | 
					
						
							| 
									
										
										
										
											2022-03-22 16:28:04 -07:00
										 |  |  |       // BEGIN: Reserved CI
 | 
					
						
							| 
									
										
										
										
											2022-02-24 12:39:28 -08:00
										 |  |  |       CI: undefined, | 
					
						
							| 
									
										
										
										
											2022-03-22 16:28:04 -07:00
										 |  |  |       BUILD_URL: undefined, | 
					
						
							|  |  |  |       CI_COMMIT_SHA: undefined, | 
					
						
							|  |  |  |       CI_JOB_URL: undefined, | 
					
						
							|  |  |  |       CI_PROJECT_URL: undefined, | 
					
						
							|  |  |  |       GITHUB_REPOSITORY: undefined, | 
					
						
							|  |  |  |       GITHUB_RUN_ID: undefined, | 
					
						
							|  |  |  |       GITHUB_SERVER_URL: undefined, | 
					
						
							|  |  |  |       GITHUB_SHA: undefined, | 
					
						
							|  |  |  |       // END: Reserved CI
 | 
					
						
							| 
									
										
										
										
											2022-02-24 12:39:28 -08:00
										 |  |  |       PW_TEST_HTML_REPORT_OPEN: undefined, | 
					
						
							| 
									
										
										
										
											2021-11-03 15:26:18 -07:00
										 |  |  |       PLAYWRIGHT_DOCKER: undefined, | 
					
						
							|  |  |  |       PW_GRID: undefined, | 
					
						
							| 
									
										
										
										
											2022-02-08 09:22:45 -08:00
										 |  |  |       PW_TEST_REPORTER: undefined, | 
					
						
							| 
									
										
										
										
											2022-02-01 17:15:55 -08:00
										 |  |  |       PW_TEST_REPORTER_WS_ENDPOINT: undefined, | 
					
						
							|  |  |  |       PW_TEST_SOURCE_TRANSFORM: undefined, | 
					
						
							| 
									
										
										
										
											2022-02-09 07:14:11 -08:00
										 |  |  |       PW_TEST_SOURCE_TRANSFORM_SCOPE: undefined, | 
					
						
							| 
									
										
										
										
											2022-02-01 17:15:55 -08:00
										 |  |  |       PW_OUT_OF_PROCESS_DRIVER: undefined, | 
					
						
							|  |  |  |       NODE_OPTIONS: undefined, | 
					
						
							| 
									
										
										
										
											2022-02-24 12:39:28 -08:00
										 |  |  |       ...env, | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-03-23 17:05:49 -06:00
										 |  |  |     cwd: options.cwd ? path.resolve(baseDir, options.cwd) : baseDir, | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-09-21 16:24:48 -07:00
										 |  |  |   let didSendSigint = false; | 
					
						
							|  |  |  |   testProcess.onOutput = () => { | 
					
						
							|  |  |  |     if (options.sendSIGINTAfter && !didSendSigint && countTimes(testProcess.output, '%%SEND-SIGINT%%') >= options.sendSIGINTAfter) { | 
					
						
							|  |  |  |       didSendSigint = true; | 
					
						
							|  |  |  |       process.kill(testProcess.process.pid, 'SIGINT'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   const { exitCode } = await testProcess.exited; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   await removeFolderAsync(cacheDir); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 17:17:12 -07:00
										 |  |  |   const outputString = testProcess.output.toString(); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   const summary = (re: RegExp) => { | 
					
						
							|  |  |  |     let result = 0; | 
					
						
							|  |  |  |     let match = re.exec(outputString); | 
					
						
							|  |  |  |     while (match) { | 
					
						
							|  |  |  |       result += (+match[1]); | 
					
						
							|  |  |  |       match = re.exec(outputString); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   const passed = summary(/(\d+) passed/g); | 
					
						
							|  |  |  |   const failed = summary(/(\d+) failed/g); | 
					
						
							|  |  |  |   const flaky = summary(/(\d+) flaky/g); | 
					
						
							|  |  |  |   const skipped = summary(/(\d+) skipped/g); | 
					
						
							|  |  |  |   let report; | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     report = JSON.parse(fs.readFileSync(reportFile).toString()); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							| 
									
										
										
										
											2021-09-20 17:17:12 -07:00
										 |  |  |     testProcess.output += '\n' + e.toString(); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const results = []; | 
					
						
							| 
									
										
										
										
											2021-07-08 17:16:36 -07:00
										 |  |  |   function visitSuites(suites?: JSONReportSuite[]) { | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |     if (!suites) | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     for (const suite of suites) { | 
					
						
							|  |  |  |       for (const spec of suite.specs) { | 
					
						
							|  |  |  |         for (const test of spec.tests) | 
					
						
							|  |  |  |           results.push(...test.results); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       visitSuites(suite.suites); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (report) | 
					
						
							|  |  |  |     visitSuites(report.suites); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2021-09-20 17:17:12 -07:00
										 |  |  |     exitCode, | 
					
						
							|  |  |  |     output: testProcess.output, | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |     passed, | 
					
						
							|  |  |  |     failed, | 
					
						
							|  |  |  |     flaky, | 
					
						
							|  |  |  |     skipped, | 
					
						
							|  |  |  |     report, | 
					
						
							|  |  |  |     results, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-07 12:04:43 -07:00
										 |  |  | type RunOptions = { | 
					
						
							|  |  |  |   sendSIGINTAfter?: number; | 
					
						
							| 
									
										
										
										
											2021-09-20 17:17:12 -07:00
										 |  |  |   usesCustomOutputDir?: boolean; | 
					
						
							|  |  |  |   additionalArgs?: string[]; | 
					
						
							| 
									
										
										
										
											2022-03-23 17:05:49 -06:00
										 |  |  |   cwd?: string, | 
					
						
							| 
									
										
										
										
											2021-07-07 12:04:43 -07:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | type Fixtures = { | 
					
						
							|  |  |  |   writeFiles: (files: Files) => Promise<string>; | 
					
						
							| 
									
										
										
										
											2022-03-22 16:28:04 -07:00
										 |  |  |   runInlineTest: (files: Files, params?: Params, env?: Env, options?: RunOptions, beforeRunPlaywrightTest?: ({ baseDir }: { baseDir: string }) => Promise<void>) => Promise<RunResult>; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   runTSC: (files: Files) => Promise<TSCResult>; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
  foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: 123,
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: [123, { option: true }],
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
											
										 
											2021-11-18 15:45:52 -08:00
										 |  |  | export const test = base | 
					
						
							| 
									
										
										
										
											2021-12-18 10:12:35 -08:00
										 |  |  |     .extend<CommonFixtures>(commonFixtures) | 
					
						
							| 
									
										
										
										
											2022-01-06 09:29:05 -08:00
										 |  |  |     .extend<ServerFixtures, ServerWorkerOptions>(serverFixtures) | 
					
						
							| 
									
										
											  
											
												feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
  foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: 123,
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: [123, { option: true }],
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
											
										 
											2021-11-18 15:45:52 -08:00
										 |  |  |     .extend<Fixtures>({ | 
					
						
							|  |  |  |       writeFiles: async ({}, use, testInfo) => { | 
					
						
							|  |  |  |         await use(files => writeFiles(testInfo, files)); | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
  foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: 123,
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: [123, { option: true }],
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
											
										 
											2021-11-18 15:45:52 -08:00
										 |  |  |       runInlineTest: async ({ childProcess }, use, testInfo: TestInfo) => { | 
					
						
							| 
									
										
										
										
											2022-03-22 16:28:04 -07:00
										 |  |  |         await use(async (files: Files, params: Params = {}, env: Env = {}, options: RunOptions = {}, beforeRunPlaywrightTest?: ({ baseDir: string }) => Promise<void>) => { | 
					
						
							| 
									
										
											  
											
												feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
  foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: 123,
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: [123, { option: true }],
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
											
										 
											2021-11-18 15:45:52 -08:00
										 |  |  |           const baseDir = await writeFiles(testInfo, files); | 
					
						
							| 
									
										
										
										
											2022-03-22 16:28:04 -07:00
										 |  |  |           if (beforeRunPlaywrightTest) | 
					
						
							|  |  |  |             await beforeRunPlaywrightTest({ baseDir }); | 
					
						
							| 
									
										
											  
											
												feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
  foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: 123,
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: [123, { option: true }],
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
											
										 
											2021-11-18 15:45:52 -08:00
										 |  |  |           return await runPlaywrightTest(childProcess, baseDir, params, env, options); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
  foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: 123,
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
  myOption: [123, { option: true }],
  myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
											
										 
											2021-11-18 15:45:52 -08:00
										 |  |  |       runTSC: async ({ childProcess }, use, testInfo) => { | 
					
						
							|  |  |  |         await use(async files => { | 
					
						
							|  |  |  |           const baseDir = await writeFiles(testInfo, { 'tsconfig.json': JSON.stringify(TSCONFIG), ...files }); | 
					
						
							|  |  |  |           const tsc = childProcess({ | 
					
						
							|  |  |  |             command: ['npx', 'tsc', '-p', baseDir], | 
					
						
							|  |  |  |             cwd: baseDir, | 
					
						
							|  |  |  |             shell: true, | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |           const { exitCode } = await tsc.exited; | 
					
						
							|  |  |  |           return { exitCode, output: tsc.output }; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-09-20 17:17:12 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const TSCONFIG = { | 
					
						
							|  |  |  |   'compilerOptions': { | 
					
						
							|  |  |  |     'target': 'ESNext', | 
					
						
							|  |  |  |     'moduleResolution': 'node', | 
					
						
							|  |  |  |     'module': 'commonjs', | 
					
						
							|  |  |  |     'strict': true, | 
					
						
							|  |  |  |     'esModuleInterop': true, | 
					
						
							|  |  |  |     'allowSyntheticDefaultImports': true, | 
					
						
							|  |  |  |     'rootDir': '.', | 
					
						
							| 
									
										
										
										
											2022-03-16 21:34:41 +01:00
										 |  |  |     'lib': ['esnext', 'dom', 'DOM.Iterable'], | 
					
						
							|  |  |  |     'noEmit': true, | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   }, | 
					
						
							|  |  |  |   'exclude': [ | 
					
						
							|  |  |  |     'node_modules' | 
					
						
							|  |  |  |   ] | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-03 11:22:25 -07:00
										 |  |  | export { expect } from './stable-test-runner'; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const asciiRegex = new RegExp('[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g'); | 
					
						
							| 
									
										
										
										
											2022-01-31 18:14:59 -07:00
										 |  |  | export function stripAnsi(str: string): string { | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  |   return str.replace(asciiRegex, ''); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-07-07 12:04:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-13 10:38:47 -08:00
										 |  |  | export function countTimes(s: string, sub: string): number { | 
					
						
							| 
									
										
										
										
											2021-07-07 12:04:43 -07:00
										 |  |  |   let result = 0; | 
					
						
							|  |  |  |   for (let index = 0; index !== -1;) { | 
					
						
							|  |  |  |     index = s.indexOf(sub, index); | 
					
						
							|  |  |  |     if (index !== -1) { | 
					
						
							|  |  |  |       result++; | 
					
						
							|  |  |  |       index += sub.length; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-23 14:17:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 13:25:59 -07:00
										 |  |  | export function createImage(width: number, height: number, r: number = 0, g: number = 0, b: number = 0, a: number = 255): Buffer { | 
					
						
							| 
									
										
										
										
											2022-02-23 14:17:37 -07:00
										 |  |  |   const image = new PNG({ width, height }); | 
					
						
							|  |  |  |   // Make both images red.
 | 
					
						
							|  |  |  |   for (let i = 0; i < width * height; ++i) { | 
					
						
							|  |  |  |     image.data[i * 4 + 0] = r; | 
					
						
							|  |  |  |     image.data[i * 4 + 1] = g; | 
					
						
							|  |  |  |     image.data[i * 4 + 2] = b; | 
					
						
							| 
									
										
										
										
											2022-02-28 13:25:59 -07:00
										 |  |  |     image.data[i * 4 + 3] = a; | 
					
						
							| 
									
										
										
										
											2022-02-23 14:17:37 -07:00
										 |  |  |   } | 
					
						
							|  |  |  |   return PNG.sync.write(image); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function createWhiteImage(width: number, height: number) { | 
					
						
							|  |  |  |   return createImage(width, height, 255, 255, 255); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function paintBlackPixels(image: Buffer, blackPixelsCount: number): Buffer { | 
					
						
							| 
									
										
										
										
											2022-03-08 20:29:31 -08:00
										 |  |  |   const png = PNG.sync.read(image); | 
					
						
							| 
									
										
										
										
											2022-02-23 14:17:37 -07:00
										 |  |  |   for (let i = 0; i < blackPixelsCount; ++i) { | 
					
						
							|  |  |  |     for (let j = 0; j < 3; ++j) | 
					
						
							| 
									
										
										
										
											2022-03-08 20:29:31 -08:00
										 |  |  |       png.data[i * 4 + j] = 0; | 
					
						
							| 
									
										
										
										
											2022-02-23 14:17:37 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-03-08 20:29:31 -08:00
										 |  |  |   return PNG.sync.write(png); | 
					
						
							| 
									
										
										
										
											2022-02-23 14:17:37 -07:00
										 |  |  | } |