@@ -12,6 +12,9 @@ const { del } = require("request");
1212const { delete_archive } = require ( "../archive.js" ) ;
1313const poller = require ( "../poller/poller.js" ) ;
1414const builds = require ( "../poller/build" ) ;
15+ const batcher = require ( "./batcher.js" ) ;
16+ const reports = require ( "../../../commands/generate_reports.js" ) ;
17+ const { fail } = require ( "yargs" ) ;
1518
1619var batchCounter = 0 ;
1720var totalBatches = 0 ;
@@ -91,6 +94,7 @@ async function run(lt_config, batches, env) {
9194 . archive_project ( lt_config )
9295 . then ( function ( file_obj ) {
9396 project_file = file_obj [ "name" ] ;
97+ lt_config [ "run_settings" ] [ "project_file" ] = project_file ;
9498 //upload the project and get the project link
9599 uploader
96100 . upload_zip ( lt_config , file_obj [ "name" ] , "project" , env )
@@ -121,14 +125,15 @@ async function run(lt_config, batches, env) {
121125 access_key : lt_config [ "lambdatest_auth" ] [ "access_key" ] ,
122126 type : "cypress"
123127 } ) ;
124-
125128 run_test (
126129 payload ,
127130 env ,
128131 lt_config . run_settings . reject_unauthorized
129132 )
130133 . then ( function ( session_id ) {
131- delete_archive ( project_file ) ;
134+ if ( lt_config [ "run_settings" ] [ "retry_failed" ] == false ) {
135+ delete_archive ( project_file ) ;
136+ }
132137 delete_archive ( file_obj [ "name" ] ) ;
133138 //listen to control+c signal and stop tests
134139 process . on ( "SIGINT" , async ( ) => {
@@ -148,13 +153,48 @@ async function run(lt_config, batches, env) {
148153 } ) ;
149154 if (
150155 lt_config [ "run_settings" ] [ "sync" ] == true ||
151- ( lt_config [ "tunnel_settings" ] [ "tunnel" ] && lt_config [ "tunnel_settings" ] [ "autostart" ] )
156+ ( lt_config [ "tunnel_settings" ] [ "tunnel" ] && lt_config [ "tunnel_settings" ] [ "autostart" ] ) || ( lt_config [ "run_settings" ] [ "retry_failed" ] == true )
152157 ) {
153158 console . log ( "Waiting for build to finish..." ) ;
154- poller
155- . poll_build ( lt_config , session_id , env )
156- . then ( function ( exit_code ) {
157- resolve ( exit_code ) ;
159+ poller . update_status ( true ) ;
160+ poller . poll_build ( lt_config , session_id , env )
161+ . then ( function ( result ) {
162+ const { exit_code, build_info } = result ;
163+ if ( lt_config [ "run_settings" ] [ "retry_failed" ] == true && build_info != null ) {
164+ let failed_test_suites = [ ] ;
165+ for ( i = 0 ; i < build_info [ "data" ] . length ; i ++ ) {
166+ if ( build_info [ "data" ] [ i ] [ "status_ind" ] == "failed" ) {
167+ let failed_spec = findSpecFile ( lt_config [ "test_suite" ] , build_info [ "data" ] [ i ] )
168+ let failed_suite = {
169+ spec_file : failed_spec ,
170+ path : build_info [ "data" ] [ i ] [ "path" ] ,
171+ browser : build_info [ "data" ] [ i ] [ "browser" ] ,
172+ version : build_info [ "data" ] [ i ] [ "version" ] ,
173+ platform : build_info [ "data" ] [ i ] [ "platform" ]
174+ }
175+ failed_test_suites . push ( failed_suite ) ;
176+ }
177+ }
178+ if ( failed_test_suites . length > 0 ) {
179+ console . log ( "Retrying failed tests." )
180+ let batches = [ failed_test_suites ]
181+ retry_run ( lt_config , batches , env )
182+ . then ( function ( exit_code ) {
183+ if ( exit_code ) {
184+ console . log ( "retried failed tests ended with exit code " + exit_code ) ;
185+ }
186+ resolve ( exit_code ) ;
187+ } )
188+ . catch ( function ( error ) {
189+ console . log ( error ) ;
190+ resolve ( 1 ) ;
191+ } ) ;
192+ } else {
193+ resolve ( exit_code ) ;
194+ }
195+ } else {
196+ resolve ( exit_code ) ;
197+ }
158198 } )
159199 . catch ( function ( err ) {
160200 console . log (
@@ -193,6 +233,97 @@ async function run(lt_config, batches, env) {
193233 } ) ;
194234}
195235
236+ async function retry_run ( lt_config , batches , env ) {
237+ totalBatches = batches . length ;
238+ return new Promise ( function ( resolve , reject ) {
239+ lt_config [ "test_suite" ] = batches [ 0 ] ;
240+ archive
241+ . archive_batch ( lt_config , batches [ 0 ] , env )
242+ . then ( async function ( file_obj ) {
243+ uploader
244+ . upload_zip ( lt_config , file_obj [ "name" ] , "tests" , env )
245+ . then ( async function ( resp ) {
246+
247+ var payload = JSON . stringify ( {
248+ payload : {
249+ test_file : resp [ "value" ] [ "message" ] . split ( "?" ) [ 0 ] ,
250+ } ,
251+ username : lt_config [ "lambdatest_auth" ] [ "username" ] ,
252+ access_key : lt_config [ "lambdatest_auth" ] [ "access_key" ] ,
253+ type : "cypress"
254+ } ) ;
255+
256+ run_test (
257+ payload ,
258+ env ,
259+ lt_config . run_settings . reject_unauthorized
260+ ) . then ( function ( session_id ) {
261+
262+ delete_archive ( lt_config [ "run_settings" ] [ "project_file" ] ) ;
263+ delete_archive ( file_obj [ "name" ] ) ;
264+
265+ process . on ( "SIGINT" , async ( ) => {
266+ try {
267+ console . log (
268+ "Retry - Control+c signal received.\nTrying to Terminate the processes"
269+ ) ;
270+ await builds . stop_cypress_session (
271+ lt_config ,
272+ session_id ,
273+ env
274+ ) ;
275+ resolve ( 0 ) ;
276+ } catch ( e ) {
277+ console . log ( "Retry - Could not exit process. Try Again!!!" ) ;
278+ }
279+ } ) ;
280+ if (
281+ lt_config [ "run_settings" ] [ "sync" ] == true ||
282+ ( lt_config [ "tunnel_settings" ] [ "tunnel" ] && lt_config [ "tunnel_settings" ] [ "autostart" ] )
283+ ) {
284+ console . log ( "Retry - Waiting for build to finish..." ) ;
285+ poller . update_status ( true ) ;
286+ poller . poll_build ( lt_config , session_id , env )
287+ . then ( function ( result ) {
288+ const { exit_code, build_json } = result ;
289+ resolve ( exit_code ) ;
290+ } )
291+ . catch ( function ( err ) {
292+ console . log (
293+ "Retry - Some error occured in getting build updates" ,
294+ err . message
295+ ) ;
296+ } ) ;
297+ } else {
298+ resolve ( 0 ) ;
299+ }
300+
301+ } )
302+ . catch ( function ( err ) {
303+ console . log ( "Retry - Error occured while creating tests" , err ) ;
304+ } ) ;
305+
306+
307+ } )
308+ . catch ( function ( err ) {
309+ console . log ( "Retry - Not able to archive the batch of test files" , err ) ;
310+ } ) ;
311+
312+ } )
313+ . catch ( function ( err ) {
314+ console . log ( "Retry - Unable to archive the project" ) ;
315+ console . log ( err ) ;
316+ reject ( err ) ;
317+ } ) ;
318+ } ) ;
319+ }
320+
321+ function findSpecFile ( testSuite , buildInfoData ) {
322+ const foundTest = testSuite . find ( ( test ) => test . path === buildInfoData . path ) ;
323+ return foundTest ? foundTest . spec_file : null ;
324+ }
325+
196326module . exports = {
197327 run_batches : run ,
328+ run_batches_retry : retry_run ,
198329} ;
0 commit comments