Revert "feat: expect(locator).toHaveAttribute to assert attribute presence (#16767)"

This reverts commit 622c73cc1e.
This commit is contained in:
Andrey Lushnikov 2022-09-19 13:55:33 -04:00
parent 90ac0f56d5
commit 48e97d8b88
5 changed files with 11 additions and 31 deletions

View file

@ -909,16 +909,11 @@ Whether to use `element.innerText` instead of `element.textContent` when retriev
* langs: * langs:
- alias-java: hasAttribute - alias-java: hasAttribute
Ensures the [Locator] points to an element with given attribute. If the method Ensures the [Locator] points to an element with given attribute.
is used without `'value'` argument, then the method will assert attribute existance.
```js ```js
const locator = page.locator('input'); const locator = page.locator('input');
// Assert attribute with given value.
await expect(locator).toHaveAttribute('type', 'text'); await expect(locator).toHaveAttribute('type', 'text');
// Assert attribute existance.
await expect(locator).toHaveAttribute('disabled');
await expect(locator).not.toHaveAttribute('open');
``` ```
```java ```java
@ -952,9 +947,9 @@ Attribute name.
### param: LocatorAssertions.toHaveAttribute.value ### param: LocatorAssertions.toHaveAttribute.value
* since: v1.18 * since: v1.18
- `value` ?<[string]|[RegExp]> - `value` <[string]|[RegExp]>
Optional expected attribute value. If missing, method will assert attribute presence. Expected attribute value.
### option: LocatorAssertions.toHaveAttribute.timeout = %%-js-assertions-timeout-%% ### option: LocatorAssertions.toHaveAttribute.timeout = %%-js-assertions-timeout-%%
* since: v1.18 * since: v1.18

View file

@ -1038,9 +1038,7 @@ export class InjectedScript {
{ {
// Element state / boolean values. // Element state / boolean values.
let elementState: boolean | 'error:notconnected' | 'error:notcheckbox' | undefined; let elementState: boolean | 'error:notconnected' | 'error:notcheckbox' | undefined;
if (expression === 'to.have.attribute') { if (expression === 'to.be.checked') {
elementState = element.hasAttribute(options.expressionArg);
} else if (expression === 'to.be.checked') {
elementState = progress.injectedScript.elementState(element, 'checked'); elementState = progress.injectedScript.elementState(element, 'checked');
} else if (expression === 'to.be.unchecked') { } else if (expression === 'to.be.unchecked') {
elementState = progress.injectedScript.elementState(element, 'unchecked'); elementState = progress.injectedScript.elementState(element, 'unchecked');
@ -1100,7 +1098,7 @@ export class InjectedScript {
{ {
// Single text value. // Single text value.
let received: string | undefined; let received: string | undefined;
if (expression === 'to.have.attribute.value') { if (expression === 'to.have.attribute') {
received = element.getAttribute(options.expressionArg) || ''; received = element.getAttribute(options.expressionArg) || '';
} else if (expression === 'to.have.class') { } else if (expression === 'to.have.class') {
received = element.classList.toString(); received = element.classList.toString();

View file

@ -141,17 +141,12 @@ export function toHaveAttribute(
this: ReturnType<Expect['getState']>, this: ReturnType<Expect['getState']>,
locator: LocatorEx, locator: LocatorEx,
name: string, name: string,
expected: string | RegExp | undefined, expected: string | RegExp,
options?: { timeout?: number }, 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) => { return toMatchText.call(this, 'toHaveAttribute', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
const expectedText = toExpectedTextValues([expected]); const expectedText = toExpectedTextValues([expected]);
return await locator._expect(customStackTrace, 'to.have.attribute.value', { expressionArg: name, expectedText, isNot, timeout }); return await locator._expect(customStackTrace, 'to.have.attribute', { expressionArg: name, expectedText, isNot, timeout });
}, expected, options); }, expected, options);
} }

View file

@ -3437,23 +3437,18 @@ interface LocatorAssertions {
}): Promise<void>; }): Promise<void>;
/** /**
* Ensures the [Locator] points to an element with given attribute. If the method is used without `'value'` argument, then * Ensures the [Locator] points to an element with given attribute.
* the method will assert attribute existance.
* *
* ```js * ```js
* const locator = page.locator('input'); * const locator = page.locator('input');
* // Assert attribute with given value.
* await expect(locator).toHaveAttribute('type', 'text'); * await expect(locator).toHaveAttribute('type', 'text');
* // Assert attribute existance.
* await expect(locator).toHaveAttribute('disabled');
* await expect(locator).not.toHaveAttribute('open');
* ``` * ```
* *
* @param name Attribute name. * @param name Attribute name.
* @param value Optional expected attribute value. If missing, method will assert attribute presence. * @param value Expected attribute value.
* @param options * @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`. * Time to retry the assertion for. Defaults to `timeout` in `TestConfig.expect`.
*/ */

View file

@ -228,11 +228,8 @@ test.describe('toHaveURL', () => {
test.describe('toHaveAttribute', () => { test.describe('toHaveAttribute', () => {
test('pass', async ({ page }) => { test('pass', async ({ page }) => {
await page.setContent('<div checked id=node>Text content</div>'); await page.setContent('<div id=node>Text content</div>');
const locator = page.locator('#node'); 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'); await expect(locator).toHaveAttribute('id', 'node');
}); });
}); });