import * as SUT from '../index' ; import { Browser , Page , chromium } from 'playwright' ; import * as path from 'path' ; describe ( 'query selector all in page' , ( ) : void => { let browser = undefined ; beforeEach ( ( ) : void => { } ) ; afterEach ( async ( ) : Promise < void > => { if ( browser ) { await browser . close ( ) ; } } ) ; test ( 'should return an error when page has not been initalized' , async ( ) : Promise < void > => { const page = undefined ; const expectedError = new Error ( "Cannot query selector 'foobar' because no browser has been launched" ) ; await SUT . querySelectorAllInPage ( 'foobar' , page ) . catch ( ( error ) : void => expect ( error ) . toMatchObject ( expectedError ) ) ; } ) ; test ( 'should return an empty array when selector is not found' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const context = await browser . newContext ( { viewport : null } ) ; const page = await context . newPage ( ) ; const result = await SUT . querySelectorAllInPage ( 'foobar' , page ) ; expect ( Array . isArray ( result ) ) . toBe ( true ) ; expect ( result . length ) . toBe ( 0 ) ; } ) ; test ( 'should return all found elements' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const context = await browser . newContext ( { viewport : null } ) ; const page = await context . newPage ( ) ; await page . goto ( `file:${ path . join ( __dirname , 'query-selector-all-in-page.test.html' ) }` ) ; const result = await SUT . querySelectorAllInPage ( '[role="row"]' , page ) ; expect ( result . length ) . toBe ( 3 ) ; expect ( ( await result [ 0 ] ! . getProperty ( 'innerText' ) ) ! . toString ( ) ) . toContain ( 'row1' ) ; expect ( ( await result [ 1 ] ! . getProperty ( 'innerText' ) ) ! . toString ( ) ) . toContain ( 'row2' ) ; expect ( ( await result [ 2 ] ! . getProperty ( 'innerText' ) ) ! . toString ( ) ) . toContain ( 'row3' ) ; } ) ; } ) ;