From 6a14b1dc51d1b45f599e438513a2848bb7bb1019 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 25 Jan 2024 11:46:47 -0800 Subject: [PATCH] chore: automatically detect the dev server (#29176) --- packages/playwright-core/src/utils/network.ts | 3 ++- packages/playwright-ct-core/src/vitePlugin.ts | 2 +- tests/playwright-test/playwright.ct-build.spec.ts | 7 ++++++- tests/playwright-test/playwright.ct-react.spec.ts | 10 ++++++++-- tests/playwright-test/ui-mode-test-ct.spec.ts | 6 +++++- tests/playwright-test/web-server.spec.ts | 2 +- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/playwright-core/src/utils/network.ts b/packages/playwright-core/src/utils/network.ts index 8cacb8aeb6..bf834abaa0 100644 --- a/packages/playwright-core/src/utils/network.ts +++ b/packages/playwright-core/src/utils/network.ts @@ -181,8 +181,9 @@ export async function isURLAvailable(url: URL, ignoreHTTPSErrors: boolean, onLog async function httpStatusCode(url: URL, ignoreHTTPSErrors: boolean, onLog?: (data: string) => void, onStdErr?: (data: string) => void): Promise { return new Promise(resolve => { - onLog?.(`HTTP GET: ${url}`); + onLog?.(`HTTP HEAD: ${url}`); httpRequest({ + method: 'HEAD', url: url.toString(), headers: { Accept: '*/*' }, rejectUnauthorized: !ignoreHTTPSErrors diff --git a/packages/playwright-ct-core/src/vitePlugin.ts b/packages/playwright-ct-core/src/vitePlugin.ts index a1809676b4..afe3fff9e9 100644 --- a/packages/playwright-ct-core/src/vitePlugin.ts +++ b/packages/playwright-ct-core/src/vitePlugin.ts @@ -56,7 +56,7 @@ export function createPlugin( const endpoint = resolveEndpoint(config); const protocol = endpoint.https ? 'https:' : 'http:'; const url = new URL(`${protocol}//${endpoint.host}:${endpoint.port}`); - if (process.env.PW_CT_DEV && await isURLAvailable(url, true)) { + if (await isURLAvailable(url, true)) { // eslint-disable-next-line no-console console.log(`Test Server is already running at ${url.toString()}, using it.\n`); process.env.PLAYWRIGHT_TEST_BASE_URL = url.toString(); diff --git a/tests/playwright-test/playwright.ct-build.spec.ts b/tests/playwright-test/playwright.ct-build.spec.ts index 6fd4f6d474..6304b44a12 100644 --- a/tests/playwright-test/playwright.ct-build.spec.ts +++ b/tests/playwright-test/playwright.ct-build.spec.ts @@ -21,7 +21,12 @@ test.describe.configure({ mode: 'parallel' }); const playwrightConfig = ` import { defineConfig } from '@playwright/experimental-ct-react'; - export default defineConfig({ projects: [{name: 'foo'}] }); + export default defineConfig({ + use: { + ctPort: ${3200 + (+process.env.TEST_PARALLEL_INDEX)} + }, + projects: [{name: 'foo'}], + }); `; test('should work with the empty component list', async ({ runInlineTest }, testInfo) => { diff --git a/tests/playwright-test/playwright.ct-react.spec.ts b/tests/playwright-test/playwright.ct-react.spec.ts index f565c80ea2..7e27149317 100644 --- a/tests/playwright-test/playwright.ct-react.spec.ts +++ b/tests/playwright-test/playwright.ct-react.spec.ts @@ -18,7 +18,12 @@ import { test, expect } from './playwright-test-fixtures'; const playwrightConfig = ` import { defineConfig } from '@playwright/experimental-ct-react'; - export default defineConfig({ projects: [{name: 'foo'}] }); + export default defineConfig({ + use: { + ctPort: ${3200 + (+process.env.TEST_PARALLEL_INDEX)} + }, + projects: [{name: 'foo'}], + }); `; test('should work with TSX', async ({ runInlineTest }) => { @@ -420,7 +425,8 @@ test('should handle the vite host config', async ({ runInlineTest }) => { test('pass component', async ({ page, mount }) => { const component = await mount(); - await expect(page).toHaveURL('http://127.0.0.1:3100/'); + const host = await page.evaluate(() => window.location.hostname); + await expect(host).toBe('127.0.0.1'); }); `, }, { workers: 1 }); diff --git a/tests/playwright-test/ui-mode-test-ct.spec.ts b/tests/playwright-test/ui-mode-test-ct.spec.ts index 12c3cda430..c4cc2492a9 100644 --- a/tests/playwright-test/ui-mode-test-ct.spec.ts +++ b/tests/playwright-test/ui-mode-test-ct.spec.ts @@ -21,7 +21,11 @@ test.describe.configure({ mode: 'parallel', retries }); const basicTestTree = { 'playwright.config.ts': ` import { defineConfig } from '@playwright/experimental-ct-react'; - export default defineConfig({}); + export default defineConfig({ + use: { + ctPort: ${3200 + (+process.env.TEST_PARALLEL_INDEX)} + } + }); `, 'playwright/index.html': ``, 'playwright/index.ts': ``, diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts index 5fac838dac..0cadbddad8 100644 --- a/tests/playwright-test/web-server.spec.ts +++ b/tests/playwright-test/web-server.spec.ts @@ -661,7 +661,7 @@ test('should check ipv4 and ipv6 with happy eyeballs when URL is passed', async expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); expect(result.output).toContain('Process started'); - expect(result.output).toContain(`HTTP GET: http://localhost:${port}/`); + expect(result.output).toContain(`HTTP HEAD: http://localhost:${port}/`); expect(result.output).toContain('WebServer available'); });