@@ -50,8 +50,9 @@ const { createValueEngine, createDisabledEngine } = require('./extras/Playwright
50
50
const {
51
51
seeElementError, dontSeeElementError, dontSeeElementInDOMError, seeElementInDOMError,
52
52
} = require ( './errors/ElementAssertion' ) ;
53
- const { createAdvancedTestResults, allParameterValuePairsMatchExtreme, extractQueryObjects } = require ( './networkTraffics/utils' ) ;
54
- const { log } = require ( '../output' ) ;
53
+ const {
54
+ dontSeeTraffic, seeTraffic, grabRecordedNetworkTraffics, stopRecordingTraffic, flushNetworkTraffics,
55
+ } = require ( './network/actions' ) ;
55
56
56
57
const pathSeparator = path . sep ;
57
58
@@ -3010,37 +3011,6 @@ class Playwright extends Helper {
3010
3011
} ) ;
3011
3012
}
3012
3013
3013
- /**
3014
- * {{> grabRecordedNetworkTraffics }}
3015
- */
3016
- async grabRecordedNetworkTraffics ( ) {
3017
- if ( ! this . recording || ! this . recordedAtLeastOnce ) {
3018
- throw new Error ( 'Failure in test automation. You use "I.grabRecordedNetworkTraffics", but "I.startRecordingTraffic" was never called before.' ) ;
3019
- }
3020
-
3021
- const promises = this . requests . map ( async ( request ) => {
3022
- const resp = await request . response ;
3023
- let body ;
3024
- try {
3025
- // There's no 'body' for some requests (redirect etc...)
3026
- body = JSON . parse ( ( await resp . body ( ) ) . toString ( ) ) ;
3027
- } catch ( e ) {
3028
- // only interested in JSON, not HTML responses.
3029
- }
3030
-
3031
- return {
3032
- url : resp . url ( ) ,
3033
- response : {
3034
- status : resp . status ( ) ,
3035
- statusText : resp . statusText ( ) ,
3036
- body,
3037
- } ,
3038
- } ;
3039
- } ) ;
3040
-
3041
- return Promise . all ( promises ) ;
3042
- }
3043
-
3044
3014
/**
3045
3015
* Blocks traffic of a given URL or a list of URLs.
3046
3016
*
@@ -3120,67 +3090,19 @@ class Playwright extends Helper {
3120
3090
}
3121
3091
3122
3092
/**
3093
+ *
3123
3094
* {{> flushNetworkTraffics }}
3124
3095
*/
3125
3096
flushNetworkTraffics ( ) {
3126
- this . requests = [ ] ;
3097
+ flushNetworkTraffics . call ( this ) ;
3127
3098
}
3128
3099
3129
3100
/**
3101
+ *
3130
3102
* {{> stopRecordingTraffic }}
3131
3103
*/
3132
3104
stopRecordingTraffic ( ) {
3133
- this . page . removeAllListeners ( 'request' ) ;
3134
- this . recording = false ;
3135
- }
3136
-
3137
- /**
3138
- * {{> seeTraffic }}
3139
- */
3140
- async seeTraffic ( {
3141
- name, url, parameters, requestPostData, timeout = 10 ,
3142
- } ) {
3143
- if ( ! name ) {
3144
- throw new Error ( 'Missing required key "name" in object given to "I.seeTraffic".' ) ;
3145
- }
3146
-
3147
- if ( ! url ) {
3148
- throw new Error ( 'Missing required key "url" in object given to "I.seeTraffic".' ) ;
3149
- }
3150
-
3151
- if ( ! this . recording || ! this . recordedAtLeastOnce ) {
3152
- throw new Error ( 'Failure in test automation. You use "I.seeTraffic", but "I.startRecordingTraffic" was never called before.' ) ;
3153
- }
3154
-
3155
- for ( let i = 0 ; i <= timeout * 2 ; i ++ ) {
3156
- const found = this . _isInTraffic ( url , parameters ) ;
3157
- if ( found ) {
3158
- return true ;
3159
- }
3160
- await new Promise ( ( done ) => {
3161
- setTimeout ( done , 1000 ) ;
3162
- } ) ;
3163
- }
3164
-
3165
- // check request post data
3166
- if ( requestPostData && this . _isInTraffic ( url ) ) {
3167
- const advancedTestResults = createAdvancedTestResults ( url , requestPostData , this . requests ) ;
3168
-
3169
- assert . equal ( advancedTestResults , true , `Traffic named "${ name } " found correct URL ${ url } , BUT the post data did not match:\n ${ advancedTestResults } ` ) ;
3170
- } else if ( parameters && this . _isInTraffic ( url ) ) {
3171
- const advancedTestResults = createAdvancedTestResults ( url , parameters , this . requests ) ;
3172
-
3173
- assert . fail (
3174
- `Traffic named "${ name } " found correct URL ${ url } , BUT the query parameters did not match:\n`
3175
- + `${ advancedTestResults } ` ,
3176
- ) ;
3177
- } else {
3178
- assert . fail (
3179
- `Traffic named "${ name } " not found in recorded traffic within ${ timeout } seconds.\n`
3180
- + `Expected url: ${ url } .\n`
3181
- + `Recorded traffic:\n${ this . _getTrafficDump ( ) } ` ,
3182
- ) ;
3183
- }
3105
+ stopRecordingTraffic . call ( this ) ;
3184
3106
}
3185
3107
3186
3108
/**
@@ -3217,73 +3139,30 @@ class Playwright extends Helper {
3217
3139
}
3218
3140
3219
3141
/**
3220
- * {{> dontSeeTraffic }}
3221
3142
*
3143
+ * {{> grabRecordedNetworkTraffics }}
3222
3144
*/
3223
- dontSeeTraffic ( { name, url } ) {
3224
- if ( ! this . recordedAtLeastOnce ) {
3225
- throw new Error ( 'Failure in test automation. You use "I.dontSeeTraffic", but "I.startRecordingTraffic" was never called before.' ) ;
3226
- }
3227
-
3228
- if ( ! name ) {
3229
- throw new Error ( 'Missing required key "name" in object given to "I.dontSeeTraffic".' ) ;
3230
- }
3231
-
3232
- if ( ! url ) {
3233
- throw new Error ( 'Missing required key "url" in object given to "I.dontSeeTraffic".' ) ;
3234
- }
3235
-
3236
- if ( this . _isInTraffic ( url ) ) {
3237
- assert . fail ( `Traffic with name "${ name } " (URL: "${ url } ') found, but was not expected to be found.` ) ;
3238
- }
3145
+ async grabRecordedNetworkTraffics ( ) {
3146
+ return grabRecordedNetworkTraffics . call ( this ) ;
3239
3147
}
3240
3148
3241
3149
/**
3242
- * Checks if URL with parameters is part of network traffic. Returns true or false. Internal method for this helper.
3243
3150
*
3244
- * @param url URL to look for.
3245
- * @param [parameters] Parameters that this URL needs to contain
3246
- * @return {boolean } Whether or not URL with parameters is part of network traffic.
3247
- * @private
3151
+ * {{> seeTraffic }}
3248
3152
*/
3249
- _isInTraffic ( url , parameters ) {
3250
- let isInTraffic = false ;
3251
- this . requests . forEach ( ( request ) => {
3252
- if ( isInTraffic ) {
3253
- return ; // We already found traffic. Continue with next request
3254
- }
3255
-
3256
- if ( ! request . url . match ( new RegExp ( url ) ) ) {
3257
- return ; // url not found in this request. continue with next request
3258
- }
3259
-
3260
- // URL has matched. Now we check the parameters
3261
-
3262
- if ( parameters ) {
3263
- const advancedReport = allParameterValuePairsMatchExtreme ( extractQueryObjects ( request . url ) , parameters ) ;
3264
- if ( advancedReport === true ) {
3265
- isInTraffic = true ;
3266
- }
3267
- } else {
3268
- isInTraffic = true ;
3269
- }
3270
- } ) ;
3271
-
3272
- return isInTraffic ;
3153
+ async seeTraffic ( {
3154
+ name, url, parameters, requestPostData, timeout = 10 ,
3155
+ } ) {
3156
+ await seeTraffic . call ( this , ...arguments ) ;
3273
3157
}
3274
3158
3275
3159
/**
3276
- * Returns all URLs of all network requests recorded so far during execution of test scenario.
3277
3160
*
3278
- * @return { string } List of URLs recorded as a string, separated by new lines after each URL
3279
- * @private
3161
+ * {{> dontSeeTraffic }}
3162
+ *
3280
3163
*/
3281
- _getTrafficDump ( ) {
3282
- let dumpedTraffic = '' ;
3283
- this . requests . forEach ( ( request ) => {
3284
- dumpedTraffic += `${ request . method } - ${ request . url } \n` ;
3285
- } ) ;
3286
- return dumpedTraffic ;
3164
+ dontSeeTraffic ( { name, url } ) {
3165
+ dontSeeTraffic . call ( this , ...arguments ) ;
3287
3166
}
3288
3167
3289
3168
/**
0 commit comments