|
| 1 | +// @ts-check |
| 2 | +const process = require('process'); |
| 3 | +const _ = require('lodash'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +let userConfig = {}; |
| 7 | + |
| 8 | +// Prefer ts over js to match default behaviour of playwright-test |
| 9 | +const configFiles = process.env.PLAYWRIGHT_CFG_FILE ? |
| 10 | + [process.env.PLAYWRIGHT_CFG_FILE] : |
| 11 | + ['./playwright.config.ts', './playwright.config.js']; |
| 12 | + |
| 13 | +for (const file of configFiles) { |
| 14 | + if (fs.existsSync(file)) { |
| 15 | + try { |
| 16 | + userConfig = require(file); |
| 17 | + // it should put config just under root level to get it work with playwright.config.ts |
| 18 | + // there is no such issue with playwright.config.js |
| 19 | + if (userConfig.default) { |
| 20 | + userConfig = userConfig.default; |
| 21 | + } |
| 22 | + break; |
| 23 | + } catch (e) { |
| 24 | + console.error(e); |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const overrides = { |
| 30 | + use: { |
| 31 | + headless: process.env.HEADLESS === 'true', |
| 32 | + video: 'off', |
| 33 | + launchOptions: {}, |
| 34 | + }, |
| 35 | + reporter: [ |
| 36 | + ['list'], |
| 37 | + // outputFile is set by playwright-runner.js as an env variable. The runner needs to process it |
| 38 | + // so better for it to set the output path |
| 39 | + ['junit'], |
| 40 | + // outputFile is set by playwright-runner.js as an env variable. The runner needs to process it |
| 41 | + // so better for it to set the output path |
| 42 | + ['@saucelabs/playwright-reporter', |
| 43 | + { |
| 44 | + upload: false, |
| 45 | + }, |
| 46 | + ], |
| 47 | + ], |
| 48 | + testIgnore: process.env.TEST_IGNORE?.split(','), |
| 49 | +}; |
| 50 | + |
| 51 | +// Values that are arrays are merged at the very end (see arrMerger()), but primitives are not. |
| 52 | +// Allow the user to set a single reporter like so: `reporter: 'list'`. |
| 53 | +if (userConfig.reporter && !(userConfig.reporter instanceof Array)) { |
| 54 | + overrides.reporter.push([userConfig.reporter]); |
| 55 | +} |
| 56 | + |
| 57 | +if (process.env.BROWSER_NAME !== 'chrome') { |
| 58 | + // chromium, firefox and webkit come pre-packaged with playwright. |
| 59 | + // So we can just pass those browser values to playwright and |
| 60 | + // it knows what to do and where to pick them up. |
| 61 | + overrides.use.browserName = process.env.BROWSER_NAME; // override browserName with suite browserName |
| 62 | +} else { |
| 63 | + // Google chrome is provided by the sauce VM. So we have to let playwright know where to look. |
| 64 | + overrides.use.channel = 'chrome'; |
| 65 | + overrides.use.launchOptions.executablePath = process.env.BROWSER_PATH; |
| 66 | +} |
| 67 | + |
| 68 | +if ('HTTP_PROXY' in process.env && process.env.HTTP_PROXY !== '') { |
| 69 | + const proxy = { |
| 70 | + server: process.env.HTTP_PROXY, |
| 71 | + }; |
| 72 | + |
| 73 | + overrides.use.contextOptions = {proxy, ignoreHTTPSErrors: true}; |
| 74 | + // Need to set the browser launch option as well, it is a hard requirement when testing chromium + windows. |
| 75 | + overrides.use.launchOptions = {proxy, ignoreHTTPSErrors: true}; |
| 76 | +} |
| 77 | + |
| 78 | +function arrMerger(objValue, srcValue) { |
| 79 | + if (_.isArray(objValue)) { |
| 80 | + return objValue.concat(srcValue); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +module.exports = _.mergeWith(userConfig, overrides, arrMerger); |
0 commit comments