Skip to content

Commit

Permalink
test: structure tests to use environments, closer to end user (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Apr 8, 2020
1 parent be06bb0 commit 2ef8e26
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 153 deletions.
271 changes: 139 additions & 132 deletions test/playwright.spec.js
Expand Up @@ -14,107 +14,141 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const fs = require('fs');
const path = require('path');
const rm = require('rimraf').sync;
const readline = require('readline');
const {TestServer} = require('../utils/testserver/');
const {Environment} = require('../utils/testrunner/Test');

const serverEnvironment = new Environment('TestServer');
serverEnvironment.beforeAll(async state => {
const assetsPath = path.join(__dirname, 'assets');
const cachedPath = path.join(__dirname, 'assets', 'cached');

const port = 8907 + state.parallelIndex * 3;
state.server = await TestServer.create(assetsPath, port);
state.server.enableHTTPCache(cachedPath);
state.server.PORT = port;
state.server.PREFIX = `http://localhost:${port}`;
state.server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`;
state.server.EMPTY_PAGE = `http://localhost:${port}/empty.html`;

const httpsPort = port + 1;
state.httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort);
state.httpsServer.enableHTTPCache(cachedPath);
state.httpsServer.PORT = httpsPort;
state.httpsServer.PREFIX = `https://localhost:${httpsPort}`;
state.httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`;
state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;

const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m';
const sourcePort = port + 2;
state.sourceServer = await TestServer.create(path.join(__dirname, '..'), sourcePort);
state.sourceServer.PORT = sourcePort;
state.sourceServer.PREFIX = `http://localhost:${sourcePort}`;
});
serverEnvironment.afterAll(async({server, sourceServer, httpsServer}) => {
await Promise.all([
server.stop(),
httpsServer.stop(),
sourceServer.stop(),
]);
});
serverEnvironment.beforeEach(async({server, httpsServer}) => {
server.reset();
httpsServer.reset();
});

const goldenEnvironment = new Environment('Golden');
goldenEnvironment.beforeAll(async ({browserType}) => {
const { OUTPUT_DIR, GOLDEN_DIR } = require('./utils').testOptions(browserType);
if (fs.existsSync(OUTPUT_DIR))
rm(OUTPUT_DIR);
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
expect.setupGolden(GOLDEN_DIR, OUTPUT_DIR);
});

