fix(expect): throw better received error when no element was found

This commit is contained in:
Max Schmitt 2024-03-22 15:56:48 +01:00
parent b5e36583f6
commit 9d16a61c1d
9 changed files with 46 additions and 9 deletions

View file

@ -1382,6 +1382,8 @@ export class Frame extends SdkObject {
// Library mode special case for the expect errors which are return values, not exceptions. // Library mode special case for the expect errors which are return values, not exceptions.
if (result.matches === options.isNot) if (result.matches === options.isNot)
metadata.error = { error: { name: 'Expect', message: 'Expect failed' } }; metadata.error = { error: { name: 'Expect', message: 'Expect failed' } };
if (result.log?.[result.log.length - 1].startsWith('waiting for '))
result.received = '<element(s) not found>';
return result; return result;
} }

View file

@ -1166,7 +1166,7 @@ export class InjectedScript {
throw this.createStacklessError('Element is not a checkbox'); throw this.createStacklessError('Element is not a checkbox');
if (elementState === 'error:notconnected') if (elementState === 'error:notconnected')
throw this.createStacklessError('Element is not connected'); throw this.createStacklessError('Element is not connected');
return { received: elementState, matches: elementState }; return { matches: elementState };
} }
} }

View file

@ -20,6 +20,7 @@ import type { Locator } from 'playwright-core';
import type { StackFrame } from '@protocol/channels'; import type { StackFrame } from '@protocol/channels';
import { stringifyStackFrames } from 'playwright-core/lib/utils'; import { stringifyStackFrames } from 'playwright-core/lib/utils';
export function matcherHint(state: ExpectMatcherContext, locator: Locator | undefined, matcherName: string, expression: any, actual: any, matcherOptions: any, timeout?: number) { export function matcherHint(state: ExpectMatcherContext, locator: Locator | undefined, matcherName: string, expression: any, actual: any, matcherOptions: any, timeout?: number) {
let header = state.utils.matcherHint(matcherName, expression, actual, matcherOptions).replace(/ \/\/ deep equality/, '') + '\n\n'; let header = state.utils.matcherHint(matcherName, expression, actual, matcherOptions).replace(/ \/\/ deep equality/, '') + '\n\n';
if (timeout) if (timeout)

View file

@ -40,13 +40,13 @@ export async function toBeTruthy(
}; };
const timeout = currentExpectTimeout(options); const timeout = currentExpectTimeout(options);
const { matches, log, timedOut } = await query(!!this.isNot, timeout); const { matches, log, timedOut, received } = await query(!!this.isNot, timeout);
const actual = matches ? expected : unexpected; const actual = matches ? expected : unexpected;
const message = () => { const message = () => {
const header = matcherHint(this, receiver, matcherName, 'locator', arg, matcherOptions, timedOut ? timeout : undefined); const header = matcherHint(this, receiver, matcherName, 'locator', arg, matcherOptions, timedOut ? timeout : undefined);
const logText = callLogText(log); const logText = callLogText(log);
return matches ? `${header}Expected: not ${expected}\nReceived: ${expected}${logText}` : return matches ? `${header}Expected: not ${expected}\nReceived: ${received ?? expected}${logText}` :
`${header}Expected: ${expected}\nReceived: ${unexpected}${logText}`; `${header}Expected: ${expected}\nReceived: ${received ?? unexpected}${logText}`;
}; };
return { return {
message, message,

View file

@ -69,11 +69,11 @@ export async function toMatchText(
typeof expected === 'string' typeof expected === 'string'
? matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) + ? matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) +
`Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}\n` + `Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}\n` +
`Received string: ${printReceivedStringContainExpectedSubstring( `Received string: ${receivedString.indexOf(expected) !== -1 ? printReceivedStringContainExpectedSubstring(
receivedString, receivedString,
receivedString.indexOf(expected), receivedString.indexOf(expected),
expected.length, expected.length,
)}` + callLogText(log) ) : '"' + receivedString + '"'}` + callLogText(log)
: matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) + : matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) +
`Expected pattern: not ${this.utils.printExpected(expected)}\n` + `Expected pattern: not ${this.utils.printExpected(expected)}\n` +
`Received string: ${printReceivedStringContainExpectedResult( `Received string: ${printReceivedStringContainExpectedResult(

View file

@ -86,7 +86,7 @@ test('toBeTruthy-based assertions should have matcher result', async ({ page })
Locator: locator('#node2') Locator: locator('#node2')
Expected: visible Expected: visible
Received: hidden Received: <element(s) not found>
Call log`); Call log`);
} }

