Skip to content

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

chore: unify expect message calculation (#37337)

Dmitry Gozmana717592f6d75
diff --git a/packages/playwright/bundles/expect/src/expectBundleImpl.ts b/packages/playwright/bundles/expect/src/expectBundleImpl.ts
index 0ffe8acbd8..f9c27bff5f 100644
--- a/packages/playwright/bundles/expect/src/expectBundleImpl.ts
+++ b/packages/playwright/bundles/expect/src/expectBundleImpl.ts
@@ -45,5 +45,6 @@ export {
EXPECTED_COLOR,
INVERTED_COLOR,
RECEIVED_COLOR,
+ DIM_COLOR,
printReceived,
} from 'jest-matcher-utils';
diff --git a/packages/playwright/src/common/expectBundle.ts b/packages/playwright/src/common/expectBundle.ts
index 4b601b77ae..281922916c 100644
--- a/packages/playwright/src/common/expectBundle.ts
+++ b/packages/playwright/src/common/expectBundle.ts
@@ -21,4 +21,5 @@ export const matcherUtils = require('./expectBundleImpl').matcherUtils;
export const EXPECTED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').EXPECTED_COLOR = require('./expectBundleImpl').EXPECTED_COLOR;
export const INVERTED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').INVERTED_COLOR = require('./expectBundleImpl').INVERTED_COLOR;
export const RECEIVED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').RECEIVED_COLOR = require('./expectBundleImpl').RECEIVED_COLOR;
+export const DIM_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').DIM_COLOR = require('./expectBundleImpl').DIM_COLOR;
export const printReceived: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').printReceived = require('./expectBundleImpl').printReceived;
diff --git a/packages/playwright/src/matchers/matcherHint.ts b/packages/playwright/src/matchers/matcherHint.ts
index 65328ed68d..6fc0e10eb6 100644
--- a/packages/playwright/src/matchers/matcherHint.ts
+++ b/packages/playwright/src/matchers/matcherHint.ts
@@ -15,22 +15,62 @@
*/
import { stringifyStackFrames } from 'playwright-core/lib/utils';
+import { DIM_COLOR, RECEIVED_COLOR, EXPECTED_COLOR } from '../common/expectBundle';
import type { ExpectMatcherState } from '../../types/test';
import type { StackFrame } from '@protocol/channels';
import type { Locator } from 'playwright-core';
-export function matcherHint(state: ExpectMatcherState, locator: Locator | undefined, matcherName: string, expression: any, actual: any, matcherOptions: any, timeout: number | undefined, expectedReceivedString?: string, preventExtraStatIndent: boolean = false) {
- let header = state.utils.matcherHint(matcherName, expression, actual, matcherOptions).replace(/ \/\/ deep equality/, '') + ' failed\n\n';
- // Extra space added after locator and timeout to match Jest's received/expected output
- const extraSpace = preventExtraStatIndent ? '' : ' ';
- if (locator)
- header += `Locator: ${extraSpace}${String(locator)}\n`;
- if (expectedReceivedString)
- header += `${expectedReceivedString}\n`;
- if (timeout)
- header += `Timeout: ${extraSpace}${timeout}ms\n`;
- return header;
+type MatcherMessageDetails = {
+ receiver?: string; // Assuming 'locator' when locator is provided, 'page' otherwise.
+ matcherName: string;
+ expectation: string;
+ locator?: Locator;
+ printedExpected?: string;
+ printedReceived?: string;
+ printedDiff?: string;
+ timedOut?: boolean;
+ timeout?: number;
+ errorMessage?: string;
+ log?: string[];
+};
+
+export function formatMatcherMessage(state: ExpectMatcherState, details: MatcherMessageDetails) {
+ const receiver = details.receiver ?? (details.locator ? 'locator' : 'page');
+ let message = DIM_COLOR('expect(') + RECEIVED_COLOR(receiver)
+ + DIM_COLOR(')' + (state.promise ? '.' + state.promise : '') + (state.isNot ? '.not' : '') + '.')
+ + details.matcherName
+ + DIM_COLOR('(') + EXPECTED_COLOR(details.expectation) + DIM_COLOR(')')
+ + ' failed\n\n';
+
+ // Sometimes diff is actually expected + received. Turn it into two lines to
+ // simplify alignment logic.
+ const diffLines = details.printedDiff?.split('\n');
+ if (diffLines?.length === 2) {
+ details.printedExpected = diffLines[0];
+ details.printedReceived = diffLines[1];
+ details.printedDiff = undefined;
+ }
+
+ const align = !details.errorMessage && details.printedExpected?.startsWith('Expected:')
+ && (!details.printedReceived || details.printedReceived.startsWith('Received:'));
+ if (details.locator)
+ message += `Locator: ${align ? ' ' : ''}${String(details.locator)}\n`;
+ if (details.printedExpected)
+ message += details.printedExpected + '\n';
+ if (details.printedReceived)
+ message += details.printedReceived + '\n';
+ if (details.timedOut && details.timeout)
+ message += `Timeout: ${align ? ' ' : ''}${details.timeout}ms\n`;
+ if (details.printedDiff)
+ message += details.printedDiff + '\n';
+ if (details.errorMessage) {
+ message += details.errorMessage;
+ if (!details.errorMessage.endsWith('\n'))
+ message += '\n';
+ }
+ message += callLogText(details.log);
+ return message;
}
export type MatcherResult<E, A> = {
@@ -71,3 +111,12 @@ export class ExpectError extends Error {
export function isJestError(e: unknown): e is JestError {
return e instanceof Error && 'matcherResult' in e;
}
+
+export const callLogText = (log: string[] | undefined) => {
+ if (!log || !log.some(l => !!l))
+ return '';
+ return `
+Call log:
+${DIM_COLOR(log.join('\n'))}
+`;
+};
diff --git a/packages/playwright/src/matchers/matchers.ts b/packages/playwright/src/matchers/matchers.ts
index 78db00e9c5..8fb03c1130 100644
--- a/packages/playwright/src/matchers/matchers.ts
+++ b/packages/playwright/src/matchers/matchers.ts
@@ -17,7 +17,7 @@
import { constructURLBasedOnBaseURL, isRegExp, isString, isTextualMimeType, pollAgainstDeadline, serializeExpectedTextValues } from 'playwright-core/lib/utils';
import { colors } from 'playwright-core/lib/utils';
-import { callLogText, expectTypes } from '../util';
+import { expectTypes } from '../util';
import { toBeTruthy } from './toBeTruthy';
import { toEqual } from './toEqual';
import { toHaveURLWithPredicate } from './toHaveURL';
@@ -25,6 +25,7 @@ import { toMatchText } from './toMatchText';
import { takeFirst } from '../common/config';
import { currentTestInfo } from '../common/globals';
import { TestInfoImpl } from '../worker/testInfo';
+import { formatMatcherMessage } from './matcherHint';
import type { ExpectMatcherState } from '../../types/test';
import type { TestStepInfoImpl } from '../worker/testInfo';
@@ -187,7 +188,7 @@ export function toContainText(
return toMatchText.call(this, 'toContainText', locator, 'Locator', async (isNot, timeout) => {
const expectedText = serializeExpectedTextValues([expected], { matchSubstring: true, normalizeWhiteSpace: true, ignoreCase: options.ignoreCase });
return await locator._expect('to.have.text', { expectedText, isNot, useInnerText: options.useInnerText, timeout });
- }, expected, options);
+ }, expected, { ...options, matchSubstring: true });
}
}
@@ -262,7 +263,7 @@ export function toHaveClass(
return toEqual.call(this, 'toHaveClass', locator, 'Locator', async (isNot, timeout) => {
const expectedText = serializeExpectedTextValues(expected);
return await locator._expect('to.have.class.array', { expectedText, isNot, timeout });
- }, expected, options, true);
+ }, expected, options);
} else {
return toMatchText.call(this, 'toHaveClass', locator, 'Locator', async (isNot, timeout) => {
const expectedText = serializeExpectedTextValues([expected]);
@@ -283,7 +284,7 @@ export function toContainClass(
return toEqual.call(this, 'toContainClass', locator, 'Locator', async (isNot, timeout) => {
const expectedText = serializeExpectedTextValues(expected);
return await locator._expect('to.contain.class.array', { expectedText, isNot, timeout });
- }, expected, options, true);
+ }, expected, options);
} else {
if (isRegExp(expected))
throw new Error(`"expected" argument in toContainClass cannot be a RegExp value`);
@@ -408,7 +409,7 @@ export function toHaveTitle(
return toMatchText.call(this, 'toHaveTitle', page, 'Page', async (isNot, timeout) => {
const expectedText = serializeExpectedTextValues([expected], { normalizeWhiteSpace: true });
return await (page.mainFrame() as FrameEx)._expect('to.have.title', { expectedText, isNot, timeout });
- }, expected, { receiverLabel: 'page', ...options });
+ }, expected, options);
}
export function toHaveURL(
@@ -426,7 +427,7 @@ export function toHaveURL(
return toMatchText.call(this, 'toHaveURL', page, 'Page', async (isNot, timeout) => {
const expectedText = serializeExpectedTextValues([expected], { ignoreCase: options?.ignoreCase });
return await (page.mainFrame() as FrameEx)._expect('to.have.url', { expectedText, isNot, timeout });
- }, expected, { receiverLabel: 'page', ...options });
+ }, expected, options);
}
export async function toBeOK(
@@ -443,9 +444,12 @@ export async function toBeOK(
isTextEncoding ? response.text() : null
]) : [];
- const message = () => this.utils.matcherHint(matcherName, undefined, '', { isNot: this.isNot }) +
- callLogText(log) +
- (text === null ? '' : `\nResponse text:\n${colors.dim(text?.substring(0, 1000) || '')}`);
+ const message = () => formatMatcherMessage(this, {
+ matcherName,
+ receiver: 'response',
+ expectation: '',
+ log,
+ }) + (text === null ? '' : `\nResponse text:\n${colors.dim(text?.substring(0, 1000) || '')}`);
const pass = response.ok();
return { message, pass };
diff --git a/packages/playwright/src/matchers/toBeTruthy.ts b/packages/playwright/src/matchers/toBeTruthy.ts
index 94da12644c..89940e7a3e 100644
--- a/packages/playwright/src/matchers/toBeTruthy.ts
+++ b/packages/playwright/src/matchers/toBeTruthy.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import { callLogText, expectTypes } from '../util';
-import { matcherHint } from './matcherHint';
+import { expectTypes } from '../util';
+import { formatMatcherMessage } from './matcherHint';
import { runBrowserBackendOnError } from '../mcp/test/browserBackend';
import type { MatcherResult } from './matcherHint';
@@ -25,19 +25,14 @@ import type { Locator } from 'playwright-core';
export async function toBeTruthy(
this: ExpectMatcherState,
matcherName: string,
- receiver: Locator,
+ locator: Locator,
receiverType: string,
expected: string,
arg: string,
query: (isNot: boolean, timeout: number) => Promise<{ matches: boolean, log?: string[], received?: any, timedOut?: boolean, errorMessage?: string }>,
options: { timeout?: number } = {},
): Promise<MatcherResult<any, any>> {
- expectTypes(receiver, [receiverType], matcherName);
-
- const matcherOptions = {
- isNot: this.isNot,
- promise: this.promise,
- };
+ expectTypes(locator, [receiverType], matcherName);
const timeout = options.timeout ?? this.timeout;
@@ -56,18 +51,26 @@ export async function toBeTruthy(
let printedExpected: string | undefined;
if (pass) {
printedExpected = `Expected: not ${expected}`;
- printedReceived = errorMessage ?? `Received: ${expected}`;
+ printedReceived = errorMessage ? '' : `Received: ${expected}`;
} else {
printedExpected = `Expected: ${expected}`;
- printedReceived = errorMessage ?? `Received: ${received}`;
+ printedReceived = errorMessage ? '' : `Received: ${received}`;
}
const message = () => {
- const header = matcherHint(this, receiver, matcherName, 'locator', arg, matcherOptions, timedOut ? timeout : undefined, `${printedExpected}\n${printedReceived}`);
- const logText = callLogText(log);
- return `${header}${logText}`;
+ return formatMatcherMessage(this, {
+ matcherName,
+ expectation: arg,
+ locator,
+ timeout,
+ timedOut,
+ printedExpected,
+ printedReceived,
+ errorMessage,
+ log,
+ });
};
- await runBrowserBackendOnError(receiver.page(), message);
+ await runBrowserBackendOnError(locator.page(), message);
return {
message,
diff --git a/packages/playwright/src/matchers/toEqual.ts b/packages/playwright/src/matchers/toEqual.ts
index 967778db5d..082eaa3b4d 100644
--- a/packages/playwright/src/matchers/toEqual.ts
+++ b/packages/playwright/src/matchers/toEqual.ts
@@ -16,8 +16,8 @@
import { isRegExp } from 'playwright-core/lib/utils';
-import { callLogText, expectTypes } from '../util';
-import { matcherHint } from './matcherHint';
+import { expectTypes } from '../util';
+import { formatMatcherMessage } from './matcherHint';
import { runBrowserBackendOnError } from '../mcp/test/browserBackend';
import type { MatcherResult } from './matcherHint';
@@ -31,20 +31,13 @@ const RECEIVED_LABEL = 'Received';
export async function toEqual<T>(
this: ExpectMatcherState,
matcherName: string,
- receiver: Locator,
+ locator: Locator,
receiverType: string,
query: (isNot: boolean, timeout: number) => Promise<{ matches: boolean, received?: any, log?: string[], timedOut?: boolean, errorMessage?: string }>,
expected: T,
options: { timeout?: number, contains?: boolean } = {},
- messagePreventExtraStatIndent?: boolean
): Promise<MatcherResult<any, any>> {
- expectTypes(receiver, [receiverType], matcherName);
-
- const matcherOptions = {
- comment: options.contains ? '' : 'deep equality',
- isNot: this.isNot,
- promise: this.promise,
- };
+ expectTypes(locator, [receiverType], matcherName);
const timeout = options.timeout ?? this.timeout;
@@ -64,10 +57,9 @@ export async function toEqual<T>(
let printedDiff: string | undefined;
if (pass) {
printedExpected = `Expected: not ${this.utils.printExpected(expected)}`;
- printedReceived = errorMessage ?? `Received: ${this.utils.printReceived(received)}`;
+ printedReceived = errorMessage ? '' : `Received: ${this.utils.printReceived(received)}`;
} else if (errorMessage) {
printedExpected = `Expected: ${this.utils.printExpected(expected)}`;
- printedReceived = errorMessage;
} else if (Array.isArray(expected) && Array.isArray(received)) {
const normalizedExpected = expected.map((exp, index) => {
const rec = received[index];
@@ -93,12 +85,21 @@ export async function toEqual<T>(
);
}
const message = () => {
- const details = printedDiff || `${printedExpected}\n${printedReceived}`;
- const header = matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined, details, messagePreventExtraStatIndent);
- return `${header}${callLogText(log)}`;
+ return formatMatcherMessage(this, {
+ matcherName,
+ expectation: 'expected',
+ locator,
+ timeout,
+ timedOut,
+ printedExpected,
+ printedReceived,
+ printedDiff,
+ errorMessage,
+ log,
+ });
};
- await runBrowserBackendOnError(receiver.page(), message);
+ await runBrowserBackendOnError(locator.page(), message);
// Passing the actual and expected objects so that a custom reporter
// could access them, for example in order to display a custom visual diff,
diff --git a/packages/playwright/src/matchers/toHaveURL.ts b/packages/playwright/src/matchers/toHaveURL.ts
index 4169e83d1a..f3c4ded541 100644
--- a/packages/playwright/src/matchers/toHaveURL.ts
+++ b/packages/playwright/src/matchers/toHaveURL.ts
@@ -15,11 +15,10 @@
*/
import { urlMatches } from 'playwright-core/lib/utils';
-import { colors } from 'playwright-core/lib/utils';
import { printReceivedStringContainExpectedResult } from './expect';
-import { matcherHint } from './matcherHint';
-import { EXPECTED_COLOR, printReceived } from '../common/expectBundle';
+import { formatMatcherMessage } from './matcherHint';
+import { printReceived } from '../common/expectBundle';
import type { MatcherResult } from './matcherHint';
import type { ExpectMatcherState } from '../../types/test';
@@ -32,23 +31,6 @@ export async function toHaveURLWithPredicate(
options?: { ignoreCase?: boolean; timeout?: number },
): Promise<MatcherResult<string | RegExp, string>> {
const matcherName = 'toHaveURL';
- const expression = 'page';
- const matcherOptions = {
- isNot: this.isNot,
- promise: this.promise,
- };
-
- if (typeof expected !== 'function') {
- throw new Error(
- [
- // Always display `expected` in expectation place
- matcherHint(this, undefined, matcherName, expression, undefined, matcherOptions, undefined, undefined, true),
- `${colors.bold('Matcher error')}: ${EXPECTED_COLOR('expected')} value must be a string, regular expression, or predicate`,
- this.utils.printWithType('Expected', expected, this.utils.printExpected),
- ].join('\n\n'),
- );
- }
-
const timeout = options?.timeout ?? this.timeout;
const baseURL: string | undefined = (page.context() as any)._options.baseURL;
let conditionSucceeded = false;
@@ -91,7 +73,6 @@ export async function toHaveURLWithPredicate(
toHaveURLMessage(
this,
matcherName,
- expression,
expected,
lastCheckedURLString,
this.isNot,
@@ -106,26 +87,20 @@ export async function toHaveURLWithPredicate(
function toHaveURLMessage(
state: ExpectMatcherState,
matcherName: string,
- expression: string,
expected: Function,
received: string | undefined,
pass: boolean,
- didTimeout: boolean,
+ timedOut: boolean,
timeout: number,
): string {
- const matcherOptions = {
- isNot: state.isNot,
- promise: state.promise,
- };
const receivedString = received || '';
- const messagePrefix = matcherHint(state, undefined, matcherName, expression, undefined, matcherOptions, didTimeout ? timeout : undefined, undefined, true);
let printedReceived: string | undefined;
let printedExpected: string | undefined;
let printedDiff: string | undefined;
if (typeof expected === 'function') {
- printedExpected = `Expected predicate to ${!state.isNot ? 'succeed' : 'fail'}`;
- printedReceived = `Received string: ${printReceived(receivedString)}`;
+ printedExpected = `Expected: predicate to ${!state.isNot ? 'succeed' : 'fail'}`;
+ printedReceived = `Received: ${printReceived(receivedString)}`;
} else {
if (pass) {
printedExpected = `Expected pattern: not ${state.utils.printExpected(expected)}`;
@@ -137,6 +112,13 @@ function toHaveURLMessage(
}
}
- const resultDetails = printedDiff ? printedDiff : printedExpected + '\n' + printedReceived;
- return messagePrefix + resultDetails;
+ return formatMatcherMessage(state, {
+ matcherName,
+ expectation: 'expected',
+ timeout,
+ timedOut,
+ printedExpected,
+ printedReceived,
+ printedDiff,
+ });
}
diff --git a/packages/playwright/src/matchers/toMatchAriaSnapshot.ts b/packages/playwright/src/matchers/toMatchAriaSnapshot.ts
index ef304fad46..1075d8d86d 100644
--- a/packages/playwright/src/matchers/toMatchAriaSnapshot.ts
+++ b/packages/playwright/src/matchers/toMatchAriaSnapshot.ts
@@ -20,8 +20,8 @@ import path from 'path';
import { escapeTemplateString, isString } from 'playwright-core/lib/utils';
-import { matcherHint } from './matcherHint';
-import { callLogText, fileExistsAsync } from '../util';
+import { formatMatcherMessage } from './matcherHint';
+import { fileExistsAsync } from '../util';
import { printReceivedStringContainExpectedSubstring } from './expect';
import { currentTestInfo } from '../common/globals';
@@ -39,7 +39,7 @@ type ToMatchAriaSnapshotExpected = {
export async function toMatchAriaSnapshot(
this: ExpectMatcherState,
- receiver: LocatorEx,
+ locator: LocatorEx,
expectedParam?: ToMatchAriaSnapshotExpected,
options: { timeout?: number } = {},
): Promise<MatcherResult<string | RegExp, string>> {
@@ -54,11 +54,6 @@ export async function toMatchAriaSnapshot(
const updateSnapshots = testInfo.config.updateSnapshots;
- const matcherOptions = {
- isNot: this.isNot,
- promise: this.promise,
- };
-
let expected: string;
let timeout: number;
let expectedPath: string | undefined;
@@ -88,35 +83,39 @@ export async function toMatchAriaSnapshot(
}
expected = unshift(expected);
- const { matches: pass, received, log, timedOut, errorMessage } = await receiver._expect('to.match.aria', { expectedValue: expected, isNot: this.isNot, timeout });
+ const { matches: pass, received, log, timedOut, errorMessage } = await locator._expect('to.match.aria', { expectedValue: expected, isNot: this.isNot, timeout });
const typedReceived = received as MatcherReceived;
- const matcherHintWithExpect = (expectedReceivedString: string) => {
- return matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined, expectedReceivedString);
- };
-
- if (errorMessage) {
- return {
- pass: this.isNot,
- message: () => matcherHintWithExpect(`Expected: ${this.utils.printExpected(expected)}\n${errorMessage}`) + callLogText(log),
- name: 'toMatchAriaSnapshot',
- expected,
- };
- }
-
- const receivedText = typedReceived.raw;
const message = () => {
- if (pass) {
- const receivedString = printReceivedStringContainExpectedSubstring(receivedText, receivedText.indexOf(expected), expected.length);
- const expectedReceivedString = `Expected: not ${this.utils.printExpected(expected)}\nReceived: ${receivedString}`;
- return matcherHintWithExpect(expectedReceivedString) + callLogText(log);
+ let printedExpected: string | undefined;
+ let printedReceived: string | undefined;
+ let printedDiff: string | undefined;
+ if (errorMessage) {
+ printedExpected = `Expected: ${this.isNot ? 'not ' : ''}${this.utils.printExpected(expected)}`;
+ } else if (pass) {
+ const receivedString = printReceivedStringContainExpectedSubstring(typedReceived.raw, typedReceived.raw.indexOf(expected), expected.length);
+ printedExpected = `Expected: not ${this.utils.printExpected(expected)}`;
+ printedReceived = `Received: ${receivedString}`;
} else {
- const labelExpected = `Expected`;
- const expectedReceivedString = this.utils.printDiffOrStringify(expected, receivedText, labelExpected, 'Received', false);
- return matcherHintWithExpect(expectedReceivedString) + callLogText(log);
+ printedDiff = this.utils.printDiffOrStringify(expected, typedReceived.raw, 'Expected', 'Received', false);
}
+ return formatMatcherMessage(this, {
+ matcherName,
+ expectation: 'expected',
+ locator,
+ timeout,
+ timedOut,
+ printedExpected,
+ printedReceived,
+ printedDiff,
+ errorMessage,
+ log,
+ });
};
+ if (errorMessage)
+ return { pass: this.isNot, message, name: 'toMatchAriaSnapshot', expected };
+
if (!this.isNot) {
if ((updateSnapshots === 'all') ||
(updateSnapshots === 'changed' && pass === this.isNot) ||
diff --git a/packages/playwright/src/matchers/toMatchSnapshot.ts b/packages/playwright/src/matchers/toMatchSnapshot.ts
index 5136fa6884..3f36ea6445 100644
--- a/packages/playwright/src/matchers/toMatchSnapshot.ts
+++ b/packages/playwright/src/matchers/toMatchSnapshot.ts
@@ -21,8 +21,8 @@ import { compareBuffersOrStrings, getComparator, isString } from 'playwright-cor
import { colors } from 'playwright-core/lib/utils';
import { mime } from 'playwright-core/lib/utilsBundle';
-import { addSuffixToFilePath, callLogText, expectTypes } from '../util';
-import { matcherHint } from './matcherHint';
+import { addSuffixToFilePath, expectTypes } from '../util';
+import { callLogText, formatMatcherMessage } from './matcherHint';
import { currentTestInfo } from '../common/globals';
import type { MatcherResult } from './matcherHint';
@@ -301,8 +301,7 @@ export function toMatchSnapshot(
if (!result)
return helper.handleMatching();
- const receiver = isString(received) ? 'string' : 'Buffer';
- const header = matcherHint(this, undefined, 'toMatchSnapshot', receiver, undefined, undefined, undefined);
+ const header = formatMatcherMessage(this, { matcherName: 'toMatchSnapshot', receiver: isString(received) ? 'string' : 'Buffer', expectation: 'expected' });
return helper.handleDifferent(received, expected, undefined, result.diff, header, result.errorMessage, undefined, this._stepInfo);
}
@@ -376,14 +375,13 @@ export async function toHaveScreenshot(
if (helper.updateSnapshots === 'none' && !hasSnapshot)
return helper.createMatcherResult(`A snapshot doesn't exist at ${helper.expectedPath}.`, false);
- const receiver = locator ? 'locator' : 'page';
if (!hasSnapshot) {
// Regenerate a new screenshot by waiting until two screenshots are the same.
const { actual, previous, diff, errorMessage, log, timedOut } = await page._expectScreenshot(expectScreenshotOptions);
// We tried re-generating new snapshot but failed.
// This can be due to e.g. spinning animation, so we want to show it as a diff.
if (errorMessage) {
- const header = matcherHint(this, locator, 'toHaveScreenshot', receiver, undefined, undefined, timedOut ? timeout : undefined);
+ const header = formatMatcherMessage(this, { matcherName: 'toHaveScreenshot', locator, expectation: 'expected', timeout, timedOut });
return helper.handleDifferent(actual, undefined, previous, diff, header, errorMessage, log, this._stepInfo);
}
@@ -418,12 +416,12 @@ export async function toHaveScreenshot(
if (helper.updateSnapshots === 'changed' || helper.updateSnapshots === 'all') {
if (actual)
return writeFiles(actual);
- let header = matcherHint(this, undefined, 'toHaveScreenshot', receiver, undefined, undefined, timedOut ? timeout : undefined);
+ let header = formatMatcherMessage(this, { matcherName: 'toHaveScreenshot', locator, expectation: 'expected', timeout, timedOut });
header += ' Failed to re-generate expected.\n';
return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log, this._stepInfo);
}
- const header = matcherHint(this, undefined, 'toHaveScreenshot', receiver, undefined, undefined, timedOut ? timeout : undefined);
+ const header = formatMatcherMessage(this, { matcherName: 'toHaveScreenshot', locator, expectation: 'expected', timeout, timedOut });
return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log, this._stepInfo);
}
diff --git a/packages/playwright/src/matchers/toMatchText.ts b/packages/playwright/src/matchers/toMatchText.ts
index 1552022d98..8c5cfd06dc 100644
--- a/packages/playwright/src/matchers/toMatchText.ts
+++ b/packages/playwright/src/matchers/toMatchText.ts
@@ -15,14 +15,12 @@
*/
-import { colors } from 'playwright-core/lib/utils';
-
-import { callLogText, expectTypes } from '../util';
+import { expectTypes } from '../util';
import {
printReceivedStringContainExpectedResult,
printReceivedStringContainExpectedSubstring
} from './expect';
-import { matcherHint } from './matcherHint';
+import { formatMatcherMessage } from './matcherHint';
import { EXPECTED_COLOR } from '../common/expectBundle';
import { runBrowserBackendOnError } from '../mcp/test/browserBackend';
@@ -37,25 +35,17 @@ export async function toMatchText(
receiverType: 'Locator' | 'Page',
query: (isNot: boolean, timeout: number) => Promise<{ matches: boolean, received?: string, log?: string[], timedOut?: boolean, errorMessage?: string }>,
expected: string | RegExp,
- options: { timeout?: number, matchSubstring?: boolean, receiverLabel?: string } = {},
+ options: { timeout?: number, matchSubstring?: boolean } = {},
): Promise<MatcherResult<string | RegExp, string>> {
expectTypes(receiver, [receiverType], matcherName);
-
- const matcherOptions = {
- isNot: this.isNot,
- promise: this.promise,
- };
+ const locator = receiverType === 'Locator' ? receiver as Locator : undefined;
if (
!(typeof expected === 'string') &&
!(expected && typeof expected.test === 'function')
) {
- // Same format as jest's matcherErrorMessage
- throw new Error([
- matcherHint(this, receiverType === 'Locator' ? receiver as Locator : undefined, matcherName, options.receiverLabel ?? receiver, expected, matcherOptions, undefined, undefined, true),
- `${colors.bold('Matcher error')}: ${EXPECTED_COLOR('expected',)} value must be a string or regular expression`,
- this.utils.printWithType('Expected', expected, this.utils.printExpected)
- ].join('\n\n'));
+ const errorMessage = `Error: ${EXPECTED_COLOR('expected')} value must be a string or regular expression\n${this.utils.printWithType('Expected', expected, this.utils.printExpected)}`;
+ throw new Error(formatMatcherMessage(this, { locator, matcherName, expectation: 'expected', errorMessage }));
}
const timeout = options.timeout ?? this.timeout;
@@ -71,48 +61,50 @@ export async function toMatchText(
};
}
- const stringSubstring = options.matchSubstring ? 'substring' : 'string';
+ const expectedSuffix = typeof expected === 'string' ? (options.matchSubstring ? ' substring' : '') : ' pattern';
+ const receivedSuffix = typeof expected === 'string' ? (options.matchSubstring ? ' string' : '') : ' string';
const receivedString = received || '';
-
let printedReceived: string | undefined;
let printedExpected: string | undefined;
let printedDiff: string | undefined;
if (pass) {
if (typeof expected === 'string') {
- printedExpected = `Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}`;
- if (errorMessage) {
- printedReceived = errorMessage;
- } else {
+ printedExpected = `Expected${expectedSuffix}: not ${this.utils.printExpected(expected)}`;
+ if (!errorMessage) {
const formattedReceived = printReceivedStringContainExpectedSubstring(receivedString, receivedString.indexOf(expected), expected.length);
- printedReceived = `Received string: ${formattedReceived}`;
+ printedReceived = `Received${receivedSuffix}: ${formattedReceived}`;
}
} else {
- printedExpected = `Expected pattern: not ${this.utils.printExpected(expected)}`;
- if (errorMessage) {
- printedReceived = errorMessage;
- } else {
+ printedExpected = `Expected${expectedSuffix}: not ${this.utils.printExpected(expected)}`;
+ if (!errorMessage) {
const formattedReceived = printReceivedStringContainExpectedResult(receivedString, typeof expected.exec === 'function' ? expected.exec(receivedString) : null);
- printedReceived = `Received string: ${formattedReceived}`;
+ printedReceived = `Received${receivedSuffix}: ${formattedReceived}`;
}
}
} else {
- const labelExpected = `Expected ${typeof expected === 'string' ? stringSubstring : 'pattern'}`;
- if (errorMessage) {
- printedExpected = `${labelExpected}: ${this.utils.printExpected(expected)}`;
- printedReceived = errorMessage;
- } else {
- printedDiff = this.utils.printDiffOrStringify(expected, receivedString, labelExpected, 'Received string', false);
- }
+ if (errorMessage)
+ printedExpected = `Expected${expectedSuffix}: ${this.utils.printExpected(expected)}`;
+ else
+ printedDiff = this.utils.printDiffOrStringify(expected, receivedString, `Expected${expectedSuffix}`, `Received${receivedSuffix}`, false);
}
const message = () => {
- const resultDetails = printedDiff ? printedDiff : printedExpected + '\n' + printedReceived;
- const hints = matcherHint(this, receiverType === 'Locator' ? receiver as Locator : undefined, matcherName, options.receiverLabel ?? 'locator', undefined, matcherOptions, timedOut ? timeout : undefined, resultDetails, true);
- return hints + callLogText(log);
+ return formatMatcherMessage(this, {
+ matcherName,
+ expectation: 'expected',
+ locator,
+ timeout,
+ timedOut,
+ printedExpected,
+ printedReceived,
+ printedDiff,
+ log,
+ errorMessage,
+ });
};
- if (receiverType === 'Locator')
- await runBrowserBackendOnError((receiver as Locator).page(), message);
+ if (locator)
+ await runBrowserBackendOnError(locator.page(), message);
return {
name: matcherName,
diff --git a/packages/playwright/src/util.ts b/packages/playwright/src/util.ts
index 71aab10048..65040aea91 100644
--- a/packages/playwright/src/util.ts
+++ b/packages/playwright/src/util.ts
@@ -20,7 +20,7 @@ import url from 'url';
import util from 'util';
import { parseStackFrame, sanitizeForFilePath, calculateSha1, isRegExp, isString, stringifyStackFrames } from 'playwright-core/lib/utils';
-import { colors, debug, mime, minimatch } from 'playwright-core/lib/utilsBundle';
+import { debug, mime, minimatch } from 'playwright-core/lib/utilsBundle';
import type { Location } from './../types/testReporter';
import type { TestInfoErrorImpl } from './common/ipc';
@@ -227,15 +227,6 @@ export function getContainedPath(parentPath: string, subPath: string = ''): stri
export const debugTest = debug('pw:test');
-export const callLogText = (log: string[] | undefined) => {
- if (!log || !log.some(l => !!l))
- return '';
- return `
-Call log:
-${colors.dim(log.join('\n'))}
-`;
-};
-
const folderToPackageJsonPath = new Map<string, string>();
export function getPackageJsonPath(folderPath: string): string {
diff --git a/tests/page/expect-boolean.spec.ts b/tests/page/expect-boolean.spec.ts
index d9174b4b25..4f256cb65a 100644
--- a/tests/page/expect-boolean.spec.ts
+++ b/tests/page/expect-boolean.spec.ts
@@ -123,12 +123,15 @@ Timeout: 1000ms`);
const error = await expect(locator2).not.toBeChecked({ timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).not.toBeChecked() failed
-Locator: locator('input2')
+Locator: locator('input2')
Expected: not checked
+Timeout: 1000ms
Error: element(s) not found
-Timeout: 1000ms`);
- expect(stripAnsi(error.message)).toContain(`- Expect "not toBeChecked" with timeout 1000ms`);
- expect(stripAnsi(error.message)).toContain(`- waiting for locator(\'input2\')`);
+
+Call log:
+ - Expect "not toBeChecked" with timeout 1000ms
+ - waiting for locator('input2')
+`);
});
test('with role', async ({ page }) => {
@@ -466,11 +469,14 @@ test.describe('toBeHidden', () => {
const error = await expect(locator).not.toBeHidden({ timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).not.toBeHidden() failed
-Locator: locator('button')
+Locator: locator('button')
Expected: not hidden
+Timeout: 1000ms
Error: element(s) not found
-Timeout: 1000ms`);
- expect(stripAnsi(error.message)).toContain(`- Expect "not toBeHidden" with timeout 1000ms`);
+
+Call log:
+ - Expect "not toBeHidden" with timeout 1000ms
+`);
});
test('with impossible timeout .not', async ({ page }) => {
@@ -568,9 +574,10 @@ test.describe(() => {
test('text content type', async ({ page, server }) => {
const res = await page.request.get(`${server.PREFIX}/text-content-type`);
const error = await expect(res).toBeOK().catch(e => e);
+ expect(stripAnsi(error.message)).toContain(`expect(response).toBeOK() failed`);
expect(error.message).toContain(`→ GET ${server.PREFIX}/text-content-type`);
expect(error.message).toContain(`← 404 Not Found`);
- expect(error.message).toContain(`Text error`);
+ expect(stripAnsi(error.message)).toContain(`Response text:\nText error`);
});
test('no content type', async ({ page, server }) => {
diff --git a/tests/page/expect-matcher-result.spec.ts b/tests/page/expect-matcher-result.spec.ts
index 549df88e46..d5afc751d1 100644
--- a/tests/page/expect-matcher-result.spec.ts
+++ b/tests/page/expect-matcher-result.spec.ts
@@ -87,12 +87,13 @@ test('toBeTruthy-based assertions should have matcher result', async ({ page })
expect.soft(stripAnsi(e.toString())).toContain(`Error: expect(locator).toBeVisible() failed
-Locator: locator('#node2')
+Locator: locator('#node2')
Expected: visible
+Timeout: 1ms
Error: element(s) not found
-Timeout: 1ms
-Call log`);
+Call log:
+`);
}
@@ -116,7 +117,8 @@ Expected: not visible
Received: visible
Timeout: 1ms
-Call log`);
+Call log:
+`);
}
});
diff --git a/tests/page/expect-misc.spec.ts b/tests/page/expect-misc.spec.ts
index e7a91dd10f..e550d3ca72 100644
--- a/tests/page/expect-misc.spec.ts
+++ b/tests/page/expect-misc.spec.ts
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-import { stripVTControlCharacters } from 'node:util';
import { stripAnsi } from '../config/utils';
import { test, expect } from './pageTest';
@@ -243,10 +242,13 @@ test.describe('toHaveClass', () => {
const error = await expect(locator).toHaveClass('foo bar baz', { timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveClass(expected) failed
-Locator: locator('div')
-Expected string: "foo bar baz"
-Received string: "bar baz"
-Timeout: 1000ms`);
+Locator: locator('div')
+Expected: "foo bar baz"
+Received: "bar baz"
+Timeout: 1000ms
+
+Call log:
+`);
expect(stripAnsi(error.message)).toContain(`- Expect "toHaveClass" with timeout 1000ms`);
});
@@ -289,10 +291,13 @@ test.describe('toContainClass', () => {
const error = await expect(locator).toContainClass('does-not-exist', { timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).toContainClass(expected) failed
-Locator: locator('div')
-Expected string: "does-not-exist"
-Received string: "bar baz"
-Timeout: 1000ms`);
+Locator: locator('div')
+Expected: "does-not-exist"
+Received: "bar baz"
+Timeout: 1000ms
+
+Call log:
+`);
expect(stripAnsi(error.message)).toContain(`- Expect "toContainClass" with timeout 1000ms`);
});
@@ -326,9 +331,12 @@ test.describe('toHaveTitle', () => {
const error = await expect(page).toHaveTitle('Hello', { timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(page).toHaveTitle(expected) failed
-Expected string: "Hello"
-Received string: "Bye"
-Timeout: 1000ms`);
+Expected: "Hello"
+Received: "Bye"
+Timeout: 1000ms
+
+Call log:
+`);
expect(stripAnsi(error.message)).toContain(`- Expect "toHaveTitle" with timeout 1000ms`);
});
});
@@ -344,32 +352,46 @@ test.describe('toHaveURL', () => {
const error = await expect(page).toHaveURL('wrong', { timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(page).toHaveURL(expected) failed
-Expected string: "wrong"
-Received string: "data:text/html,<div>A</div>"
-Timeout: 1000ms`);
- expect(stripVTControlCharacters(error.message)).toContain('Expected string: "wrong"\nReceived string: "data:text/html,<div>A</div>"');
+Expected: "wrong"
+Received: "data:text/html,<div>A</div>"
+Timeout: 1000ms
+
+Call log:
+`);
});
test('fail with invalid argument', async ({ page }) => {
await page.goto('data:text/html,<div>A</div>');
// @ts-expect-error
const error = await expect(page).toHaveURL({}).catch(e => e);
- expect(stripVTControlCharacters(error.message)).toContain(`expect(page).toHaveURL([object Object])`);
- expect(stripVTControlCharacters(error.message)).toContain('Expected has type: object\nExpected has value: {}');
+ expect(stripAnsi(error.message)).toContain(`expect(page).toHaveURL(expected) failed
+
+Error: expected value must be a string or regular expression
+Expected has type: object
+Expected has value: {}
+`);
});
test('fail with positive predicate', async ({ page }) => {
await page.goto('data:text/html,<div>A</div>');
const error = await expect(page).toHaveURL(_url => false).catch(e => e);
- expect(stripVTControlCharacters(error.message)).toContain('expect(page).toHaveURL(expected)');
- expect(stripVTControlCharacters(error.message)).toContain('Expected predicate to succeed\nReceived string: "data:text/html,<div>A</div>"');
+ expect(stripAnsi(error.message)).toContain(`expect(page).toHaveURL(expected) failed
+
+Expected: predicate to succeed
+Received: "data:text/html,<div>A</div>"
+Timeout: 10000ms
+`);
});
test('fail with negative predicate', async ({ page }) => {
await page.goto('data:text/html,<div>A</div>');
const error = await expect(page).not.toHaveURL(_url => true).catch(e => e);
- expect(stripVTControlCharacters(error.message)).toContain('expect(page).not.toHaveURL(expected)');
- expect(stripVTControlCharacters(error.message)).toContain('Expected predicate to fail\nReceived string: "data:text/html,<div>A</div>"');
+ expect(stripAnsi(error.message)).toContain(`expect(page).not.toHaveURL(expected) failed
+
+Expected: predicate to fail
+Received: "data:text/html,<div>A</div>"
+Timeout: 10000ms
+`);
});
test('resolve predicate on initial call', async ({ page }) => {
@@ -404,10 +426,13 @@ test.describe('toHaveAttribute', () => {
const error = await expect(locator).toHaveAttribute('disabled', '', { timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveAttribute(expected) failed
-Locator: locator('#node')
-Expected string: ""
-Received string: serializes to the same string
-Timeout: 1000ms`);
+Locator: locator('#node')
+Expected: ""
+Received: serializes to the same string
+Timeout: 1000ms
+
+Call log:
+`);
expect(stripAnsi(error.message)).toContain(`- Expect "toHaveAttribute" with timeout 1000ms`);
}
{
@@ -417,7 +442,10 @@ Timeout: 1000ms`);
Locator: locator('#node')
Expected pattern: /.*/
Received string: ""
-Timeout: 1000ms`);
+Timeout: 1000ms
+
+Call log:
+`);
expect(stripAnsi(error.message)).toContain(`- Expect "toHaveAttribute" with timeout 1000ms`);
}
await expect(locator).not.toHaveAttribute('disabled', '');
@@ -433,10 +461,13 @@ Timeout: 1000ms`);
const error = await expect(locator).not.toHaveAttribute('checked', '', { timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).not.toHaveAttribute(expected) failed
-Locator: locator('#node')
-Expected string: not ""
-Received string: ""
-Timeout: 1000ms`);
+Locator: locator('#node')
+Expected: not ""
+Received: ""
+Timeout: 1000ms
+
+Call log:
+`);
expect(stripAnsi(error.message)).toContain(`- Expect "not toHaveAttribute" with timeout 1000ms`);
}
{
@@ -584,15 +615,29 @@ test('toHaveText that does not match should not produce logs twice', async ({ pa
});
test('strict mode violation error format', async ({ page }) => {
- await page.setContent('<div>hello</div><div>hi</div>');
- const error = await expect(page.locator('div')).toBeVisible().catch(e => e);
- expect(error.message).toContain('Expected: visible');
- expect(error.message).toContain(`Error: strict mode violation: locator('div') resolved to 2 elements:`);
+ await page.setContent('<div>a</div><div>b</div>');
+ const error = await expect(page.locator('div')).toHaveText('foo').catch(e => e);
+ expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected) failed
+
+Locator: locator('div')
+Expected: "foo"
+Error: strict mode violation: locator('div') resolved to 2 elements:
+ 1) <div>a</div> aka getByText('a')
+ 2) <div>b</div> aka getByText('b')
+
+Call log:
+`);
});
test('invalid selector error format', async ({ page }) => {
- await page.setContent('<div>hello</div><div>hi</div>');
+ await page.setContent('<div>a</div><div>b</div>');
const error = await expect(page.locator('##')).toBeVisible().catch(e => e);
- expect(error.message).toContain('Expected: visible');
- expect(error.message).toContain(`Error: Unexpected token "#" while parsing css selector "##".`);
+ expect(stripAnsi(error.message)).toContain(`expect(locator).toBeVisible() failed
+
+Locator: ##
+Expected: visible
+Error: Unexpected token "#" while parsing css selector "##". Did you mean to CSS.escape it?
+
+Call log:
+`);
});
diff --git a/tests/page/expect-timeout.spec.ts b/tests/page/expect-timeout.spec.ts
index b45a378aa5..211dab2d0c 100644
--- a/tests/page/expect-timeout.spec.ts
+++ b/tests/page/expect-timeout.spec.ts
@@ -17,15 +17,18 @@
import { stripAnsi } from '../config/utils';
import { test, expect } from './pageTest';
-test('should print timed out error message', async ({ page }) => {
+test('should print element not found', async ({ page }) => {
await page.setContent('<div id=node>Text content</div>');
const error = await expect(page.locator('no-such-thing')).toHaveText('hey', { timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected) failed
Locator: locator('no-such-thing')
-Expected string: "hey"
+Expected: "hey"
+Timeout: 1000ms
Error: element(s) not found
-Timeout: 1000ms`);
+
+Call log:
+`);
});
test('should print timed out error message when value does not match', async ({ page }) => {
@@ -33,10 +36,13 @@ test('should print timed out error message when value does not match', async ({
const error = await expect(page.locator('div')).toHaveText('hey', { timeout: 1000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected) failed
-Locator: locator('div')
-Expected string: "hey"
-Received string: "Text content"
-Timeout: 1000ms`);
+Locator: locator('div')
+Expected: "hey"
+Received: "Text content"
+Timeout: 1000ms
+
+Call log:
+`);
});
test('should print timed out error message with impossible timeout', async ({ page }) => {
@@ -45,9 +51,11 @@ test('should print timed out error message with impossible timeout', async ({ pa
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected) failed
Locator: locator('no-such-thing')
-Expected string: "hey"
+Expected: "hey"
+Timeout: 1ms
Error: element(s) not found
-Timeout: 1ms`);
+
+Call log:`);
});
test('should print timed out error message when value does not match with impossible timeout', async ({ page }) => {
@@ -55,10 +63,13 @@ test('should print timed out error message when value does not match with imposs
const error = await expect(page.locator('div')).toHaveText('hey', { timeout: 1 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected) failed
-Locator: locator('div')
-Expected string: "hey"
-Received string: "Text content"
-Timeout: 1ms`);
+Locator: locator('div')
+Expected: "hey"
+Received: "Text content"
+Timeout: 1ms
+
+Call log:
+`);
});
test('should have timeout error name', async ({ page }) => {
@@ -89,10 +100,13 @@ test('should timeout during first locator handler check', async ({ page, server
const error = await expect(page.locator('span')).toHaveText('bye', { timeout: 3000 }).catch(e => e);
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected) failed
-Locator: locator('span')
-Expected string: "bye"
-Received string: ""
-Timeout: 3000ms`);
+Locator: locator('span')
+Expected: "bye"
+Received: ""
+Timeout: 3000ms
+
+Call log:
+`);
expect(error.message).toContain(`locator handler has finished, waiting for locator('div') to be hidden`);
expect(error.message).toContain(`locator resolved to visible <div>hello</div>`);
});
diff --git a/tests/page/expect-to-have-text.spec.ts b/tests/page/expect-to-have-text.spec.ts
index 17f510e665..6b80d522fa 100644
--- a/tests/page/expect-to-have-text.spec.ts
+++ b/tests/page/expect-to-have-text.spec.ts
@@ -35,8 +35,15 @@ test.describe('toHaveText with regex', () => {
await page.setContent('<div id=node>Text content</div>');
const locator = page.locator('#node');
const error = await expect(locator).toHaveText(/Text 2/, { timeout: 1000 }).catch(e => e);
- expect(stripAnsi(error.message)).toContain('Expected pattern: /Text 2/');
- expect(stripAnsi(error.message)).toContain('Received string: "Text content"');
+ expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected) failed
+
+Locator: locator('#node')
+Expected pattern: /Text 2/
+Received string: "Text content"
+Timeout: 1000ms
+
+Call log:
+`);
});
});
@@ -59,6 +66,29 @@ test.describe('toContainText with regex', () => {
});
});
+test.describe('toContainText with string', () => {
+ test('pass', async ({ page }) => {
+ await page.setContent('<div id=node>Text content</div>');
+ const locator = page.locator('#node');
+ await expect(locator).toContainText('content');
+ });
+
+ test('fail', async ({ page }) => {
+ await page.setContent('<div id=node>Text content</div>');
+ const locator = page.locator('#node');
+ const error = await expect(locator).toContainText('foo', { timeout: 1000 }).catch(e => e);
+ expect(stripAnsi(error.message)).toContain(`expect(locator).toContainText(expected) failed
+
+Locator: locator('#node')
+Expected substring: "foo"
+Received string: "Text content"
+Timeout: 1000ms
+
+Call log:
+`);
+ });
+});
+
test.describe('toHaveText with text', () => {
test('pass', async ({ page }) => {
await page.setContent('<div id=node><span></span>Text \ncontent&nbsp; </div>');
@@ -91,8 +121,8 @@ test.describe('toHaveText with text', () => {
await page.setContent('<div id=node>Text content</div>');
const locator = page.locator('#node');
const error = await expect(locator).toHaveText('Text', { timeout: 1000 }).catch(e => e);
- expect(stripAnsi(error.message)).toContain('Expected string: "Text"');
- expect(stripAnsi(error.message)).toContain('Received string: "Text content"');
+ expect(stripAnsi(error.message)).toContain('Expected: "Text"');
+ expect(stripAnsi(error.message)).toContain('Received: "Text content"');
});
test('pass eventually', async ({ page }) => {
@@ -134,8 +164,8 @@ test.describe('toHaveText with text', () => {
test('fail with impossible timeout', async ({ page }) => {
await page.setContent('<div id=node>Text content</div>');
const error = await expect(page.locator('#node')).toHaveText('Text', { timeout: 1 }).catch(e => e);
- expect(stripAnsi(error.message)).toContain('Expected string: "Text"');
- expect(stripAnsi(error.message)).toContain('Received string: "Text content"');
+ expect(stripAnsi(error.message)).toContain('Expected: "Text"');
+ expect(stripAnsi(error.message)).toContain('Received: "Text content"');
});
});
@@ -152,14 +182,14 @@ test.describe('not.toHaveText', () => {
await page.setContent('<div id=node>Text content</div>');
const locator = page.locator('#node');
const error = await expect(locator).not.toHaveText('Text content', { timeout: 1000 }).catch(e => e);
- expect(stripAnsi(error.message)).toContain('Expected string: not "Text content"');
- expect(stripAnsi(error.message)).toContain('Received string: "Text content');
+ expect(stripAnsi(error.message)).toContain('Expected: not "Text content"');
+ expect(stripAnsi(error.message)).toContain('Received: "Text content');
});
test('should work when selector does not match', async ({ page }) => {
await page.setContent('<div>hello</div>');
const error = await expect(page.locator('span')).not.toHaveText('hello', { timeout: 1000 }).catch(e => e);
- expect(stripAnsi(error.message)).toContain('Expected string: not "hello"');
+ expect(stripAnsi(error.message)).toContain('Expected: not "hello"');
expect(stripAnsi(error.message)).toContain('Error: element(s) not found');
expect(stripAnsi(error.message)).toContain('waiting for locator(\'span\')');
});
@@ -226,11 +256,23 @@ test.describe('toHaveText with array', () => {
await page.setContent('<div>Text 1</div><div>Text 3</div>');
const locator = page.locator('div');
const error = await expect(locator).toHaveText(['Text 1', /Text \d/, 'Extra'], { timeout: 1000 }).catch(e => e);
- expect(stripAnsi(error.message)).toContain('- "Extra"');
- expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected)`);
- expect(stripAnsi(error.message)).toContain(`Timeout: 1000ms`);
- expect(stripAnsi(error.message)).toContain(`- Expect "toHaveText" with timeout 1000ms`);
- expect(stripAnsi(error.message)).toContain('waiting for locator(\'div\')');
+ expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected) failed
+
+Locator: locator('div')
+Timeout: 1000ms
+- Expected - 1
++ Received + 0
+
+ Array [
+ \"Text 1\",
+ \"Text 3\",
+- \"Extra\",
+ ]
+
+Call log:
+ - Expect \"toHaveText\" with timeout 1000ms
+ - waiting for locator('div')
+`);
expect(stripAnsi(error.message)).toContain('locator resolved to 2 elements');
});
diff --git a/tests/page/to-match-aria-snapshot.spec.ts b/tests/page/to-match-aria-snapshot.spec.ts
index f162ff8d13..71c5141969 100644
--- a/tests/page/to-match-aria-snapshot.spec.ts
+++ b/tests/page/to-match-aria-snapshot.spec.ts
@@ -338,8 +338,21 @@ test('selected attribute', async ({ page }) => {
const e = await expect(page.locator('body')).toMatchAriaSnapshot(`
- row [selected=false]
`, { timeout: 1000 }).catch(e => e);
- expect(stripAnsi(e.message)).toContain('expect(locator).toMatchAriaSnapshot(expected) failed');
- expect(stripAnsi(e.message)).toContain('Timeout: 1000ms');
+ expect(stripAnsi(e.message)).toContain(`expect(locator).toMatchAriaSnapshot(expected) failed
+
+Locator: locator('body')
+Timeout: 1000ms
+- Expected - 1
++ Received + 4
+
+- - row [selected=false]
++ - table:
++ - rowgroup:
++ - row "Row" [selected]:
++ - cell "Row"
+
+Call log:
+`);
}
{
@@ -425,8 +438,10 @@ test('expected formatter', async ({ page }) => {
// Note that error message should not contain any regular expressions,
// unlike the baseline generated by --update-snapshots.
- expect(stripAnsi(error.message)).toContain(`
-Locator: locator('body')
+ expect(stripAnsi(error.message)).toContain(`expect(locator).toMatchAriaSnapshot(expected) failed
+
+Locator: locator('body')
+Timeout: 1ms
- Expected - 2
+ Received + 4
@@ -436,7 +451,9 @@ Locator: locator('body')
+ - heading "todos" [level=1]
+ - textbox "What needs to be done?"
+ - button "Time 15:30"
-Timeout: 1ms`);
+
+Call log:
+`);
});
test('should unpack escaped names', async ({ page }) => {
@@ -730,7 +747,6 @@ test('should detect unexpected children: equal', async ({ page }) => {
`, { timeout: 1000 }).catch(e => e);
expect(stripAnsi(e.message)).toContain('expect(locator).toMatchAriaSnapshot(expected) failed');
- expect(stripAnsi(e.message)).toContain('Timeout: 1000ms');
expect(stripAnsi(e.message)).toContain('+ - listitem: Two');
});
@@ -770,7 +786,6 @@ test('should detect unexpected children: deep-equal', async ({ page }) => {
`, { timeout: 1000 }).catch(e => e);
expect(stripAnsi(e.message)).toContain('expect(locator).toMatchAriaSnapshot(expected) failed');
- expect(stripAnsi(e.message)).toContain('Timeout: 1000ms');
expect(stripAnsi(e.message)).toContain('+ - listitem: \"1.2\"');
});
@@ -795,7 +810,6 @@ test('should allow restoring contain mode inside deep-equal', async ({ page }) =
`, { timeout: 1000 }).catch(e => e);
expect(stripAnsi(e.message)).toContain('expect(locator).toMatchAriaSnapshot(expected) failed');
- expect(stripAnsi(e.message)).toContain('Timeout: 1000ms');
expect(stripAnsi(e.message)).toContain('+ - listitem: \"1.2\"');
await expect(page.locator('body')).toMatchAriaSnapshot(`
diff --git a/tests/playwright-test/expect-configure.spec.ts b/tests/playwright-test/expect-configure.spec.ts
index 6b568eb918..b0bf725cd6 100644
--- a/tests/playwright-test/expect-configure.spec.ts
+++ b/tests/playwright-test/expect-configure.spec.ts
@@ -49,7 +49,7 @@ test('should configure message', async ({ runInlineTest }) => {
expect(result.passed).toBe(0);
expect(result.output).toContain('Error: x-foo must be visible');
expect(result.output).toContain('expect(locator).toBeVisible() failed');
- expect(result.output).toContain('Timeout: 1ms');
+ expect(result.output).toContain('Timeout: 1ms');
expect(result.output).toContain('Call log:');
});
@@ -68,7 +68,7 @@ test('should prefer local message', async ({ runInlineTest }) => {
expect(result.output).toContain('Error: overridden');
expect(result.output).toContain(`expect(locator).toBeVisible() failed`);
- expect(result.output).toContain('Timeout: 1ms');
+ expect(result.output).toContain('Timeout: 1ms');
expect(result.output).toContain('Call log:');
});
diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts
index fca4d09e39..43c42447b6 100644
--- a/tests/playwright-test/expect.spec.ts
+++ b/tests/playwright-test/expect.spec.ts
@@ -68,7 +68,7 @@ test('should include custom expect message with web-first assertions', async ({
expect(result.output).toContain('Error: x-foo must be visible');
expect(result.output).toContain('expect(locator).toBeVisible() failed');
- expect(result.output).toContain('Timeout: 1ms');
+ expect(result.output).toContain('Timeout: 1ms');
expect(result.output).toContain('Call log:');
});
@@ -533,13 +533,13 @@ test('should support toHaveURL with baseURL from webServer', async ({ runInlineT
}, { workers: 1 });
const output = result.output;
expect(output).toContain('expect(page).toHaveURL');
- expect(output).toContain(`Expected string: \"http://localhost:${port}/kek\"`);
+ expect(output).toContain(`Expected: \"http://localhost:${port}/kek\"`);
expect(result.passed).toBe(1);
expect(result.failed).toBe(1);
expect(result.exitCode).toBe(1);
});
-test('should respect expect.timeout', async ({ runInlineTest }) => {
+test('should respect expect.timeout in toHaveURL', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`,
'a.test.ts': `
@@ -550,7 +550,7 @@ test('should respect expect.timeout', async ({ runInlineTest }) => {
await page.goto('data:text/html,<div>A</div>');
const error = await expect(page).toHaveURL('data:text/html,<div>B</div>').catch(e => e);
expect(stripVTControlCharacters(error.message)).toContain('expect(page).toHaveURL(expected) failed');
- expect(stripVTControlCharacters(error.message)).toContain('Timeout: 1000ms');
+ expect(stripVTControlCharacters(error.message)).toContain('Timeout: 1000ms');
expect(error.message).toContain('data:text/html,<div>');
});
`,
@@ -570,7 +570,7 @@ test('should support toHaveURL predicate', async ({ runInlineTest }) => {
await page.goto('data:text/html,<div>A</div>');
const error = await expect(page).toHaveURL(url => url === 'data:text/html,<div>B</div>').catch(e => e);
expect(stripVTControlCharacters(error.message)).toContain('expect(page).toHaveURL(expected) failed');
- expect(stripVTControlCharacters(error.message)).toContain('Timeout: 1000ms');
+ expect(stripVTControlCharacters(error.message)).toContain('Timeout: 1000ms');
expect(error.message).toContain('data:text/html,<div>');
});
`,
@@ -615,8 +615,8 @@ test('should print expected/received before timeout', async ({ runInlineTest })
expect(result.passed).toBe(0);
expect(result.failed).toBe(1);
expect(result.output).toContain('Test timeout of 2000ms exceeded.');
- expect(result.output).toContain('Expected string: "Text 2"');
- expect(result.output).toContain('Received string: "Text content"');
+ expect(result.output).toContain('Expected: "Text 2"');
+ expect(result.output).toContain('Received: "Text content"');
});
test('should print pending operations for toHaveText', async ({ runInlineTest }) => {
@@ -634,7 +634,7 @@ test('should print pending operations for toHaveText', async ({ runInlineTest })
expect(result.exitCode).toBe(1);
const output = result.output;
expect(output).toContain(`expect(locator).toHaveText(expected)`);
- expect(output).toContain('Expected string: "Text"');
+ expect(output).toContain('Expected: "Text"');
expect(output).toContain('Error: element(s) not found');
expect(output).toContain('waiting for locator(\'no-such-thing\')');
});
@@ -690,8 +690,8 @@ test('should print expected/received on Ctrl+C', async ({ interactWithTestRunner
const result = parseTestRunnerOutput(testProcess.output);
expect(result.passed).toBe(0);
expect(result.interrupted).toBe(1);
- expect(result.output).toContain('Expected string: "Text 2"');
- expect(result.output).toContain('Received string: "Text content"');
+ expect(result.output).toContain('Expected: "Text 2"');
+ expect(result.output).toContain('Received: "Text content"');
});
test('should not print timed out error message when test times out', async ({ runInlineTest }) => {
@@ -1024,7 +1024,7 @@ test('should respect timeout from configured expect when used outside of the tes
expect(code).toBe(1);
expect(stdout).toBe('');
expect(stripAnsi(stderr)).toContain('expect(locator).toBeAttached() failed');
- expect(stripAnsi(stderr)).toContain('Timeout: 10ms');
+ expect(stripAnsi(stderr)).toContain('Timeout: 10ms');
});
test('should expose timeout to custom matchers', async ({ runInlineTest, runTSC }) => {
diff --git a/tests/playwright-test/watch.spec.ts b/tests/playwright-test/watch.spec.ts
index 750c3f854d..45395f861b 100644
--- a/tests/playwright-test/watch.spec.ts
+++ b/tests/playwright-test/watch.spec.ts
@@ -721,7 +721,7 @@ test('should run CT on changed deps', async ({ runWatchTest, writeFiles }) => {
await testProcess.waitForOutput(`src${path.sep}button.spec.tsx:4:11 › pass`);
expect(testProcess.output).not.toContain(`src${path.sep}link.spec.tsx`);
await testProcess.waitForOutput(`Error: expect(locator).toHaveText(expected) failed`);
- await testProcess.waitForOutput('Timeout: 1000ms');
+ await testProcess.waitForOutput('Timeout: 1000ms');
await testProcess.waitForOutput('Waiting for file changes.');
});