docs: Improve toHaveURL doc clarity (#34935)

This commit is contained in:
Adam Gastineau 2025-02-27 11:00:50 -08:00 committed by GitHub
parent 08ea36caa2
commit b0ceed51a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 7 deletions

View File

@ -296,7 +296,18 @@ Ensures the page is navigated to the given URL.
**Usage**
```js
await expect(page).toHaveURL(/.*checkout/);
// Check for the page URL to be 'https://playwright.dev/docs/intro' (including query string)
await expect(page).toHaveURL('https://playwright.dev/docs/intro');
// Check for the page URL to contain 'doc', followed by an optional 's', followed by '/'
await expect(page).toHaveURL(/docs?\//);
// Check for the predicate to be satisfied
// For example: verify query strings
await expect(page).toHaveURL(url => {
const params = url.searchParams;
return params.has('search') && params.has('options') && params.get('id') === '5';
});
```
```java
@ -328,7 +339,7 @@ await Expect(Page).ToHaveURLAsync(new Regex(".*checkout"));
- `url` <[string]|[RegExp]|[function]\([URL]\):[boolean]>
Expected URL string, RegExp, or predicate receiving [URL] to match.
When a [`option: Browser.newContext.baseURL`] via the context options was provided and the passed URL is a path, it gets merged via the [`new URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) constructor.
When [`option: Browser.newContext.baseURL`] is provided via the context options and the `url` argument is a string, the two values are merged via the [`new URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) constructor and used for the comparison against the current browser URL.
### option: PageAssertions.toHaveURL.ignoreCase
* since: v1.44

View File

@ -8902,13 +8902,25 @@ interface PageAssertions {
* **Usage**
*
* ```js
* await expect(page).toHaveURL(/.*checkout/);
* // Check for the page URL to be 'https://playwright.dev/docs/intro' (including query string)
* await expect(page).toHaveURL('https://playwright.dev/docs/intro');
*
* // Check for the page URL to contain 'doc', followed by an optional 's', followed by '/'
* await expect(page).toHaveURL(/docs?\//);
*
* // Check for the predicate to be satisfied
* // For example: verify query strings
* await expect(page).toHaveURL(url => {
* const params = url.searchParams;
* return params.has('search') && params.has('options') && params.get('id') === '5';
* });
* ```
*
* @param url Expected URL string, RegExp, or predicate receiving [URL] to match. When a
* [`baseURL`](https://playwright.dev/docs/api/class-browser#browser-new-context-option-base-url) via the context
* options was provided and the passed URL is a path, it gets merged via the
* [`new URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) constructor.
* @param url Expected URL string, RegExp, or predicate receiving [URL] to match. When
* [`baseURL`](https://playwright.dev/docs/api/class-browser#browser-new-context-option-base-url) is provided via the
* context options and the `url` argument is a string, the two values are merged via the
* [`new URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) constructor and used for the comparison
* against the current browser URL.
* @param options
*/
toHaveURL(url: string|RegExp|((url: URL) => boolean), options?: {