View file

@ -158,7 +158,7 @@ test.describe('not.toHaveText', () => {
await page.setContent('<div>hello</div>'); await page.setContent('<div>hello</div>');
const error = await expect(page.locator('span')).not.toHaveText('hello', { timeout: 1000 }).catch(e => e); 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 string: not "hello"');
expect(stripAnsi(error.message)).toContain('Received string: ""'); expect(stripAnsi(error.message)).toContain('Received string: "<element(s) not found>"');
expect(stripAnsi(error.message)).toContain('waiting for locator(\'span\')'); expect(stripAnsi(error.message)).toContain('waiting for locator(\'span\')');
}); });
}); });

View file

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { stripAnsi } from '../config/utils';
import { test as it, expect } from './pageTest'; import { test as it, expect } from './pageTest';
it('should outlive frame navigation', async ({ page, server }) => { it('should outlive frame navigation', async ({ page, server }) => {
@ -23,3 +24,36 @@ it('should outlive frame navigation', async ({ page, server }) => {
}, 1000); }, 1000);
await expect(page.locator('.box').first()).toBeEmpty(); await expect(page.locator('.box').first()).toBeEmpty();
}); });
it('should print no-locator-resolved error when locator matcher did not resolve to any element', async ({ page, server }) => {
const myLocator = page.locator('.nonexisting');
const expectWithShortLivingTimeout = expect.configure({ timeout: 10 });
const locatorMatchers = [
() => expectWithShortLivingTimeout(myLocator).toBeAttached(),
() => expectWithShortLivingTimeout(myLocator).toBeChecked(),
() => expectWithShortLivingTimeout(myLocator).toBeDisabled(),
() => expectWithShortLivingTimeout(myLocator).toBeEditable(),
() => expectWithShortLivingTimeout(myLocator).toBeEmpty(),
() => expectWithShortLivingTimeout(myLocator).toBeEnabled(),
() => expectWithShortLivingTimeout(myLocator).toBeFocused(),
() => expectWithShortLivingTimeout(myLocator).toBeInViewport(),
() => expectWithShortLivingTimeout(myLocator).toBeVisible(),
() => expectWithShortLivingTimeout(myLocator).toContainText('abc'),
() => expectWithShortLivingTimeout(myLocator).toHaveAttribute('abc'),
() => expectWithShortLivingTimeout(myLocator).toHaveClass('abc'),
() => expectWithShortLivingTimeout(myLocator).toHaveCSS('abc', 'abc'),
() => expectWithShortLivingTimeout(myLocator).toHaveId('abc'),
() => expectWithShortLivingTimeout(myLocator).toHaveJSProperty('abc', 'abc'),
() => expectWithShortLivingTimeout(myLocator).toHaveText('abc'),
() => expectWithShortLivingTimeout(myLocator).toHaveValue('abc'),
() => expectWithShortLivingTimeout(myLocator).toHaveValues(['abc']),
];
for (const matcher of locatorMatchers) {
await it.step(matcher.toString(), async () => {
const error = await matcher().catch(e => e);
expect(error).toBeInstanceOf(Error);
expect(error.message).toContain(`waiting for locator('.nonexisting')`);
expect(stripAnsi(error.message)).toMatch(/Received( string)?: "?<element\(s\) not found>/);
});
}
});

View file

@ -647,7 +647,7 @@ test('should print pending operations for toHaveText', async ({ runInlineTest })
const output = result.output; const output = result.output;
expect(output).toContain(`expect(locator).toHaveText(expected)`); expect(output).toContain(`expect(locator).toHaveText(expected)`);
expect(output).toContain('Expected string: "Text"'); expect(output).toContain('Expected string: "Text"');
expect(output).toContain('Received string: ""'); expect(output).toContain('Received string: "<element(s) not found>"');
expect(output).toContain('waiting for locator(\'no-such-thing\')'); expect(output).toContain('waiting for locator(\'no-such-thing\')');
}); });