import * as SUT from '../../playwright-fluent' ; import * as path from 'path' ; describe ( 'Playwright Fluent - onRequestTo(url).respondFromHar()' , ( ) : void => { let p ; beforeEach ( ( ) : void => { p = new SUT . PlaywrightFluent ( ) ; } ) ; afterEach ( async ( ) : Promise < void > => { await p . close ( ) ; } ) ; test ( 'should load github web site from HAR file' , async ( ) : Promise < void > => { const harFile = path . join ( __dirname , 'github.com.har' ) ; const foundUrls = [ ] ; const notFoundUrls = [ ] ; const options = { onHarEntryNotFound : ( _allEntries , requestedUrl , requestedMethod ) => { notFoundUrls . push ( `not found url : ${ requestedMethod } '${ requestedUrl }'` ) ; } , onHarEntryFound : ( _foundEntry , requestedUrl , requestedMethod ) => { foundUrls . push ( `found url : ${ requestedMethod } '${ requestedUrl }'` ) ; } , filterHarEntryByPostData : ( requestPostData , harRequestPostData ) => { if ( requestPostData === harRequestPostData . text ) { return true ; } return false ; } , provideEntryOnHarEntryNotFound : ( request , allEntries ) => { const requestedUrl = request . url ( ) ; if ( requestedUrl . includes ( '/browser/stats' ) ) { console . log ( `manually select entry for POST ${ requestedUrl }` ) ; return ( allEntries . filter ( ( entry ) => entry . request . method === 'POST' ) . filter ( ( entry ) => entry . request . url === requestedUrl ) . pop ( ) || null ) ; } if ( requestedUrl . startsWith ( 'https://collector.githubapp.com/github/page_view?dimensions' ) ) { console . log ( `manually select entry for GET ${ requestedUrl }` ) ; return ( allEntries . filter ( ( entry ) => entry . request . method === 'GET' ) . filter ( ( entry ) => entry . request . url . startsWith ( 'https://collector.githubapp.com/github/page_view?dimensions' ) ) . pop ( ) || null ) ; } return null ; } , } ; await p . withBrowser ( 'chromium' ) . withOptions ( { headless : false } ) . withCursor ( ) . onRequestTo ( '/' ) . respondFromHar ( [ harFile ] , options ) ; await p . navigateTo ( 'https://github.com/' ) ; const signupButton = p . selector ( 'button' ) . withText ( 'Sign up for GitHub' ) ; await p . expectThat ( signupButton ) . isEnabled ( ) ; console . log ( foundUrls . join ( '\n' ) ) ; console . log ( notFoundUrls . join ( '\n' ) ) ; } ) ; } ) ;