@@ -826,8 +826,9 @@ class Playwright extends Helper {
826826
827827 async _stopBrowser ( ) {
828828 this . withinLocator = null ;
829- this . _setPage ( null ) ;
829+ await this . _setPage ( null ) ;
830830 this . context = null ;
831+ this . frame = null ;
831832 popupStore . clear ( ) ;
832833 await this . browser . close ( ) ;
833834 }
@@ -866,6 +867,7 @@ class Playwright extends Helper {
866867 this . withinLocator = null ;
867868 this . context = await this . page ;
868869 this . contextLocator = null ;
870+ this . frame = null ;
869871 }
870872
871873 _extractDataFromPerformanceTiming ( timing , ...dataNames ) {
@@ -1155,6 +1157,9 @@ class Playwright extends Helper {
11551157 */
11561158 async _locate ( locator ) {
11571159 const context = await this . context || await this . _getContext ( ) ;
1160+
1161+ if ( this . frame ) return findElements ( this . frame , locator ) ;
1162+
11581163 return findElements ( context , locator ) ;
11591164 }
11601165
@@ -1881,11 +1886,11 @@ class Playwright extends Helper {
18811886 * @returns {Promise<any> }
18821887 */
18831888 async executeScript ( fn , arg ) {
1884- let context = this . page ;
1885- if ( this . context && this . context . constructor . name === 'Frame' ) {
1886- context = this . context ; // switching to iframe context
1889+ if ( this . context && this . context . constructor . name === 'FrameLocator' ) {
1890+ // switching to iframe context
1891+ return this . context . locator ( ':root' ) . evaluate ( fn , arg ) ;
18871892 }
1888- return context . evaluate . apply ( context , [ fn , arg ] ) ;
1893+ return this . page . evaluate . apply ( this . page , [ fn , arg ] ) ;
18891894 }
18901895
18911896 /**
@@ -2407,7 +2412,7 @@ class Playwright extends Helper {
24072412 }
24082413
24092414 async _getContext ( ) {
2410- if ( this . context && this . context . constructor . name === 'Frame ' ) {
2415+ if ( this . context && this . context . constructor . name === 'FrameLocator ' ) {
24112416 return this . context ;
24122417 }
24132418 return this . page ;
@@ -2480,6 +2485,14 @@ class Playwright extends Helper {
24802485 } , [ locator . value , text , $XPath . toString ( ) ] , { timeout : waitTimeout } ) ;
24812486 }
24822487 } else {
2488+ // we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
2489+ if ( this . frame ) {
2490+ const { setTimeout } = require ( 'timers/promises' ) ;
2491+ await setTimeout ( waitTimeout ) ;
2492+ waiter = await this . frame . locator ( `:has-text('${ text } ')` ) . first ( ) . isVisible ( ) ;
2493+ if ( ! waiter ) throw new Error ( `Text "${ text } " was not found on page after ${ waitTimeout / 1000 } sec` ) ;
2494+ return ;
2495+ }
24832496 waiter = contextObject . waitForFunction ( text => document . body && document . body . innerText . indexOf ( text ) > - 1 , text , { timeout : waitTimeout } ) ;
24842497 }
24852498 return waiter . catch ( ( err ) => {
@@ -2534,37 +2547,37 @@ class Playwright extends Helper {
25342547 }
25352548
25362549 if ( locator >= 0 && locator < childFrames . length ) {
2537- this . context = childFrames [ locator ] ;
2550+ this . context = await this . page . frameLocator ( 'iframe' ) . nth ( locator ) ;
25382551 this . contextLocator = locator ;
25392552 } else {
25402553 throw new Error ( 'Element #invalidIframeSelector was not found by text|CSS|XPath' ) ;
25412554 }
25422555 return ;
25432556 }
2544- let contentFrame ;
25452557
25462558 if ( ! locator ) {
2547- this . context = await this . page . frames ( ) [ 0 ] ;
2559+ this . context = this . page ;
25482560 this . contextLocator = null ;
2561+ this . frame = null ;
25492562 return ;
25502563 }
25512564
25522565 // iframe by selector
2553- const els = await this . _locate ( locator ) ;
2554- if ( ! els [ 0 ] ) {
2555- throw new Error ( `Element ${ JSON . stringify ( locator ) } was not found by text|CSS|XPath` ) ;
2566+ locator = buildLocatorString ( new Locator ( locator , 'css' ) ) ;
2567+ const frame = await this . _locateElement ( locator ) ;
2568+
2569+ if ( ! frame ) {
2570+ throw new Error ( `Frame ${ JSON . stringify ( locator ) } was not found by text|CSS|XPath` ) ;
25562571 }
25572572
2558- // get content of the first iframe
2559- locator = new Locator ( locator , 'css' ) ;
2560- if ( ( locator . frame && locator . frame === 'iframe' ) || locator . value . toLowerCase ( ) === 'iframe' ) {
2561- contentFrame = await this . page . frames ( ) [ 1 ] ;
2562- // get content of the iframe using its name
2563- } else if ( locator . value . toLowerCase ( ) . includes ( 'name=' ) ) {
2564- const frameName = locator . value . split ( '=' ) [ 1 ] . replace ( / " / g, '' ) . replaceAll ( / ] / g, '' ) ;
2565- contentFrame = await this . page . frame ( frameName ) ;
2573+ if ( this . frame ) {
2574+ this . frame = await this . frame . frameLocator ( locator ) ;
2575+ } else {
2576+ this . frame = await this . page . frameLocator ( locator ) ;
25662577 }
25672578
2579+ const contentFrame = this . frame ;
2580+
25682581 if ( contentFrame ) {
25692582 this . context = contentFrame ;
25702583 this . contextLocator = null ;
@@ -3339,13 +3352,9 @@ async function proceedSee(assertType, text, context, strict = false) {
33393352 let allText ;
33403353
33413354 if ( ! context ) {
3342- let el = await this . context ;
3343- if ( el && ! el . getProperty ) {
3344- // Fallback to body
3345- el = await this . page . $ ( 'body' ) ;
3346- }
3355+ const el = await this . context ;
33473356
3348- allText = [ await el . innerText ( ) ] ;
3357+ allText = [ await el . locator ( 'body' ) . innerText ( ) ] ;
33493358 description = 'web application' ;
33503359 } else {
33513360 const locator = new Locator ( context , 'css' ) ;
@@ -3518,8 +3527,7 @@ async function elementSelected(element) {
35183527function isFrameLocator ( locator ) {
35193528 locator = new Locator ( locator ) ;
35203529 if ( locator . isFrame ( ) ) {
3521- const _locator = new Locator ( locator . value ) ;
3522- return _locator . value ;
3530+ return locator . value ;
35233531 }
35243532 return false ;
35253533}
0 commit comments