fix(expect): toHaveAttribute with empty value should not match missing attribute (#17477)

Reference #16517
This commit is contained in:
Yury Semikhatsky 2022-09-20 17:11:12 -07:00 committed by GitHub
parent 00cc7c2ac2
commit cd9a5946d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -1099,7 +1099,10 @@ export class InjectedScript {
// Single text value.
let received: string | undefined;
if (expression === 'to.have.attribute') {
received = element.getAttribute(options.expressionArg) || '';
const value = element.getAttribute(options.expressionArg);
if (value === null)
return { received: null, matches: false };
received = value;
} else if (expression === 'to.have.class') {
received = element.classList.toString();
} else if (expression === 'to.have.css') {

View File

@ -232,6 +232,36 @@ test.describe('toHaveAttribute', () => {
const locator = page.locator('#node');
await expect(locator).toHaveAttribute('id', 'node');
});
test('should not match missing attribute', async ({ page }) => {
await page.setContent('<div checked id=node>Text content</div>');
const locator = page.locator('#node');
{
const error = await expect(locator).toHaveAttribute('disabled', '', { timeout: 1000 }).catch(e => e);
expect(error.message).toContain('expect.toHaveAttribute with timeout 1000ms');
}
{
const error = await expect(locator).toHaveAttribute('disabled', /.*/, { timeout: 1000 }).catch(e => e);
expect(error.message).toContain('expect.toHaveAttribute with timeout 1000ms');
}
await expect(locator).not.toHaveAttribute('disabled', '');
await expect(locator).not.toHaveAttribute('disabled', /.*/);
});
test('should match boolean attribute', async ({ page }) => {
await page.setContent('<div checked id=node>Text content</div>');
const locator = page.locator('#node');
await expect(locator).toHaveAttribute('checked', '');
await expect(locator).toHaveAttribute('checked', /.*/);
{
const error = await expect(locator).not.toHaveAttribute('checked', '', { timeout: 1000 }).catch(e => e);
expect(error.message).toContain('expect.toHaveAttribute with timeout 1000ms');
}
{
const error = await expect(locator).not.toHaveAttribute('checked', /.*/, { timeout: 1000 }).catch(e => e);
expect(error.message).toContain('expect.toHaveAttribute with timeout 1000ms');
}
});
});
test.describe('toHaveCSS', () => {