import playwright from 'playwright' ; import { PlaywrightBrowser , TestScreenshots } from '../types' ; import { log , pngBufferToDataURL } from '../util' ; export default async ( browser , address , size = { width : 800 , height : 600 } , picoOptions ? ) : Promise < TestScreenshots > => { log ( 'Creating browser context' ) ; const context = await browser . newContext ( { viewport : { ... size } } ) ; log ( 'Creating context page' ) ; const page = await context . newPage ( ) ; page . on ( 'console' , ( msg ) => console . log ( '[page console]' , msg . text ( ) || msg . args ( ) . map ( jsHandle => jsHandle . toString ( ) ) . join ( ' ' ) ) ) ; page . on ( 'pageerror' , ( error ) => { console . error ( '[!] [page error]' , error ) ; } ) ; page . on ( 'requestfailed' , ( request ) => { console . error ( '[!] [request failed]' , 'url:' , request . url ( ) , 'reason:' , request . failure ( ) ?. errorText ) ; } ) ; log ( 'Browsing to target page' ) ; await page . goto ( address ) ; log ( 'Taking expected screenshot' ) ; const expected = pngBufferToDataURL ( await page . screenshot ( ) ) ; log ( 'Taking actual screenshot' ) ; const { actual , errors } = await page . evaluate ( opts => Pico . dataURL ( window , opts ) . then ( ( { value , errors } ) => ( { actual : value , errors : errors . map ( e => e . reason ) } ) ) , picoOptions ) ; log ( 'Closing page' ) ; await page . close ( ) ; log ( 'Test run done' ) ; return { actual , errors , expected } ; } ;