chore: render user-friendly intermediate match values (#18867)

This commit is contained in:
Pavel Feldman 2022-11-16 18:05:36 -08:00 committed by GitHub
parent 4e58b0c2ea
commit b1acc0d0ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 1 deletions

View File

@ -1406,7 +1406,7 @@ export class Frame extends SdkObject {
// expect(locator).not.conditionThatDoesMatch
progress.setIntermediateResult(result.received);
if (!Array.isArray(result.received))
progress.log(` unexpected value "${result.received}"`);
progress.log(` unexpected value "${progress.injectedScript.renderUnexpectedValue(options.expression, result.received)}"`);
return progress.continuePolling;
}

View File

@ -1205,6 +1205,30 @@ export class InjectedScript {
throw this.createStacklessError('Unknown expect matcher: ' + expression);
}
renderUnexpectedValue(expression: string, received: any): string {
if (expression === 'to.be.checked')
return received ? 'checked' : 'unchecked';
if (expression === 'to.be.unchecked')
return received ? 'unchecked' : 'checked';
if (expression === 'to.be.visible')
return received ? 'visible' : 'hidden';
if (expression === 'to.be.hidden')
return received ? 'hidden' : 'visible';
if (expression === 'to.be.enabled')
return received ? 'enabled' : 'disabled';
if (expression === 'to.be.disabled')
return received ? 'disabled' : 'enabled';
if (expression === 'to.be.editable')
return received ? 'editable' : 'readonly';
if (expression === 'to.be.readonly')
return received ? 'readonly' : 'editable';
if (expression === 'to.be.empty')
return received ? 'empty' : 'not empty';
if (expression === 'to.be.focused')
return received ? 'focused' : 'not focused';
return received;
}
expectArray(elements: Element[], options: FrameExpectParams): { matches: boolean, received?: any } {
const expression = options.expression;

View File

@ -86,6 +86,16 @@ test.describe('toBeChecked', () => {
});
}
});
test('friendly log', async ({ page }) => {
await page.setContent('<input type=checkbox></input>');
const message1 = await expect(page.locator('input')).toBeChecked({ timeout: 1000 }).catch(e => e.message);
expect(message1).toContain('unexpected value "unchecked"');
await page.setContent('<input type=checkbox checked></input>');
const message2 = await expect(page.locator('input')).toBeChecked({ checked: false, timeout: 1000 }).catch(e => e.message);
expect(message2).toContain('unexpected value "checked"');
});
});
test.describe('toBeEditable', () => {