Skip to content

Commit

Permalink
chore: split playwright.fixtures into files (#3982)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Sep 26, 2020
1 parent 59daaab commit 5b9f489
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 47 deletions.
70 changes: 70 additions & 0 deletions test/http.fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright Microsoft Corporation. All rights reserved.
*
* 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.
*/

import { fixtures as baseFixtures } from '@playwright/test-runner';
import path from 'path';
import { TestServer } from '../utils/testserver';
export { expect } from '@playwright/test';
export { config } from '@playwright/test-runner';

type HttpWorkerFixtures = {
asset: (path: string) => string;
httpService: { server: TestServer, httpsServer: TestServer };
};

type HttpTestFixtures = {
server: TestServer;
httpsServer: TestServer;
};

export const fixtures = baseFixtures
.declareWorkerFixtures<HttpWorkerFixtures>()
.declareTestFixtures<HttpTestFixtures>();
const { defineTestFixture, defineWorkerFixture } = fixtures;

defineWorkerFixture('httpService', async ({ testWorkerIndex }, test) => {
const assetsPath = path.join(__dirname, 'assets');
const cachedPath = path.join(__dirname, 'assets', 'cached');

const port = 8907 + testWorkerIndex * 2;
const server = await TestServer.create(assetsPath, port);
server.enableHTTPCache(cachedPath);

const httpsPort = port + 1;
const httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort);
httpsServer.enableHTTPCache(cachedPath);

await test({server, httpsServer});

await Promise.all([
server.stop(),
httpsServer.stop(),
]);
});

defineTestFixture('server', async ({httpService}, test) => {
httpService.server.reset();
await test(httpService.server);
});

defineTestFixture('httpsServer', async ({httpService}, test) => {
httpService.httpsServer.reset();
await test(httpService.httpsServer);
});

defineWorkerFixture('asset', async ({}, test) => {
await test(p => path.join(__dirname, `assets`, p));
});
56 changes: 9 additions & 47 deletions test/playwright.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
* limitations under the License.
*/

import { expect } from '@playwright/test';
import { config } from '@playwright/test-runner';
import { fixtures as httpFixtures } from './http.fixtures';
import assert from 'assert';
import childProcess from 'child_process';
import fs from 'fs';
import path from 'path';
import os from 'os';
import path from 'path';
import util from 'util';
import childProcess from 'child_process';
import type { LaunchOptions, BrowserType, Browser, BrowserContext, Page, Frame, BrowserServer, BrowserContextOptions } from '../index';
import { TestServer } from '../utils/testserver';
import type { Browser, BrowserContext, BrowserContextOptions, BrowserServer, BrowserType, Frame, LaunchOptions, Page } from '../index';
import { Connection } from '../lib/client/connection';
import { Transport } from '../lib/protocol/transport';
import { installCoverageHooks } from './coverage';
import { fixtures as baseFixtures, config } from '@playwright/test-runner';
import assert from 'assert';
import { expect } from '@playwright/test';
export { expect } from '@playwright/test';
export { config } from '@playwright/test-runner';

Expand All @@ -39,13 +39,11 @@ type PlaywrightParameters = {
};

type PlaywrightWorkerFixtures = {
asset: (path: string) => string;
defaultBrowserOptions: LaunchOptions;
golden: (path: string) => string;
playwright: typeof import('../index');
browserType: BrowserType<Browser>;
browser: Browser;
httpService: {server: TestServer, httpsServer: TestServer}
domain: void;
toImpl: (rpcObject: any) => any;
isChromium: boolean;
Expand All @@ -59,17 +57,15 @@ type PlaywrightWorkerFixtures = {

type PlaywrightTestFixtures = {
context: BrowserContext;
server: TestServer;
page: Page;
httpsServer: TestServer;
browserServer: BrowserServer;
testOutputDir: string;
tmpDir: string;
createUserDataDir: () => Promise<string>;
launchPersistent: (options?: Parameters<BrowserType<Browser>['launchPersistentContext']>[1]) => Promise<{context: BrowserContext, page: Page}>;
};

const fixtures = baseFixtures
const fixtures = httpFixtures
.declareParameters<PlaywrightParameters>()
.declareWorkerFixtures<PlaywrightWorkerFixtures>()
.declareTestFixtures<PlaywrightTestFixtures>();
Expand Down Expand Up @@ -100,26 +96,6 @@ export const options = {
TRACING: valueFromEnv('TRACING', false),
};

defineWorkerFixture('httpService', async ({ testWorkerIndex }, test) => {
const assetsPath = path.join(__dirname, 'assets');
const cachedPath = path.join(__dirname, 'assets', 'cached');

const port = 8907 + testWorkerIndex * 2;
const server = await TestServer.create(assetsPath, port);
server.enableHTTPCache(cachedPath);

const httpsPort = port + 1;
const httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort);
httpsServer.enableHTTPCache(cachedPath);

await test({server, httpsServer});

await Promise.all([
server.stop(),
httpsServer.stop(),
]);
});

const getExecutablePath = browserName => {
if (browserName === 'chromium' && process.env.CRPATH)
return process.env.CRPATH;
Expand Down Expand Up @@ -237,15 +213,11 @@ defineWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, test
await browser.close();
});

defineWorkerFixture('asset', async ({}, test) => {
await test(p => path.join(__dirname, `assets`, p));
});

defineWorkerFixture('golden', async ({browserName}, test) => {
await test(p => path.join(browserName, p));
});

defineWorkerFixture('expectedSSLError', async ({browserName, platform}, runTest) => {
defineWorkerFixture('expectedSSLError', async ({ browserName, platform }, runTest) => {
let expectedSSLError: string;
if (browserName === 'chromium') {
expectedSSLError = 'net::ERR_CERT_AUTHORITY_INVALID';
Expand Down Expand Up @@ -325,16 +297,6 @@ defineTestFixture('launchPersistent', async ({createUserDataDir, defaultBrowserO
await context.close();
});

defineTestFixture('server', async ({httpService}, test) => {
httpService.server.reset();
await test(httpService.server);
});

defineTestFixture('httpsServer', async ({httpService}, test) => {
httpService.httpsServer.reset();
await test(httpService.httpsServer);
});

defineTestFixture('tmpDir', async ({}, test) => {
const tmpDir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright-test-'));
await test(tmpDir);
Expand Down

0 comments on commit 5b9f489

Please sign in to comment.