@@ -5,6 +5,7 @@ import { logger } from "./utils/logger";
55import { NetworkConditions } from './interfaces/networkConditions' ;
66import { Har } from 'har-format' ;
77import { CDPSession } from './cdpSession' ;
8+ import CDP from 'chrome-remote-interface' ;
89
910// event types to observe
1011const observe = [
@@ -76,9 +77,11 @@ export const NETWORK_PRESETS = {
7677export class Network extends TraceOperations {
7778 private _traceFileName : string ;
7879 protected _events : any [ ] = [ ] ;
80+ private _client : CDP . Client ;
7981
80- constructor ( traceFileName : string = '' ) {
82+ constructor ( cdpSession : CDPSession , traceFileName : string = '' ) {
8183 super ( ) ;
84+ this . _client = cdpSession . client ;
8285 this . _traceFileName = traceFileName ;
8386 }
8487
@@ -87,11 +90,11 @@ export class Network extends TraceOperations {
8790 */
8891 public async startTrace ( ) : Promise < void > {
8992 try {
90- if ( CDPSession . client ) {
91- await CDPSession . client . send ( 'Page.enable' ) ;
92- await CDPSession . client . send ( 'Network.enable' ) ;
93+ if ( this . _client ) {
94+ await this . _client . send ( 'Page.enable' ) ;
95+ await this . _client . send ( 'Network.enable' ) ;
9396 observe . forEach ( method => {
94- CDPSession . client . on ( method , params => {
97+ this . _client . on ( method , params => {
9598 this . _events . push ( { method, params } ) ;
9699 } ) ;
97100 } ) ;
@@ -108,7 +111,7 @@ export class Network extends TraceOperations {
108111 */
109112 public async stopTrace ( ) : Promise < Har > {
110113 try {
111- if ( CDPSession . client ) {
114+ if ( this . _client ) {
112115 const har = await harFromMessages ( this . _events , { includeTextFromResponseBody : true } ) ;
113116 if ( this . _traceFileName ) {
114117 fs . writeFileSync ( this . _traceFileName , JSON . stringify ( har ) ) ;
@@ -119,8 +122,8 @@ export class Network extends TraceOperations {
119122 logger . error ( e ) ;
120123 throw e ;
121124 } finally {
122- await CDPSession . client . send ( 'Page.disable' ) ;
123- await CDPSession . client . send ( 'Network.disable' ) ;
125+ await this . _client . send ( 'Page.disable' ) ;
126+ await this . _client . send ( 'Network.disable' ) ;
124127 }
125128 return { log : { entries : [ ] , version : '' , creator : { name : '' , version : '' } } }
126129 }
@@ -131,8 +134,8 @@ export class Network extends TraceOperations {
131134 */
132135 public async emulateNetworkConditions ( networkConditions : NetworkConditions ) : Promise < void > {
133136 try {
134- if ( CDPSession . client ) {
135- await CDPSession . client . send ( 'Network.emulateNetworkConditions' , networkConditions ) ;
137+ if ( this . _client ) {
138+ await this . _client . send ( 'Network.emulateNetworkConditions' , networkConditions ) ;
136139 }
137140 } catch ( e ) {
138141 logger . error ( e ) ;
0 commit comments