@@ -30,7 +30,7 @@ import ElementNotFound from './errors/ElementNotFound.js'
3030import RemoteBrowserConnectionRefused from './errors/RemoteBrowserConnectionRefused.js'
3131import Popup from './extras/Popup.js'
3232import Console from './extras/Console.js'
33- import { findReact , findVue , findByPlaywrightLocator } from './extras/PlaywrightReactVueLocator .js'
33+ import { buildLocatorString , findElements , findElement , getVisibleElements , findReact , findVue , findByPlaywrightLocator , findByRole } from './extras/PlaywrightLocator .js'
3434
3535let playwright
3636let perfTiming
@@ -2424,13 +2424,21 @@ class Playwright extends Helper {
24242424 *
24252425 */
24262426 async grabTextFrom ( locator ) {
2427+ const originalLocator = locator
2428+ if ( typeof locator === 'object' && ( locator . role || locator . react || locator . vue || locator . pw ) ) {
2429+ const els = await this . _locate ( locator )
2430+ assertElementExists ( els , locator )
2431+ const text = await els [ 0 ] . innerText ( )
2432+ this . debugSection ( 'Text' , text )
2433+ return text
2434+ }
24272435 locator = this . _contextLocator ( locator )
24282436 let text
24292437 try {
24302438 text = await this . page . textContent ( locator )
24312439 } catch ( err ) {
24322440 if ( err . message . includes ( 'Timeout' ) || err . message . includes ( 'exceeded' ) ) {
2433- throw new Error ( `Element ${ new Locator ( locator ) . toString ( ) } was not found by text|CSS|XPath` )
2441+ throw new Error ( `Element ${ new Locator ( originalLocator ) . toString ( ) } was not found by text|CSS|XPath` )
24342442 }
24352443 throw err
24362444 }
@@ -3821,47 +3829,6 @@ class Playwright extends Helper {
38213829
38223830export default Playwright
38233831
3824- function buildLocatorString ( locator ) {
3825- if ( locator . isCustom ( ) ) {
3826- return `${ locator . type } =${ locator . value } `
3827- }
3828- if ( locator . isXPath ( ) ) {
3829- return `xpath=${ locator . value } `
3830- }
3831- return locator . simplify ( )
3832- }
3833-
3834- async function findElements ( matcher , locator ) {
3835- if ( locator . react ) return findReact ( matcher , locator )
3836- if ( locator . vue ) return findVue ( matcher , locator )
3837- if ( locator . pw ) return findByPlaywrightLocator . call ( this , matcher , locator )
3838- locator = new Locator ( locator , 'css' )
3839-
3840- return matcher . locator ( buildLocatorString ( locator ) ) . all ( )
3841- }
3842-
3843- async function findElement ( matcher , locator ) {
3844- if ( locator . react ) return findReact ( matcher , locator )
3845- if ( locator . vue ) return findVue ( matcher , locator )
3846- if ( locator . pw ) return findByPlaywrightLocator . call ( this , matcher , locator )
3847- locator = new Locator ( locator , 'css' )
3848-
3849- return matcher . locator ( buildLocatorString ( locator ) ) . first ( )
3850- }
3851-
3852- async function getVisibleElements ( elements ) {
3853- const visibleElements = [ ]
3854- for ( const element of elements ) {
3855- if ( await element . isVisible ( ) ) {
3856- visibleElements . push ( element )
3857- }
3858- }
3859- if ( visibleElements . length === 0 ) {
3860- return elements
3861- }
3862- return visibleElements
3863- }
3864-
38653832async function proceedClick ( locator , context = null , options = { } ) {
38663833 let matcher = await this . _getContext ( )
38673834 if ( context ) {
@@ -3901,13 +3868,28 @@ async function findClickable(matcher, locator) {
39013868 if ( locator . react ) return findReact ( matcher , locator )
39023869 if ( locator . vue ) return findVue ( matcher , locator )
39033870 if ( locator . pw ) return findByPlaywrightLocator . call ( this , matcher , locator )
3871+ if ( locator . role ) return findByRole ( matcher , locator )
39043872
39053873 locator = new Locator ( locator )
39063874 if ( ! locator . isFuzzy ( ) ) return findElements . call ( this , matcher , locator )
39073875
39083876 let els
39093877 const literal = xpathLocator . literal ( locator . value )
39103878
3879+ try {
3880+ els = await matcher . getByRole ( 'button' , { name : locator . value } ) . all ( )
3881+ if ( els . length ) return els
3882+ } catch ( err ) {
3883+ // getByRole not supported or failed
3884+ }
3885+
3886+ try {
3887+ els = await matcher . getByRole ( 'link' , { name : locator . value } ) . all ( )
3888+ if ( els . length ) return els
3889+ } catch ( err ) {
3890+ // getByRole not supported or failed
3891+ }
3892+
39113893 els = await findElements . call ( this , matcher , Locator . clickable . narrow ( literal ) )
39123894 if ( els . length ) return els
39133895
@@ -3960,6 +3942,10 @@ async function findCheckable(locator, context) {
39603942 contextEl = contextEl [ 0 ]
39613943 }
39623944
3945+ if ( typeof locator === 'object' && ( locator . role || locator . react || locator . vue || locator . pw ) ) {
3946+ return findElements . call ( this , contextEl , locator )
3947+ }
3948+
39633949 const matchedLocator = new Locator ( locator )
39643950 if ( ! matchedLocator . isFuzzy ( ) ) {
39653951 return findElements . call ( this , contextEl , matchedLocator . simplify ( ) )
@@ -3986,6 +3972,9 @@ async function proceedIsChecked(assertType, option) {
39863972}
39873973
39883974async function findFields ( locator ) {
3975+ if ( typeof locator === 'object' && ( locator . role || locator . react || locator . vue || locator . pw ) ) {
3976+ return this . _locate ( locator )
3977+ }
39893978 const matchedLocator = new Locator ( locator )
39903979 if ( ! matchedLocator . isFuzzy ( ) ) {
39913980 return this . _locate ( matchedLocator )
0 commit comments