File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3434,6 +3434,35 @@ class Playwright extends Helper {
34343434 }
34353435 }
34363436
3437+ /**
3438+ * {{> waitCurrentPathEquals }}
3439+ */
3440+ async waitCurrentPathEquals ( path , sec = null ) {
3441+ const waitTimeout = sec ? sec * 1000 : this . options . waitForTimeout
3442+ const normalizedPath = normalizePath ( path )
3443+
3444+ try {
3445+ await this . page . waitForFunction (
3446+ expectedPath => {
3447+ const actualPath = window . location . pathname
3448+ const normalizePath = p => ( p === '' || p === '/' ? '/' : p . replace ( / \/ + / g, '/' ) . replace ( / \/ $ / , '' ) || '/' )
3449+ return normalizePath ( actualPath ) === expectedPath
3450+ } ,
3451+ { timeout : waitTimeout } ,
3452+ normalizedPath ,
3453+ )
3454+ } catch ( e ) {
3455+ const currentUrl = await this . _getPageUrl ( )
3456+ const baseUrl = this . options . url || 'http://localhost'
3457+ const actualPath = new URL ( currentUrl , baseUrl ) . pathname
3458+ if ( / T i m e o u t / i. test ( e . message ) ) {
3459+ throw new Error ( `expected path to be ${ normalizedPath } , but found ${ normalizePath ( actualPath ) } ` )
3460+ } else {
3461+ throw e
3462+ }
3463+ }
3464+ }
3465+
34373466 /**
34383467 * {{> waitForText }}
34393468 */
Original file line number Diff line number Diff line change @@ -2497,6 +2497,35 @@ class Puppeteer extends Helper {
24972497 } )
24982498 }
24992499
2500+ /**
2501+ * {{> waitCurrentPathEquals }}
2502+ */
2503+ async waitCurrentPathEquals ( path , sec = null ) {
2504+ const waitTimeout = sec ? sec * 1000 : this . options . waitForTimeout
2505+ const normalizedPath = normalizePath ( path )
2506+
2507+ return this . page
2508+ . waitForFunction (
2509+ expectedPath => {
2510+ const actualPath = window . location . pathname
2511+ const normalizePath = p => ( p === '' || p === '/' ? '/' : p . replace ( / \/ + / g, '/' ) . replace ( / \/ $ / , '' ) || '/' )
2512+ return normalizePath ( actualPath ) === expectedPath
2513+ } ,
2514+ { timeout : waitTimeout } ,
2515+ normalizedPath ,
2516+ )
2517+ . catch ( async e => {
2518+ const currUrl = await this . _getPageUrl ( )
2519+ const baseUrl = this . options . url || 'http://localhost'
2520+ const actualPath = new URL ( currUrl , baseUrl ) . pathname
2521+ if ( / W a i t i n g f a i l e d / i. test ( e . message ) || / f a i l e d : t i m e o u t / i. test ( e . message ) ) {
2522+ throw new Error ( `expected path to be ${ normalizedPath } , but found ${ normalizePath ( actualPath ) } ` )
2523+ } else {
2524+ throw e
2525+ }
2526+ } )
2527+ }
2528+
25002529 /**
25012530 * {{> waitForText }}
25022531 */
Original file line number Diff line number Diff line change @@ -2544,6 +2544,34 @@ class WebDriver extends Helper {
25442544 } )
25452545 }
25462546
2547+ /**
2548+ * {{> waitCurrentPathEquals }}
2549+ */
2550+ async waitCurrentPathEquals ( path , sec = null ) {
2551+ const aSec = sec || this . options . waitForTimeoutInSeconds
2552+ const normalizedPath = normalizePath ( path )
2553+ const baseUrl = this . options . url || 'http://localhost'
2554+ let actualPath = ''
2555+
2556+ return this . browser
2557+ . waitUntil (
2558+ async ( ) => {
2559+ const currUrl = await this . browser . getUrl ( )
2560+ const url = new URL ( currUrl , baseUrl )
2561+ actualPath = url . pathname
2562+ return normalizePath ( actualPath ) === normalizedPath
2563+ } ,
2564+ { timeout : aSec * 1000 } ,
2565+ )
2566+ . catch ( e => {
2567+ e = wrapError ( e )
2568+ if ( e . message . indexOf ( 'timeout' ) ) {
2569+ throw new Error ( `expected path to be ${ normalizedPath } , but found ${ normalizePath ( actualPath ) } ` )
2570+ }
2571+ throw e
2572+ } )
2573+ }
2574+
25472575 /**
25482576 * {{> waitForText }}
25492577 *
Original file line number Diff line number Diff line change @@ -172,6 +172,28 @@ export function tests() {
172172 } )
173173 } )
174174
175+ describe ( '#waitCurrentPathEquals' , ( ) => {
176+ it ( 'should wait for path to match (ignoring query strings)' , async ( ) => {
177+ await I . amOnPage ( '/info' )
178+ await I . waitCurrentPathEquals ( '/info' )
179+ } )
180+
181+ it ( 'should wait timeout with proper error message' , async ( ) => {
182+ try {
183+ await I . amOnPage ( '/info' )
184+ await I . waitCurrentPathEquals ( '/nonexistent' , 0.1 )
185+ } catch ( e ) {
186+ assert . include ( e . message , 'expected path to be /nonexistent' )
187+ }
188+ } )
189+
190+ it ( 'should normalize paths when comparing' , async ( ) => {
191+ await I . amOnPage ( '/form/field/' )
192+ await I . waitCurrentPathEquals ( '/form/field' )
193+ await I . waitCurrentPathEquals ( '/form/field/' )
194+ } )
195+ } )
196+
175197 describe ( 'see text : #see' , ( ) => {
176198 it ( 'should check text on site' , async ( ) => {
177199 await I . amOnPage ( '/' )
You can’t perform that action at this time.
0 commit comments