Skip to content

Commit

Permalink
chore: render user-friendly intermediate match values (#18867)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Nov 17, 2022
1 parent 4e58b0c commit b1acc0d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 24 additions & 0 deletions packages/playwright-core/src/server/injected/injectedScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 10 additions & 0 deletions tests/page/expect-boolean.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit b1acc0d

Please sign in to comment.