feat(expect) - add ignoreCase option for toHaveURL (#30192)

feat(expect): add ignoreCase option for toHaveURL

Fixes #30057
This commit is contained in:
Козынченко Вячеслав 2024-04-03 03:20:46 +04:00 committed by GitHub
parent 81bcf2a53b
commit e58a33a0ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 2 deletions

View file

@ -116,6 +116,13 @@ The opposite of [`method: PageAssertions.toHaveURL`].
Expected URL string or RegExp. Expected URL string or RegExp.
### option: PageAssertions.NotToHaveURL.ignoreCase
* since: v1.44
* langs: js
- `ignoreCase` <[boolean]>
Whether to perform case-insensitive match. [`option: ignoreCase`] option takes precedence over the corresponding regular expression flag if specified.
### option: PageAssertions.NotToHaveURL.timeout = %%-csharp-java-python-assertions-timeout-%% ### option: PageAssertions.NotToHaveURL.timeout = %%-csharp-java-python-assertions-timeout-%%
* since: v1.18 * since: v1.18
@ -325,6 +332,13 @@ await Expect(Page).ToHaveURL(new Regex(".*checkout"));
Expected URL string or RegExp. Expected URL string or RegExp.
### option: PageAssertions.toHaveURL.ignoreCase
* since: v1.44
* langs: js
- `ignoreCase` <[boolean]>
Whether to perform case-insensitive match. [`option: ignoreCase`] option takes precedence over the corresponding regular expression flag if specified.
### option: PageAssertions.toHaveURL.timeout = %%-js-assertions-timeout-%% ### option: PageAssertions.toHaveURL.timeout = %%-js-assertions-timeout-%%
* since: v1.18 * since: v1.18

View file

@ -326,13 +326,13 @@ export function toHaveURL(
this: ExpectMatcherContext, this: ExpectMatcherContext,
page: Page, page: Page,
expected: string | RegExp, expected: string | RegExp,
options?: { timeout?: number }, options?: { ignoreCase?: boolean, timeout?: number },
) { ) {
const baseURL = (page.context() as any)._options.baseURL; const baseURL = (page.context() as any)._options.baseURL;
expected = typeof expected === 'string' ? constructURLBasedOnBaseURL(baseURL, expected) : expected; expected = typeof expected === 'string' ? constructURLBasedOnBaseURL(baseURL, expected) : expected;
const locator = page.locator(':root') as LocatorEx; const locator = page.locator(':root') as LocatorEx;
return toMatchText.call(this, 'toHaveURL', locator, 'Locator', async (isNot, timeout) => { return toMatchText.call(this, 'toHaveURL', locator, 'Locator', async (isNot, timeout) => {
const expectedText = toExpectedTextValues([expected]); const expectedText = toExpectedTextValues([expected], { ignoreCase: options?.ignoreCase });
return await locator._expect('to.have.url', { expectedText, isNot, timeout }); return await locator._expect('to.have.url', { expectedText, isNot, timeout });
}, expected, options); }, expected, options);
} }

View file

@ -7210,6 +7210,12 @@ interface PageAssertions {
* @param options * @param options
*/ */
toHaveURL(urlOrRegExp: string|RegExp, options?: { toHaveURL(urlOrRegExp: string|RegExp, options?: {
/**
* 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`. * Time to retry the assertion for in milliseconds. Defaults to `timeout` in `TestConfig.expect`.
*/ */

View file

@ -245,6 +245,11 @@ test.describe('toHaveURL', () => {
const error = await expect(page).toHaveURL('wrong', { timeout: 1000 }).catch(e => e); const error = await expect(page).toHaveURL('wrong', { timeout: 1000 }).catch(e => e);
expect(error.message).toContain('expect.toHaveURL with timeout 1000ms'); expect(error.message).toContain('expect.toHaveURL with timeout 1000ms');
}); });
test('support ignoreCase', async ({ page }) => {
await page.goto('data:text/html,<div>A</div>');
await expect(page).toHaveURL('DATA:teXT/HTml,<div>a</div>', { ignoreCase: true });
});
}); });
test.describe('toHaveAttribute', () => { test.describe('toHaveAttribute', () => {