Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 3 additions & 1 deletion docs/migration-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ Test files written for 3.x keep working until you flip the flag.

### `wait*` Methods Resolve Relative URLs

`waitInUrl`, `waitUrlEquals`, and `waitCurrentPathEquals` now resolve a relative path against the helper's configured `url` before comparing. In 3.x a literal substring match against `window.location.href` would fail for relative paths.
`waitUrlEquals` and `waitCurrentPathEquals` now resolve a relative path against the helper's configured `url` before comparing. In 3.x a literal comparison against `window.location.href` would fail for relative paths.

```js
// helpers: { Playwright: { url: 'https://app.example.com' } }
Expand All @@ -623,6 +623,8 @@ I.waitUrlEquals('/dashboard') // matches https://app.example.com/dashboard
I.waitInUrl('/users') // matches any URL containing /users
```

`waitInUrl` is unchanged from 3.x — it stays a plain substring match against the current URL and never resolves its argument.

`waitUrlEquals` error messages now include the actual URL the page was on when the wait timed out — easier to diagnose `/dashboard` vs `/dashboard?session=expired`.

## 6. Adopt New Behaviors
Expand Down
5 changes: 2 additions & 3 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -3423,21 +3423,20 @@ class Playwright extends Helper {
*/
async waitInUrl(urlPart, sec = null) {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
const expectedUrl = resolveUrl(urlPart, this.options.url)

return this.page
.waitForFunction(
urlPart => {
const currUrl = decodeURIComponent(decodeURIComponent(decodeURIComponent(window.location.href)))
return currUrl.indexOf(urlPart) > -1
},
expectedUrl,
urlPart,
{ timeout: waitTimeout },
)
.catch(async e => {
const currUrl = await this._getPageUrl()
if (/Timeout/i.test(e.message)) {
throw new Error(`expected url to include ${expectedUrl}, but found ${currUrl}`)
throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`)
} else {
throw e
}
Expand Down
5 changes: 2 additions & 3 deletions lib/helper/Puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,6 @@ class Puppeteer extends Helper {
*/
async waitInUrl(urlPart, sec = null) {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
const expectedUrl = resolveUrl(urlPart, this.options.url)

return this.page
.waitForFunction(
Expand All @@ -2510,12 +2509,12 @@ class Puppeteer extends Helper {
return currUrl.indexOf(urlPart) > -1
},
{ timeout: waitTimeout },
expectedUrl,
urlPart,
)
.catch(async e => {
const currUrl = await this._getPageUrl()
if (/Waiting failed:/i.test(e.message) || /failed: timeout/i.test(e.message)) {
throw new Error(`expected url to include ${expectedUrl}, but found ${currUrl}`)
throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`)
} else {
throw e
}
Expand Down
5 changes: 2 additions & 3 deletions lib/helper/WebDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2521,23 +2521,22 @@ class WebDriver extends Helper {
async waitInUrl(urlPart, sec = null) {
const client = this.browser
const aSec = sec || this.options.waitForTimeoutInSeconds
const expectedUrl = resolveUrl(urlPart, this.options.url)
let currUrl = ''

return client
.waitUntil(
function () {
return this.getUrl().then(res => {
currUrl = decodeUrl(res)
return currUrl.indexOf(expectedUrl) > -1
return currUrl.indexOf(urlPart) > -1
})
},
{ timeout: aSec * 1000 },
)
.catch(e => {
e = wrapError(e)
if (e.message.indexOf('timeout')) {
throw new Error(`expected url to include ${expectedUrl}, but found ${currUrl}`)
throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`)
}
throw e
})
Expand Down
17 changes: 14 additions & 3 deletions test/helper/webapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,24 @@ export function tests() {

describe('#waitInUrl, #waitUrlEquals', () => {
it('should wait part of the URL to match the expected', async () => {
await I.amOnPage('/info')
await I.waitInUrl('/info')
await I.waitInUrl(`${siteUrl}/info`)

let err
try {
await I.amOnPage('/info')
await I.waitInUrl('/info')
await I.waitInUrl('/info2', 0.1)
} catch (e) {
assert.include(e.message, `expected url to include ${siteUrl}/info2, but found ${siteUrl}/info`)
err = e
}
assert.isDefined(err, 'expected waitInUrl to time out')
assert.include(err.message, `expected url to include /info2, but found ${siteUrl}/info`)
})

it('should match a URL part that is not anchored at the base url', async () => {
await I.amOnPage('/info?user=test')
await I.waitInUrl('user=test')
await I.waitInUrl('/info?user=test')
})

it('should wait for the entire URL to match the expected', async () => {
Expand Down
Loading