Skip to content

Commit 79f0ea7

Browse files
author
DavertMik
committed
imrpved wait* methods
1 parent f1e376e commit 79f0ea7

5 files changed

Lines changed: 35 additions & 39 deletions

File tree

lib/helper/Playwright.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
requireWithFallback,
2525
normalizeSpacesInString,
2626
normalizePath,
27+
resolveUrl,
2728
relativeDir,
2829
} from '../utils.js'
2930
import { isColorProperty, convertColorToRGBA } from '../colorUtils.js'
@@ -3383,20 +3384,21 @@ class Playwright extends Helper {
33833384
*/
33843385
async waitInUrl(urlPart, sec = null) {
33853386
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
3387+
const expectedUrl = resolveUrl(urlPart, this.options.url)
33863388

33873389
return this.page
33883390
.waitForFunction(
33893391
urlPart => {
33903392
const currUrl = decodeURIComponent(decodeURIComponent(decodeURIComponent(window.location.href)))
33913393
return currUrl.indexOf(urlPart) > -1
33923394
},
3393-
urlPart,
3395+
expectedUrl,
33943396
{ timeout: waitTimeout },
33953397
)
33963398
.catch(async e => {
3397-
const currUrl = await this._getPageUrl() // Required because the waitForFunction can't return data.
3399+
const currUrl = await this._getPageUrl()
33983400
if (/Timeout/i.test(e.message)) {
3399-
throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`)
3401+
throw new Error(`expected url to include ${expectedUrl}, but found ${currUrl}`)
34003402
} else {
34013403
throw e
34023404
}
@@ -3408,26 +3410,17 @@ class Playwright extends Helper {
34083410
*/
34093411
async waitUrlEquals(urlPart, sec = null) {
34103412
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
3411-
3412-
const baseUrl = this.options.url
3413-
let expectedUrl = urlPart
3414-
if (urlPart.indexOf('http') < 0) {
3415-
expectedUrl = baseUrl + urlPart
3416-
}
3413+
const expectedUrl = resolveUrl(urlPart, this.options.url)
34173414

34183415
try {
34193416
await this.page.waitForURL(
3420-
url => url.href.includes(expectedUrl),
3417+
url => url.href === expectedUrl,
34213418
{ timeout: waitTimeout },
34223419
)
34233420
} catch (e) {
34243421
const currUrl = await this._getPageUrl()
34253422
if (/Timeout/i.test(e.message)) {
3426-
if (!currUrl.includes(expectedUrl)) {
3427-
throw new Error(`expected url to be ${expectedUrl}, but found ${currUrl}`)
3428-
} else {
3429-
throw new Error(`expected url not loaded, error message: ${e.message}`)
3430-
}
3423+
throw new Error(`expected url to be ${expectedUrl}, but found ${currUrl}`)
34313424
} else {
34323425
throw e
34333426
}

lib/helper/Puppeteer.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
requireWithFallback,
2828
normalizeSpacesInString,
2929
normalizePath,
30+
resolveUrl,
3031
} from '../utils.js'
3132
import { isColorProperty, convertColorToRGBA } from '../colorUtils.js'
3233
import ElementNotFound from './errors/ElementNotFound.js'
@@ -2442,6 +2443,7 @@ class Puppeteer extends Helper {
24422443
*/
24432444
async waitInUrl(urlPart, sec = null) {
24442445
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
2446+
const expectedUrl = resolveUrl(urlPart, this.options.url)
24452447

24462448
return this.page
24472449
.waitForFunction(
@@ -2450,12 +2452,12 @@ class Puppeteer extends Helper {
24502452
return currUrl.indexOf(urlPart) > -1
24512453
},
24522454
{ timeout: waitTimeout },
2453-
urlPart,
2455+
expectedUrl,
24542456
)
24552457
.catch(async e => {
2456-
const currUrl = await this._getPageUrl() // Required because the waitForFunction can't return data.
2458+
const currUrl = await this._getPageUrl()
24572459
if (/Waiting failed:/i.test(e.message) || /failed: timeout/i.test(e.message)) {
2458-
throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`)
2460+
throw new Error(`expected url to include ${expectedUrl}, but found ${currUrl}`)
24592461
} else {
24602462
throw e
24612463
}
@@ -2467,30 +2469,21 @@ class Puppeteer extends Helper {
24672469
*/
24682470
async waitUrlEquals(urlPart, sec = null) {
24692471
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout
2470-
2471-
const baseUrl = this.options.url
2472-
let expectedUrl = urlPart
2473-
if (urlPart.indexOf('http') < 0) {
2474-
expectedUrl = baseUrl + urlPart
2475-
}
2472+
const expectedUrl = resolveUrl(urlPart, this.options.url)
24762473

24772474
return this.page
24782475
.waitForFunction(
24792476
url => {
24802477
const currUrl = decodeURIComponent(window.location.href)
2481-
return currUrl.indexOf(url) > -1
2478+
return currUrl === url
24822479
},
24832480
{ timeout: waitTimeout },
24842481
expectedUrl,
24852482
)
24862483
.catch(async e => {
24872484
const currUrl = await this._getPageUrl()
24882485
if (/Waiting failed/i.test(e.message) || /failed: timeout/i.test(e.message)) {
2489-
if (!currUrl.includes(expectedUrl)) {
2490-
throw new Error(`expected url to be ${expectedUrl}, but found ${currUrl}`)
2491-
} else {
2492-
throw new Error(`expected url not loaded, error message: ${e.message}`)
2493-
}
2486+
throw new Error(`expected url to be ${expectedUrl}, but found ${currUrl}`)
24942487
} else {
24952488
throw e
24962489
}

lib/helper/WebDriver.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
getNormalizedKeyAttributeValue,
2424
modifierKeys,
2525
normalizePath,
26+
resolveUrl,
2627
} from '../utils.js'
2728
import { isColorProperty, convertColorToRGBA } from '../colorUtils.js'
2829
import ElementNotFound from './errors/ElementNotFound.js'
@@ -2497,22 +2498,23 @@ class WebDriver extends Helper {
24972498
async waitInUrl(urlPart, sec = null) {
24982499
const client = this.browser
24992500
const aSec = sec || this.options.waitForTimeoutInSeconds
2501+
const expectedUrl = resolveUrl(urlPart, this.options.url)
25002502
let currUrl = ''
25012503

25022504
return client
25032505
.waitUntil(
25042506
function () {
25052507
return this.getUrl().then(res => {
25062508
currUrl = decodeUrl(res)
2507-
return currUrl.indexOf(urlPart) > -1
2509+
return currUrl.indexOf(expectedUrl) > -1
25082510
})
25092511
},
25102512
{ timeout: aSec * 1000 },
25112513
)
25122514
.catch(e => {
25132515
e = wrapError(e)
25142516
if (e.message.indexOf('timeout')) {
2515-
throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`)
2517+
throw new Error(`expected url to include ${expectedUrl}, but found ${currUrl}`)
25162518
}
25172519
throw e
25182520
})
@@ -2523,22 +2525,19 @@ class WebDriver extends Helper {
25232525
*/
25242526
async waitUrlEquals(urlPart, sec = null) {
25252527
const aSec = sec || this.options.waitForTimeoutInSeconds
2526-
const baseUrl = this.options.url
2527-
if (urlPart.indexOf('http') < 0) {
2528-
urlPart = baseUrl + urlPart
2529-
}
2528+
const expectedUrl = resolveUrl(urlPart, this.options.url)
25302529
let currUrl = ''
25312530
return this.browser
25322531
.waitUntil(function () {
25332532
return this.getUrl().then(res => {
25342533
currUrl = decodeUrl(res)
2535-
return currUrl === urlPart
2534+
return currUrl === expectedUrl
25362535
})
25372536
}, aSec * 1000)
25382537
.catch(e => {
25392538
e = wrapError(e)
25402539
if (e.message.indexOf('timeout')) {
2541-
throw new Error(`expected url to be ${urlPart}, but found ${currUrl}`)
2540+
throw new Error(`expected url to be ${expectedUrl}, but found ${currUrl}`)
25422541
}
25432542
throw e
25442543
})

lib/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ export const normalizePath = function (path) {
157157
.replace(/\/$/, '') || '/'
158158
}
159159

160+
export const resolveUrl = function (url, baseUrl) {
161+
if (!url) return url
162+
if (url.indexOf('http') === 0) return url
163+
if (!baseUrl) return url
164+
try {
165+
return new URL(url, baseUrl).href
166+
} catch (e) {
167+
return url
168+
}
169+
}
170+
160171
export const xpathLocator = {
161172
/**
162173
* @param {string} string

test/helper/webapi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export function tests() {
156156
await I.waitInUrl('/info')
157157
await I.waitInUrl('/info2', 0.1)
158158
} catch (e) {
159-
assert.include(e.message, `expected url to include /info2, but found ${siteUrl}/info`)
159+
assert.include(e.message, `expected url to include ${siteUrl}/info2, but found ${siteUrl}/info`)
160160
}
161161
})
162162

0 commit comments

Comments
 (0)