From e40b805782757c932410b431f6fe905d9375af60 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 7 Sep 2021 18:34:02 +0200 Subject: [PATCH] feat(test-runner): support baseURL in toHaveURL (#8743) --- src/test/matchers/matchers.ts | 10 ++++- src/utils/utils.ts | 12 ------ .../playwright.expect.misc.spec.ts | 39 +++++++++++++++++++ 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/test/matchers/matchers.ts b/src/test/matchers/matchers.ts index 852dd76d30..ded545d3e1 100644 --- a/src/test/matchers/matchers.ts +++ b/src/test/matchers/matchers.ts @@ -15,6 +15,9 @@ */ import { Locator, Page } from '../../..'; +import { PlaywrightTestOptions} from '../../../types/test'; +import { constructURLBasedOnBaseURL } from '../../utils/utils'; +import { currentTestInfo } from '../globals'; import type { Expect } from '../types'; import { toBeTruthy } from './toBeTruthy'; import { toEqual } from './toEqual'; @@ -234,9 +237,14 @@ export function toHaveURL( expected: string | RegExp, options?: { timeout?: number }, ) { + const testInfo = currentTestInfo(); + if (!testInfo) + throw new Error(`toHaveURL must be called during the test`); + const baseURL = (testInfo.project.use as PlaywrightTestOptions)?.baseURL; + return toMatchText.call(this, 'toHaveURL', page, 'Page', async () => { return page.url(); - }, expected, options); + }, typeof expected === 'string' ? constructURLBasedOnBaseURL(baseURL, expected) : expected, options); } export function toHaveValue( diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 51495e47c0..d1e1d55764 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -331,18 +331,6 @@ export function canAccessFile(file: string) { } } -const localIpAddresses = [ - 'localhost', - '127.0.0.1', - '::ffff:127.0.0.1', - '::1', - '0000:0000:0000:0000:0000:0000:0000:0001', // WebKit (Windows) -]; - -export function isLocalIpAddress(ipAdress: string): boolean { - return localIpAddresses.includes(ipAdress); -} - export function getUserAgent() { const packageJson = require('./../../package.json'); return `Playwright/${packageJson.version} (${os.arch()}/${os.platform()}/${os.release()})`; diff --git a/tests/playwright-test/playwright.expect.misc.spec.ts b/tests/playwright-test/playwright.expect.misc.spec.ts index 0c3c69a5db..f6d8408c50 100644 --- a/tests/playwright-test/playwright.expect.misc.spec.ts +++ b/tests/playwright-test/playwright.expect.misc.spec.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import http from 'http'; import { test, expect, stripAscii } from './playwright-test-fixtures'; test('should support toHaveCount', async ({ runInlineTest }) => { @@ -159,6 +160,44 @@ test('should support toHaveURL', async ({ runInlineTest }) => { expect(result.exitCode).toBe(1); }); +test('should support toHaveURL with baseURL', async ({ runInlineTest }, testInfo) => { + const port = testInfo.workerIndex + 10500; + const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => { + res.end('hello'); + }); + await new Promise(resolve => server.listen(port, resolve)); + const result = await runInlineTest({ + 'a.test.ts': ` + const { test } = pwt; + + test('pass', async ({ page }) => { + await page.goto('/foobar'); + await expect(page).toHaveURL('/foobar'); + await expect(page).toHaveURL('http://localhost:${port}/foobar'); + }); + + test('fail', async ({ page }) => { + await page.goto('/foobar'); + await expect(page).toHaveURL('/kek', { timeout: 100 }); + }); + `, + 'playwright.config.ts': ` + module.exports = { + use: { + baseURL: 'http://localhost:${port}', + } + }; + `, + }, { workers: 1 }); + const output = stripAscii(result.output); + expect(output).toContain('expect(page).toHaveURL'); + expect(output).toContain(`Expected string: \"http://localhost:${port}/kek\"`); + expect(result.passed).toBe(1); + expect(result.failed).toBe(1); + expect(result.exitCode).toBe(1); + await new Promise(resolve => server.close(resolve)); +}); + test('should support respect expect.timeout', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`,