Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expect): normalize whitespace to expect().toHaveValue() #33909

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function cacheNormalizedWhitespaces() {
export function normalizeWhiteSpace(text: string): string {
let result = normalizedWhitespaceCache?.get(text);
if (result === undefined) {
result = text.replace(/\u200b/g, '').trim().replace(/\s+/g, ' ');
result = text.replace(/\u200b/g, '').replace(/ /g, ' ').trim().replace(/\s+/g, ' ');
normalizedWhitespaceCache?.set(text, result);
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/src/matchers/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export function toHaveValue(
options?: { timeout?: number },
) {
return toMatchText.call(this, 'toHaveValue', locator, 'Locator', async (isNot, timeout) => {
const expectedText = serializeExpectedTextValues([expected]);
const expectedText = serializeExpectedTextValues([expected], { normalizeWhiteSpace: true });
return await locator._expect('to.have.value', { expectedText, isNot, timeout });
}, expected, options);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/page/expect-to-have-value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ test('should support failure', async ({ page }) => {
expect(stripAnsi(error.message)).toContain('"Text content"');
});

test('should normalize whitespace', async ({ page }) => {
await page.setContent('<input id=node></input>');
const locator = page.locator('#node');
await locator.fill('foo&nbsp;bar\nbaz');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in exactly this text being inserted into the field, which is different from non-breaking whitespaces rendered as whitespace but returned as &nbsp; for the input value. Reading the bug I see that we were not able to find a good repro. Let's start with composing a good test case that fails with the current API before implementing this feature.

image

await expect(locator).toHaveValue('foo bar baz');
});

test.describe('toHaveValues with multi-select', () => {
test('works with text', async ({ page }) => {
await page.setContent(`
Expand Down
Loading