import * as SUT from '.' ; import { Page , Browser , chromium } from 'playwright' ; describe ( 'exists' , ( ) : 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 check that 'foobar' exists because no browser has been launched" ) ; await SUT . exists ( 'foobar' , page ) . catch ( ( error ) : void => expect ( error ) . toMatchObject ( expectedError ) ) ; } ) ; test ( 'should return true when selector exists on the page - chromium' , 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 . exists ( 'body' , page ) ; expect ( result ) . toBe ( true ) ; } ) ; test ( 'should return false when selector does not exist on the page - chromium' , 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 . exists ( 'foobar' , page ) ; expect ( result ) . toBe ( false ) ; } ) ; test ( 'should return false when playright API throws an internal error' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const context = await browser . newContext ( { viewport : null } ) ; const page = await context . newPage ( ) ; page . $ = ( ) => { throw new Error ( 'internal error!' ) ; } ; const result = await SUT . exists ( 'body' , page ) ; expect ( result ) . toBe ( false ) ; } ) ; } ) ;