fix: ignoreCase in toHaveAttribute (#27809)

Fixes #27795
This commit is contained in:
Yury Semikhatsky 2023-10-25 19:22:13 -07:00 committed by GitHub
parent 47733b04fb
commit 0ade5aa9ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 2 deletions

View File

@ -1151,6 +1151,12 @@ Expected attribute value.
### option: LocatorAssertions.toHaveAttribute.timeout = %%-csharp-java-python-assertions-timeout-%%
* since: v1.18
### option: LocatorAssertions.toHaveAttribute.ignoreCase = %%-js-assertions-timeout-%%
* since: v1.40
- `ignoreCase` <[boolean]>
Whether to perform case-insensitive match. [`option: ignoreCase`] option takes precedence over the corresponding regular expression flag if specified.
## async method: LocatorAssertions.toHaveAttribute#2
* since: v1.39
* langs: js

View File

@ -178,7 +178,7 @@ export function toHaveAttribute(
locator: LocatorEx,
name: string,
expected: string | RegExp | undefined | { timeout?: number },
options?: { timeout?: number },
options?: { timeout?: number, ignoreCase?: boolean },
) {
if (!options) {
// Update params for the case toHaveAttribute(name, options);
@ -193,7 +193,7 @@ export function toHaveAttribute(
}, options);
}
return toMatchText.call(this, 'toHaveAttribute', locator, 'Locator', async (isNot, timeout) => {
const expectedText = toExpectedTextValues([expected as (string | RegExp)]);
const expectedText = toExpectedTextValues([expected as (string | RegExp)], { ignoreCase: options?.ignoreCase });
return await locator._expect('to.have.attribute.value', { expressionArg: name, expectedText, isNot, timeout });
}, expected as (string | RegExp), options);
}

View File

@ -5708,6 +5708,14 @@ interface LocatorAssertions {
* @param options
*/
toHaveAttribute(name: string, value: string|RegExp, options?: {
/**
* Time to retry the assertion for in milliseconds. Defaults to `timeout` in `TestConfig.expect`.
*
* Whether to perform case-insensitive match. `ignoreCase` option takes precedence over the corresponding regular
* expression flag if specified.
*/
ignoreCase?: boolean;
/**
* Time to retry the assertion for in milliseconds. Defaults to `timeout` in `TestConfig.expect`.
*/

View File

@ -299,6 +299,13 @@ test.describe('toHaveAttribute', () => {
await expect(locator).toHaveAttribute('checked', { timeout: 5000 });
await expect(locator).not.toHaveAttribute('open', { timeout: 5000 });
});
test('support ignoreCase', async ({ page }) => {
await page.setContent('<div id=NoDe>Text content</div>');
const locator = page.locator('#NoDe');
await expect(locator).toHaveAttribute('id', 'node', { ignoreCase: true });
await expect(locator).not.toHaveAttribute('id', 'node');
});
});
test.describe('toHaveCSS', () => {