mirror of
https://github.com/microsoft/playwright.git
synced 2025-01-06 03:16:17 +03:00
fix(expect): toHaveAttribute with empty value should not match missing attribute (#17477)
Reference #16517
This commit is contained in:
parent
00cc7c2ac2
commit
cd9a5946d2
@ -1099,7 +1099,10 @@ export class InjectedScript {
|
|||||||
// Single text value.
|
// Single text value.
|
||||||
let received: string | undefined;
|
let received: string | undefined;
|
||||||
if (expression === 'to.have.attribute') {
|
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') {
|
} else if (expression === 'to.have.class') {
|
||||||
received = element.classList.toString();
|
received = element.classList.toString();
|
||||||
} else if (expression === 'to.have.css') {
|
} else if (expression === 'to.have.css') {
|
||||||
|
@ -232,6 +232,36 @@ test.describe('toHaveAttribute', () => {
|
|||||||
const locator = page.locator('#node');
|
const locator = page.locator('#node');
|
||||||
await expect(locator).toHaveAttribute('id', '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', () => {
|
test.describe('toHaveCSS', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user