Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: split playwright.fixtures into files (#3982)

Pavel Feldman5b9f489e556d
diff --git a/test/http.fixtures.ts b/test/http.fixtures.ts
new file mode 100644
index 0000000000..c60f6ff096
--- /dev/null
+++ b/test/http.fixtures.ts
@@ -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));
+});
diff --git a/test/playwright.fixtures.ts b/test/playwright.fixtures.ts
index 2802df0b04..1c50679aa4 100644
--- a/test/playwright.fixtures.ts
+++ b/test/playwright.fixtures.ts
@@ -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';
@@ -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;
@@ -59,9 +57,7 @@ type PlaywrightWorkerFixtures = {
type PlaywrightTestFixtures = {
context: BrowserContext;
- server: TestServer;
page: Page;
- httpsServer: TestServer;
browserServer: BrowserServer;
testOutputDir: string;
tmpDir: string;
@@ -69,7 +65,7 @@ type PlaywrightTestFixtures = {
launchPersistent: (options?: Parameters<BrowserType<Browser>['launchPersistentContext']>[1]) => Promise<{context: BrowserContext, page: Page}>;
};
-const fixtures = baseFixtures
+const fixtures = httpFixtures
.declareParameters<PlaywrightParameters>()
.declareWorkerFixtures<PlaywrightWorkerFixtures>()
.declareTestFixtures<PlaywrightTestFixtures>();
@@ -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;
@@ -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';
@@ -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);