/**
* @type {TestSuite}
*/
module.exports.addPlaywrightTests = ({platform, products, playwrightPath, headless, slowMo, dumpProtocolOnFailure, coverage}) => {
const MAC = platform === 'darwin';
const LINUX = platform === 'linux';
const WIN = platform === 'win32';
module.exports.addPlaywrightTests = ({testRunner, products}) => {
const dumpProtocolOnFailure = valueFromEnv('DEBUGP', false);
const playwrightPath = require('./utils').projectRoot();
const playwright = require(playwrightPath);

beforeAll(async state => {
const assetsPath = path.join(__dirname, 'assets');
const cachedPath = path.join(__dirname, 'assets', 'cached');

const port = 8907 + state.parallelIndex * 3;
state.server = await TestServer.create(assetsPath, port);
state.server.enableHTTPCache(cachedPath);
state.server.PORT = port;
state.server.PREFIX = `http://localhost:${port}`;
state.server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`;
state.server.EMPTY_PAGE = `http://localhost:${port}/empty.html`;

const httpsPort = port + 1;
state.httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort);
state.httpsServer.enableHTTPCache(cachedPath);
state.httpsServer.PORT = httpsPort;
state.httpsServer.PREFIX = `https://localhost:${httpsPort}`;
state.httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`;
state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;

const sourcePort = port + 2;
state.sourceServer = await TestServer.create(path.join(__dirname, '..'), sourcePort);
state.sourceServer.PORT = sourcePort;
state.sourceServer.PREFIX = `http://localhost:${sourcePort}`;
const playwrightEnvironment = new Environment('Playwright');
playwrightEnvironment.beforeAll(async state => {
state.playwright = playwright;
});

afterAll(async({server, sourceServer, httpsServer}) => {
await Promise.all([
server.stop(),
httpsServer.stop(),
sourceServer.stop(),
]);
playwrightEnvironment.afterAll(async state => {
delete state.playwright;
});

beforeEach(async({server, httpsServer}) => {
server.reset();
httpsServer.reset();
});
for (const product of products) {
const browserTypeEnvironment = new Environment('BrowserType');
browserTypeEnvironment.beforeAll(async state => {
state.browserType = state.playwright[product.toLowerCase()];
});
browserTypeEnvironment.afterAll(async state => {
delete state.browserType;
});

for (const productInfo of products) {
const product = productInfo.product;
const browserType = playwright[product.toLowerCase()];
const CHROMIUM = product === 'Chromium';
const FFOX = product === 'Firefox';
const WEBKIT = product === 'WebKit';
const defaultBrowserOptions = {
handleSIGINT: false,
executablePath: productInfo.executablePath,
slowMo,
headless,
};

if (defaultBrowserOptions.executablePath) {
console.warn(`${YELLOW_COLOR}WARN: running ${product} tests with ${defaultBrowserOptions.executablePath}${RESET_COLOR}`);
} else {
// Make sure the `npm install` was run after the chromium roll.
if (!fs.existsSync(browserType.executablePath()))
throw new Error(`Browser is not downloaded. Run 'npm install' and try to re-run tests`);
}
const browserEnvironment = new Environment(product);
browserEnvironment.beforeAll(async state => {
const { defaultBrowserOptions } = require('./utils').testOptions(state.browserType);
state.browser = await state.browserType.launch(defaultBrowserOptions);
state.browserServer = state.browser._ownedServer;
state._stdout = readline.createInterface({ input: state.browserServer.process().stdout });
state._stderr = readline.createInterface({ input: state.browserServer.process().stderr });
});
browserEnvironment.afterAll(async state => {
await state.browserServer.close();
state.browser = null;
state.browserServer = null;
state._stdout.close();
state._stderr.close();
});
browserEnvironment.beforeEach(async(state, testRun) => {
const dumpout = data => testRun.log(`\x1b[33m[pw:stdio:out]\x1b[0m ${data}`);
const dumperr = data => testRun.log(`\x1b[31m[pw:stdio:err]\x1b[0m ${data}`);
state._stdout.on('line', dumpout);
state._stderr.on('line', dumperr);
if (dumpProtocolOnFailure)
state.browser._debugProtocol.log = data => testRun.log(`\x1b[32m[pw:protocol]\x1b[0m ${data}`);
state.tearDown = async () => {
state._stdout.off('line', dumpout);
state._stderr.off('line', dumperr);
if (dumpProtocolOnFailure)
delete state.browser._debugProtocol.log;
};
});
browserEnvironment.afterEach(async (state, test) => {
if (state.browser.contexts().length !== 0) {
if (test.result === 'ok')
console.warn(`\nWARNING: test "${test.fullName()}" (${test.location()}) did not close all created contexts!\n`);
await Promise.all(state.browser.contexts().map(context => context.close()));
}
await state.tearDown();
});

const GOLDEN_DIR = path.join(__dirname, 'golden-' + product.toLowerCase());
const OUTPUT_DIR = path.join(__dirname, 'output-' + product.toLowerCase());
if (fs.existsSync(OUTPUT_DIR))
rm(OUTPUT_DIR);
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
expect.setupGolden(GOLDEN_DIR, OUTPUT_DIR);

const testOptions = {
FFOX,
WEBKIT,
CHROMIUM,
MAC,
LINUX,
WIN,
browserType,
playwright,
defaultBrowserOptions,
playwrightPath,
headless: !!defaultBrowserOptions.headless,
OUTPUT_DIR,
};
const pageEnvironment = new Environment('Page');
pageEnvironment.beforeEach(async state => {
state.context = await state.browser.newContext();
state.page = await state.context.newPage();
});
pageEnvironment.afterEach(async state => {
await state.context.close();
state.context = null;
state.page = null;
});

function loadTests(modulePath) {
const testOptions = {
...require('./utils').testOptions(global.browserType),
playwright: global.playwright,
browserType: global.browserType,
};
const module = require(modulePath);
if (typeof module.describe === 'function')
describe('', module.describe, testOptions);
Expand All @@ -124,58 +158,22 @@ module.exports.addPlaywrightTests = ({platform, products, playwrightPath, headle
xdescribe('', module.xdescribe, testOptions);
}

describe(product, () => {
describe('', function() {
beforeAll(async state => {
state.browser = await browserType.launch(defaultBrowserOptions);
state.browserServer = state.browser._ownedServer;
state._stdout = readline.createInterface({ input: state.browserServer.process().stdout });
state._stderr = readline.createInterface({ input: state.browserServer.process().stderr });
});
testRunner.collector().useEnvironment(serverEnvironment); // Custom global environment.
testRunner.collector().useEnvironment(playwrightEnvironment);

afterAll(async state => {
await state.browserServer.close();
state.browser = null;
state.browserServer = null;
state._stdout.close();
state._stderr.close();
});
describe(product, () => {
// In addition to state, expose these two on global so that describes can access them.
global.playwright = playwright;
global.browserType = playwright[product.toLowerCase()];

beforeEach(async(state, testRun) => {
const dumpout = data => testRun.log(`\x1b[33m[pw:stdio:out]\x1b[0m ${data}`);
const dumperr = data => testRun.log(`\x1b[31m[pw:stdio:err]\x1b[0m ${data}`);
state._stdout.on('line', dumpout);
state._stderr.on('line', dumperr);
if (dumpProtocolOnFailure)
state.browser._debugProtocol.log = data => testRun.log(`\x1b[32m[pw:protocol]\x1b[0m ${data}`);
state.tearDown = async () => {
state._stdout.off('line', dumpout);
state._stderr.off('line', dumperr);
if (dumpProtocolOnFailure)
delete state.browser._debugProtocol.log;
};
});
testRunner.collector().useEnvironment(browserTypeEnvironment);
testRunner.collector().useEnvironment(goldenEnvironment); // Custom environment.

afterEach(async (state, test) => {
if (state.browser.contexts().length !== 0) {
if (test.result === 'ok')
console.warn(`\nWARNING: test "${test.fullName()}" (${test.location()}) did not close all created contexts!\n`);
await Promise.all(state.browser.contexts().map(context => context.close()));
}
await state.tearDown();
});
describe('', function() {
testRunner.collector().useEnvironment(browserEnvironment);

describe('', function() {
beforeEach(async state => {
state.context = await state.browser.newContext();
state.page = await state.context.newPage();
});

afterEach(async state => {
await state.context.close();
state.context = null;
state.page = null;
});
testRunner.collector().useEnvironment(pageEnvironment);

// Page-level tests that are given a browser, a context and a page.
// Each test is launched in a new browser context.
Expand Down Expand Up @@ -210,7 +208,7 @@ module.exports.addPlaywrightTests = ({platform, products, playwrightPath, headle
loadTests('./permissions.spec.js');
});

describe.skip(!CHROMIUM)('[Chromium]', () => {
describe.skip(product !== 'Chromium')('[Chromium]', () => {
loadTests('./chromium/chromium.spec.js');
loadTests('./chromium/coverage.spec.js');
loadTests('./chromium/pdf.spec.js');
Expand All @@ -236,14 +234,23 @@ module.exports.addPlaywrightTests = ({platform, products, playwrightPath, headle
loadTests('./multiclient.spec.js');
});

describe.skip(!CHROMIUM)('[Chromium]', () => {
describe.skip(product !== 'Chromium')('[Chromium]', () => {
loadTests('./chromium/launcher.spec.js');
loadTests('./chromium/oopif.spec.js');
loadTests('./chromium/tracing.spec.js');
});

if (coverage)
if (process.env.COVERAGE)
loadTests('./apicoverage.spec.js');

delete global.browserType;
delete global.playwright;
});
}
};

function valueFromEnv(name, defaultValue) {
if (!(name in process.env))
return defaultValue;
return JSON.parse(process.env[name]);
}
23 changes: 2 additions & 21 deletions test/test.js
Expand Up @@ -50,34 +50,15 @@ utils.setupTestRunner(testRunner);

console.log('Testing on Node', process.version);

const names = ['Chromium', 'Firefox', 'WebKit'].filter(name => {
const products = ['Chromium', 'Firefox', 'WebKit'].filter(name => {
return process.env.BROWSER === name.toLowerCase() || process.env.BROWSER === 'all';
});
const products = names.map(name => {
const executablePath = {
'Chromium': process.env.CRPATH,
'Firefox': process.env.FFPATH,
'WebKit': process.env.WKPATH,
}[name];
return { product: name, executablePath };
});

function valueFromEnv(name, defaultValue) {
if (!(name in process.env))
return defaultValue;
return JSON.parse(process.env[name]);
}

for (const [key, value] of Object.entries(testRunner.api()))
global[key] = value;
require('./playwright.spec.js').addPlaywrightTests({
playwrightPath: utils.projectRoot(),
testRunner,
products,
platform: os.platform(),
headless: !!valueFromEnv('HEADLESS', true),
slowMo: valueFromEnv('SLOW_MO', 0),
dumpProtocolOnFailure: valueFromEnv('DEBUGP', false),
coverage: process.env.COVERAGE,
});
for (const [key, value] of Object.entries(testRunner.api())) {
// expect is used when running tests, while the rest of api is not.
Expand Down

0 comments on commit 2ef8e26

Please sign in to comment.