import * as SUT from '../index' ; import { defaultVerboseOptions } from '../is-handle-visible' ; import { sleep , waitUntil } from '../../../../utils' ; import { Browser , chromium } from 'playwright' ; import * as path from 'path' ; describe ( 'handle is visible' , ( ) : void => { let browser = undefined ; beforeEach ( ( ) : void => { } ) ; afterEach ( async ( ) : Promise < void > => { if ( browser ) { await browser . close ( ) ; } } ) ; test ( 'should return false when selector is hidden - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; const url = `file:${ path . join ( __dirname , 'is-handle-visible.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const handle = await page . $ ( '#hidden' ) ; const result = await SUT . isHandleVisible ( handle , defaultVerboseOptions ) ; expect ( handle ) . toBeDefined ( ) ; expect ( result ) . toBe ( false ) ; } ) ; test ( 'should return true when selector is visible in the current viewport' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; const url = `file:${ path . join ( __dirname , 'is-handle-visible.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const handle = await page . $ ( '#visible' ) ; const result = await SUT . isHandleVisible ( handle , defaultVerboseOptions ) ; expect ( handle ) . toBeDefined ( ) ; expect ( result ) . toBe ( true ) ; } ) ; test ( 'should return false when selector is transparent' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; const url = `file:${ path . join ( __dirname , 'is-handle-visible.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const handle = await page . $ ( '#transparent' ) ; const result = await SUT . isHandleVisible ( handle , defaultVerboseOptions ) ; expect ( handle ) . toBeDefined ( ) ; expect ( result ) . toBe ( false ) ; } ) ; test ( 'should return false when selector is out of screen' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; const url = `file:${ path . join ( __dirname , 'is-handle-visible.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const handle = await page . $ ( '#out-of-screen' ) ; const result = await SUT . isHandleVisible ( handle , defaultVerboseOptions ) ; expect ( handle ) . toBeDefined ( ) ; expect ( result ) . toBe ( false ) ; } ) ; test ( 'should return true when selector is visible but out of viewport' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; const url = `file:${ path . join ( __dirname , 'is-handle-visible.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const handle = await page . $ ( '#out-of-viewport' ) ; const result = await SUT . isHandleVisible ( handle , defaultVerboseOptions ) ; expect ( handle ) . toBeDefined ( ) ; expect ( result ) . toBe ( true ) ; } ) ; test ( 'should return false when selector is visible then removed from DOM' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; const url = `file:${ path . join ( __dirname , 'is-handle-visible.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const handle = await page . $ ( '#visible-then-removed' ) ; const result = await SUT . isHandleVisible ( handle , defaultVerboseOptions ) ; expect ( handle ) . toBeDefined ( ) ; expect ( result ) . toBe ( true ) ; await waitUntil ( async ( ) => ! ( await SUT . isHandleVisible ( handle , defaultVerboseOptions ) ) , 'yo' , { stabilityInMilliseconds : 0 , throwOnTimeout : false , timeoutInMilliseconds : 6000 , verbose : false , wrapPredicateExecutionInsideTryCatch : false , } ) ; const resultWhenRemovedFromDom = await SUT . isHandleVisible ( handle , defaultVerboseOptions ) ; expect ( resultWhenRemovedFromDom ) . toBe ( false ) ; } ) ; } ) ;