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(locator).toHaveAttribute to assert attribute presence #16767

Merged
merged 3 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions docs/src/api/class-locatorassertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,9 @@ Attribute name.

### param: LocatorAssertions.toHaveAttribute.value
* since: v1.18
- `value` <[string]|[RegExp]>
- `value` ?<[string]|[RegExp]>

Expected attribute value.
Optional expected attribute value. If missing, method will assert attribute presence.
aslushnikov marked this conversation as resolved.
Show resolved Hide resolved

### option: LocatorAssertions.toHaveAttribute.timeout = %%-js-assertions-timeout-%%
* since: v1.18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,9 @@ export class InjectedScript {
{
// Element state / boolean values.
let elementState: boolean | 'error:notconnected' | 'error:notcheckbox' | undefined;
if (expression === 'to.be.checked') {
if (expression === 'to.have.attribute') {
elementState = element.hasAttribute(options.expressionArg);
} else if (expression === 'to.be.checked') {
elementState = progress.injectedScript.elementState(element, 'checked');
} else if (expression === 'to.be.unchecked') {
elementState = progress.injectedScript.elementState(element, 'unchecked');
Expand Down Expand Up @@ -1082,7 +1084,7 @@ export class InjectedScript {
{
// Single text value.
let received: string | undefined;
if (expression === 'to.have.attribute') {
if (expression === 'to.have.attribute.value') {
received = element.getAttribute(options.expressionArg) || '';
} else if (expression === 'to.have.class') {
received = element.classList.toString();
Expand Down
9 changes: 7 additions & 2 deletions packages/playwright-test/src/matchers/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,17 @@ export function toHaveAttribute(
this: ReturnType<Expect['getState']>,
locator: LocatorEx,
name: string,
expected: string | RegExp,
expected: string | RegExp | undefined,
options?: { timeout?: number },
) {
if (expected === undefined) {
return toBeTruthy.call(this, 'toHaveAttribute', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
return await locator._expect(customStackTrace, 'to.have.attribute', { expressionArg: name, isNot, timeout });
}, options);
}
return toMatchText.call(this, 'toHaveAttribute', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
const expectedText = toExpectedTextValues([expected]);
return await locator._expect(customStackTrace, 'to.have.attribute', { expressionArg: name, expectedText, isNot, timeout });
return await locator._expect(customStackTrace, 'to.have.attribute.value', { expressionArg: name, expectedText, isNot, timeout });
}, expected, options);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-test/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3431,10 +3431,10 @@ interface LocatorAssertions {
* ```
*
* @param name Attribute name.
* @param value Expected attribute value.
* @param value Optional expected attribute value. If missing, method will assert attribute presence.
* @param options
*/
toHaveAttribute(name: string, value: string|RegExp, options?: {
toHaveAttribute(name: string, value?: string|RegExp, options?: {
/**
* Time to retry the assertion for. Defaults to `timeout` in `TestConfig.expect`.
*/
Expand Down
5 changes: 4 additions & 1 deletion tests/page/expect-misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,11 @@ test.describe('toHaveURL', () => {

test.describe('toHaveAttribute', () => {
test('pass', async ({ page }) => {
await page.setContent('<div id=node>Text content</div>');
await page.setContent('<div checked id=node>Text content</div>');
const locator = page.locator('#node');
await expect(locator).toHaveAttribute('id');
await expect(locator).toHaveAttribute('checked');
await expect(locator).not.toHaveAttribute('open');
await expect(locator).toHaveAttribute('id', 'node');
});
});
Expand Down