diff --git a/packages/playwright/src/matchers/toHaveURL.ts b/packages/playwright/src/matchers/toHaveURL.ts index ffdb740971..df09833f84 100644 --- a/packages/playwright/src/matchers/toHaveURL.ts +++ b/packages/playwright/src/matchers/toHaveURL.ts @@ -18,7 +18,7 @@ import type { Page } from 'playwright-core'; import type { ExpectMatcherState } from '../../types/test'; import { EXPECTED_COLOR, printReceived } from '../common/expectBundle'; import { matcherHint, type MatcherResult } from './matcherHint'; -import { urlMatches } from 'playwright-core/lib/utils'; +import { constructURLBasedOnBaseURL, urlMatches } from 'playwright-core/lib/utils'; import { colors } from 'playwright-core/lib/utilsBundle'; import { printReceivedStringContainExpectedResult, printReceivedStringContainExpectedSubstring } from './expect'; @@ -51,13 +51,12 @@ export async function toHaveURL( } const timeout = options?.timeout ?? this.timeout; + const baseURL: string | undefined = (page.context() as any)._options.baseURL; let conditionSucceeded = false; let lastCheckedURLString: string | undefined = undefined; try { await page.mainFrame().waitForURL( url => { - const baseURL: string | undefined = (page.context() as any)._options - .baseURL; lastCheckedURLString = url.toString(); if (options?.ignoreCase) { @@ -96,7 +95,9 @@ export async function toHaveURL( this, matcherName, expression, - expected, + typeof expected === 'string' + ? constructURLBasedOnBaseURL(baseURL, expected) + : expected, lastCheckedURLString, this.isNot, true, diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts index dfe6a6cf02..fdf9ee35c4 100644 --- a/tests/playwright-test/expect.spec.ts +++ b/tests/playwright-test/expect.spec.ts @@ -561,11 +561,12 @@ test('should support toHaveURL predicate', async ({ runInlineTest }) => { 'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`, 'a.test.ts': ` import { test, expect } from '@playwright/test'; + import { stripVTControlCharacters } from 'node:util'; test('predicate', async ({ page }) => { await page.goto('data:text/html,