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.
if (result.matches === options.isNot)
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;
}

View file

@ -1166,7 +1166,7 @@ export class InjectedScript {
throw this.createStacklessError('Element is not a checkbox');
if (elementState === 'error:notconnected')
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 { stringifyStackFrames } from 'playwright-core/lib/utils';
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';
if (timeout)

View file

@ -40,13 +40,13 @@ export async function toBeTruthy(
};
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 message = () => {
const header = matcherHint(this, receiver, matcherName, 'locator', arg, matcherOptions, timedOut ? timeout : undefined);
const logText = callLogText(log);
return matches ? `${header}Expected: not ${expected}\nReceived: ${expected}${logText}` :
`${header}Expected: ${expected}\nReceived: ${unexpected}${logText}`;
return matches ? `${header}Expected: not ${expected}\nReceived: ${received ?? expected}${logText}` :
`${header}Expected: ${expected}\nReceived: ${received ?? unexpected}${logText}`;
};
return {
message,

View file

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

View file

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

View file

@ -158,7 +158,7 @@ test.describe('not.toHaveText', () => {
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('Received string: ""');
expect(stripAnsi(error.message)).toContain('Received string: "<element(s) not found>"');
expect(stripAnsi(error.message)).toContain('waiting for locator(\'span\')');
});
});

View file

@ -14,6 +14,7 @@
* limitations under the License.
*/
import { stripAnsi } from '../config/utils';
import { test as it, expect } from './pageTest';
it('should outlive frame navigation', async ({ page, server }) => {
@ -23,3 +24,36 @@ it('should outlive frame navigation', async ({ page, server }) => {
}, 1000);
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;
expect(output).toContain(`expect(locator).toHaveText(expected)`);
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\')');
});