forked from Qarj/WebImblaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebinject.pl
executable file
·3373 lines (2712 loc) · 142 KB
/
webinject.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# $Id$
# $Revision$
# $Date$
use strict;
use warnings;
use vars qw/ $VERSION /;
$VERSION = '2.6.0';
#removed the -w parameter from the first line so that warnings will not be displayed for code in the packages
# Copyright 2004-2006 Corey Goldberg ([email protected])
# Extensive updates 2015-2016 Tim Buckland
#
# This file is part of WebInject.
#
# WebInject is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# WebInject is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose. See the
# GNU General Public License for more details.
use File::Basename;
use File::Spec;
use File::Slurp;
use LWP;
use HTTP::Request::Common;
use XML::Simple;
use Time::HiRes 'time','sleep';
use Getopt::Long;
local $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 'false';
use File::Copy qw(copy), qw(move);
use File::Path qw(make_path remove_tree);
local $| = 1; #don't buffer output to STDOUT
## Variable declarations
our ($request, $response);
my ($useragent);
our ($latency, $verification_latency, $screenshot_latency);
my $timestamp; ## for {TIMESTAMP} - global so all substitutions in a test step have same timestamp
my $testfilename; ## for {TESTFILENAME} - file name only, without .xml extension
my ($current_date_time, $total_run_time, $start_timer, $end_timer);
my ($total_response, $avg_response, $max_response, $min_response);
my (%test_step_time); ## record in a hash the latency for every step for later use
my ($start_time); ## to store a copy of $start_run_timer in a global variable
my ($cookie_jar, @http_auth);
our ($case_failed_count, $passed_count, $failed_count);
our ($is_failure);
my ($run_count, $total_run_count, $case_passed_count);
my ($current_case_file, $current_case_filename, $case_count, $fast_fail_invoked);
our (%case);
my (%case_save);
my (%parsedresult, %varvar);
my (%config);
our ($opt_proxy);
our ($opt_driver, $opt_chromedriver_binary, $opt_selenium_binary, $opt_publish_full);
my ($opt_configfile, $opt_version, $opt_output, $opt_autocontroller);
my ($opt_ignoreretry, $opt_no_output, $opt_verbose, $opt_help);
my ($report_type); ## 'standard' and 'nagios' supported
my ($return_message); ## error message to return to nagios
my $testnum;
our $testnum_display; ## individual step file html logging
my ($previous_test_step, $delayed_file_full, $delayed_html); ## individual step file html logging
our ($retry_passed_count, $retry_failed_count, $retries_print, $jumpbacks_print); ## retry failed tests
my ($retry, $retries, $globalretries, $jumpbacks, $auto_retry, $checkpoint);
my $attempts_since_last_success = 0;
my ($sanity_result); ## if a sanity check fails, execution will stop (as soon as all retries are exhausted on the current test case)
my ($xml_test_cases, $step_index, @test_steps);
our ($output, $output_folder); ## output path including possible filename prefix, output path without filename prefix, output prefix only
my ($output_prefix); ## output path including possible filename prefix, output path without filename prefix, output prefix only
my ($outsum); ## outsum is a checksum calculated on the output directory name. Used to help guarantee test data uniqueness where two WebInject processes are running in parallel.
my ($user_config); ## support arbirtary user defined config
my ($convert_back_ports, $convert_back_ports_null); ## turn {:4040} into :4040 or null
my $total_assertion_skips = 0;
my (@visited_pages); ## page source of previously visited pages
my (@visited_page_names); ## page name of previously visited pages
my (@page_update_times); ## last time the page was updated in the cache
my $assertion_skips = 0;
my $assertion_skips_message = q{}; ## support tagging an assertion as disabled with a message
my (@hrefs, @srcs, @bg_images); ## keep an array of all grabbed assets to substitute them into the step results html (for results visualisation)
my $session_started; ## only start up http sesion if http is being used
my ($testfile_contains_selenium); ## so we know if Selenium Browser Session needs to be started
## put the current date and time into variables - startdatetime - for recording the start time in a format an xsl stylesheet can process
my @MONTHS = qw(01 02 03 04 05 06 07 08 09 10 11 12);
my @MONTHS_TEXT = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @WEEKDAYS = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
my ($SECOND, $MINUTE, $HOUR, $DAYOFMONTH, $MONTH, $YEAROFFSET, $DAYOFWEEK, $DAYOFYEAR, $DAYLIGHTSAVINGS) = localtime;
my $YEAR = 1900 + $YEAROFFSET;
my $YY = substr $YEAR, 2; #year as 2 digits
my $MONTH_TEXT = $MONTHS_TEXT[$MONTH];
$DAYOFMONTH = sprintf '%02d', $DAYOFMONTH;
my $WEEKOFMONTH = int(($DAYOFMONTH-1)/7)+1;
my $STARTDATE = "$YEAR-$MONTHS[$MONTH]-$DAYOFMONTH";
$MINUTE = sprintf '%02d', $MINUTE; #put in up to 2 leading zeros
$SECOND = sprintf '%02d', $SECOND;
$HOUR = sprintf '%02d', $HOUR;
my $TIMESECONDS = ($HOUR * 60 * 60) + ($MINUTE * 60) + $SECOND;
$current_date_time = "$WEEKDAYS[$DAYOFWEEK] $DAYOFMONTH $MONTH_TEXT $YEAR, $HOUR:$MINUTE:$SECOND";
my $this_script_folder_full = dirname(__FILE__);
chdir $this_script_folder_full;
my $counter = 0; ## keeping track of the loop we are up to
my $concurrency = 'null'; ## current working directory - not full path
our ($results_stdout, $results_html, $results_xml);
my ($results_xml_file_name);
my ($start_run_timer, $end_run_timer, $repeat, $start);
my $hostname = `hostname`; ##no critic(ProhibitBacktickOperators) ## hostname should work on Linux and Windows
$hostname =~ s/\r|\n//g; ## strip out any rogue linefeeds or carriage returns
our $is_windows = $^O eq 'MSWin32' ? 1 : 0;
## Startup
get_options(); #get command line options
process_config_file();
write_initial_stdout(); #write opening tags for STDOUT.
_whack($opt_publish_full.'http.txt');
_whack($opt_publish_full.'results.html');
write_initial_xml();
write_initial_html(); #write opening tags for results file
$total_run_count = 0;
$case_passed_count = 0;
$case_failed_count = 0;
$passed_count = 0;
$failed_count = 0;
$total_response = 0;
$avg_response = 0;
$max_response = 0;
$min_response = 10_000_000; #set to large value so first minresponse will be less
$globalretries=0; ## total number of retries for this run across all test cases
$start_run_timer = time; #timer for entire test run
$start_time = $start_run_timer; ## need a global variable to make a copy of the start run timer
$current_case_filename = basename($current_case_file); ## with extension
$testfilename = fileparse($current_case_file, '.xml'); ## without extension
read_test_case_file();
## only import WebInjectSelenium.pm package if test case file contains "selenium"
my $module_to_import = $testfile_contains_selenium ? "plugins::WebInjectSelenium" : "plugins::WebInjectSeleniumDummy";
my $file_to_require = $module_to_import;
$file_to_require =~ s[::][/]g;
$file_to_require .= '.pm';
require $file_to_require;
$module_to_import->import;
$repeat = $xml_test_cases->{repeat}; #grab the number of times to iterate test case file
if (!$repeat) { $repeat = 1; } #set to 1 in case it is not defined in test case file
$start = $xml_test_cases->{start}; #grab the start for repeating (for restart)
if (!$start) { $start = 1; } #set to 1 in case it is not defined in test case file
$counter = $start - 1; #so starting position and counter are aligned
$results_stdout .= "-------------------------------------------------------\n";
## Repeat Loop
foreach ($start .. $repeat) {
$counter = $counter + 1;
$run_count = 0;
$jumpbacks_print = q{}; ## we do not indicate a jump back until we actually jump back
$jumpbacks = 0;
@test_steps = sort {$a<=>$b} keys %{$xml_test_cases->{case}};
my $numsteps = scalar @test_steps;
## Loop over each of the test cases (test steps) with C Style for loop (due to need to update $step_index in a non standard fashion)
TESTCASE: for ($step_index = 0; $step_index < $numsteps; $step_index++) { ## no critic(ProhibitCStyleForLoops)
$testnum = $test_steps[$step_index];
$testnum_display = get_testnum_display($testnum, $counter);
$is_failure = 0;
$retries = 1; ## we increment retries after writing to the log
$retries_print = q{}; ## the printable value is used before writing the results to the log, so it is one behind, 0 being printed as null
my $skip_message = get_test_step_skip_message();
if ( $skip_message ) {
$results_stdout .= "Skipping Test Case $testnum... ($skip_message)\n";
$results_stdout .= qq|------------------------------------------------------- \n|;
next TESTCASE; ## skip running this test step
}
# populate variables with values from testcase file, do substitutions, and revert converted values back
substitute_variables();
$retry = get_number_of_times_to_retry_this_test_step(); # 0 means do not retry this step
do ## retry loop
{
substitute_retry_variables(); ## for each retry, there are a few substitutions that we need to redo - like the retry number
set_var_variables(); ## finally set any variables after doing all the static and dynamic substitutions
substitute_var_variables();
set_retry_to_zero_if_global_limit_exceeded();
$is_failure = 0;
$fast_fail_invoked = ''; # false
$retry_passed_count = 0;
$retry_failed_count = 0;
if ($case{description1} and $case{description1} =~ /dummy test case/) { ## if we hit the dummy record, skip it (this is a hack for test case files with only one step)
next;
}
check_for_checkpoint();
output_test_step_description();
output_assertions();
execute_test_step();
display_request_response();
decode_smtp();
decode_quoted_printable();
verify(); #verify result from http response
gethrefs(); ## get specified web page href assets
getsrcs(); ## get specified web page src assets
getbackgroundimages(); ## get specified web page src assets
parseresponse(); #grab string from response to send later
httplog(); #write to http.txt file
pass_fail_or_retry();
output_test_step_latency();
increment_run_count();
update_latency_statistics();
restart_browser();
sleep_before_next_step();
output_test_step_results();
$retry = $retry - 1;
} ## end of retry loop
until ($retry < 0); ## no critic(ProhibitNegativeExpressionsInUnlessAndUntilConditions])
if ($case{sanitycheck} && ($case_failed_count > 0)) { ## if sanitycheck fails (i.e. we have had any error at all after retries exhausted), then execution is aborted
$results_stdout .= qq|SANITY CHECK FAILED ... Aborting \n|;
last;
}
} ## end of test case loop
$testnum = 1; #reset testcase counter so it will reprocess test case file if repeat is set
} ## end of repeat loop
final_tasks(); #do return/cleanup tasks
my $status = $case_failed_count cmp 0;
exit $status;
## End main code
#------------------------------------------------------------------
# SUBROUTINES
#------------------------------------------------------------------
sub display_request_response {
if (not $opt_verbose) { return; }
$results_stdout .= "\n\nREQUEST ===>\n".$request->as_string."\n<=== END REQUEST\n\n";
$results_stdout .= "\n\nRESPONSE ===>\n".$response->as_string."\n<=== END RESPONSE\n\n";
return;
}
sub get_testnum_display {
my ($_testnum, $_counter) = @_;
## use $testnum_display for all testnum output, add 10,000 in case of repeat loop
my $_testnum_display = $_testnum + ($_counter*10_000) - 10_000;
$_testnum_display = sprintf '%.2f', $_testnum_display; ## maximul of 2 decimal places
$_testnum_display =~ s/0+\z// if $_testnum_display =~ /[.]/; ## remove trailing non significant zeros
if (not ($_testnum_display =~ s/[.]\z//) ) { ## remove decimal point if nothing after
$_testnum_display = sprintf '%.2f', $_testnum_display; ## put back the non significant zero if we have a decimal point
}
return $_testnum_display;
}
#------------------------------------------------------------------
sub set_useragent {
my ($_useragent) = @_;
if (not $_useragent) { return; }
$useragent->agent($_useragent);
return;
}
#------------------------------------------------------------------
sub set_max_redirect {
my ($_max_redirect) = @_;
if (not $_max_redirect) { return; }
$useragent->max_redirect($_max_redirect);
return;
}
#------------------------------------------------------------------
sub get_test_step_skip_message {
$case{runon} = $xml_test_cases->{case}->{$testnum}->{runon}; ## skip test cases not flagged for this environment
if ($case{runon}) { ## is this test step conditional on the target environment?
if ( _run_this_step($case{runon}) ) {
## run this test case as normal since it is allowed
}
else {
return "run on $case{runon}";
}
}
$case{donotrunon} = $xml_test_cases->{case}->{$testnum}->{donotrunon}; ## skip test cases flagged not to run on this environment
if ($case{donotrunon}) { ## is this test step conditional on the target environment?
if ( not _run_this_step($case{donotrunon}) ) {
## run this test case as normal since it is allowed
}
else {
return "do not run on $case{donotrunon}";
}
}
$case{autocontrolleronly} = $xml_test_cases->{case}->{$testnum}->{autocontrolleronly}; ## only run this test case on the automation controller, e.g. test case may involve a test virus which cannot be run on a regular corporate desktop
if ($case{autocontrolleronly}) { ## is the autocontrolleronly value set for this testcase?
if ($opt_autocontroller) { ## if so, was the auto controller option specified?
## run this test case as normal since it is allowed
}
else {
return 'This is not the automation controller';
}
}
$case{firstlooponly} = $xml_test_cases->{case}->{$testnum}->{firstlooponly}; ## only run this test case on the first loop
if ($case{firstlooponly}) { ## is the firstlooponly value set for this testcase?
if ($counter == 1) { ## counter keeps track of what loop number we are on
## run this test case as normal since it is the first pass
}
else {
return 'firstlooponly';
}
}
$case{lastlooponly} = $xml_test_cases->{case}->{$testnum}->{lastlooponly}; ## only run this test case on the last loop
if ($case{lastlooponly}) { ## is the lastlooponly value set for this testcase?
if ($counter == $repeat) { ## counter keeps track of what loop number we are on
## run this test case as normal since it is the first pass
}
else {
return 'lastlooponly';
}
}
return;
}
#------------------------------------------------------------------
sub substitute_variables {
## "method", "description1", "description2", "url", "postbody", "posttype", "addheader", "command", "command1", ... "command20", "parms", "verifytext",
## "verifypositive", "verifypositive1", ... "verifypositive9999",
## "verifynegative", "verifynegative1", ... "verifynegative9999",
## "parseresponse", "parseresponse1", ... , "parseresponse40", ... , "parseresponse9999", "parseresponseORANYTHING", "verifyresponsecode",
## "verifyresponsetime", "sleep", "errormessage", "ignorehttpresponsecode", "ignoreautoassertions", "ignoresmartassertions",
## "retry", "sanitycheck", "logastext", "section", "assertcount", "searchimage", ... "searchimage5", "formatxml", "formatjson",
## "logresponseasfile", "addcookie", "restartbrowseronfail", "restartbrowser", "commandonerror", "gethrefs", "getsrcs", "getbackgroundimages",
## "firstlooponly", "lastlooponly", "decodequotedprintable"
$timestamp = time; #used to replace parsed {timestamp} with real timestamp value
undef %case_save; ## we need a clean array for each test case
undef %case; ## do not allow values from previous test cases to bleed over
foreach my $_case_attribute ( keys %{ $xml_test_cases->{case}->{$testnum} } ) {
#print "DEBUG: $_case_attribute", ": ", $xml_test_cases->{case}->{$testnum}->{$_case_attribute};
#print "\n";
$case{$_case_attribute} = $xml_test_cases->{case}->{$testnum}->{$_case_attribute};
convert_back_xml($case{$_case_attribute});
$case_save{$_case_attribute} = $case{$_case_attribute}; ## in case we have to retry, some parms need to be resubbed
}
return;
}
#------------------------------------------------------------------
sub get_number_of_times_to_retry_this_test_step {
if (defined $case{autoretry}) { $auto_retry = $case{autoretry}; } ## we need to capture this value if it is present since it applies to subsequent steps
$case{retryfromstep} = $xml_test_cases->{case}->{$testnum}->{retryfromstep}; ## retry from a [previous] step
if ($case{retryfromstep}) { ## retryfromstep parameter found
$results_stdout .= qq|Retry from step $case{retryfromstep}\n|;
return 0; ## we will not do a regular retry
}
my $_retry;
$case{retry} = $xml_test_cases->{case}->{$testnum}->{retry}; ## optional retry of a failed test case
if ($case{retry}) { ## retry parameter found
$_retry = $case{retry}; ## assume we can retry as many times as specified
if ($config{globalretry}) { ## ensure that the global retry limit won't be exceeded
if ($_retry > ($config{globalretry} - $globalretries)) { ## we can't retry that many times
$_retry = $config{globalretry} - $globalretries; ## this is the most we can retry
if ($_retry < 0) {
return 0; ## if less than 0 then make 0
}
}
}
$results_stdout .= qq|Retry $_retry times\n|;
return $_retry;
}
## to get this far means there is no retry or retryfromstep parameter, perhaps this step is eligible for autoretry
if ( defined $auto_retry && not ($xml_test_cases->{case}->{$testnum}->{ignoreautoretry}) ) {
if ($attempts_since_last_success < $auto_retry) {
my $_max = $auto_retry - $attempts_since_last_success;
if ($_max > $auto_retry) {$_max = $auto_retry};
if ( ($_max < 0) ) {$_max = 0};
$results_stdout .= qq|Auto Retry $_max times|;
if ($attempts_since_last_success > 0) {
$results_stdout .= " :: attempts_since_last_success[$attempts_since_last_success]";
}
$results_stdout .= "\n";
return $_max;
} else {
$results_stdout .= "Will not auto retry - auto retry set to $auto_retry BUT $attempts_since_last_success attempts since last success\n";
}
}
return 0;
}
#------------------------------------------------------------------
sub substitute_retry_variables {
foreach my $_case_attribute ( keys %{ $xml_test_cases->{case}->{$testnum} } ) {
if (defined $case_save{$_case_attribute}) ## defaulted parameters like posttype may not have a saved value on a subsequent loop
{
$case{$_case_attribute} = $case_save{$_case_attribute}; ## need to restore to the original partially substituted parameter
convert_back_xml_dynamic($case{$_case_attribute}); ## now update the dynamic components
}
}
return;
}
#------------------------------------------------------------------
sub set_retry_to_zero_if_global_limit_exceeded {
if ($config{globalretry}) {
if ($globalretries >= $config{globalretry}) {
$retry = 0; ## globalretries value exceeded - not retrying any more this run
}
}
return;
}
#------------------------------------------------------------------
sub check_for_checkpoint {
if ($case{checkpoint} && lc $case{checkpoint} eq 'false') {
## checkpoint cleared - will not automatically jump back from this step onwards
$results_html .= qq|--- CHECKPOINT CLEARED --- <br />\n|;
$results_stdout .= qq|--- CHECKPOINT CLEARED --- \n|;
$checkpoint = '';
return;
}
if ($case{checkpoint}) {
## checkpoint reached - record where we are up to
$results_html .= qq|--- CHECKPOINT --- <br />\n|;
$results_stdout .= qq|--- CHECKPOINT --- \n|;
$checkpoint = $testnum;
}
return;
}
#------------------------------------------------------------------
sub output_test_step_description {
$results_html .= qq|<b>Test: $current_case_file - <a href="$output_prefix$testnum_display$jumpbacks_print$retries_print.html"> $testnum_display$jumpbacks_print$retries_print </a> </b><br />\n|;
$results_stdout .= qq|Test: $current_case_file - $testnum_display$jumpbacks_print$retries_print \n|;
$results_xml .= qq| <testcase id="$testnum_display$jumpbacks_print$retries_print">\n|;
for (qw/section description1 description2/) { ## support section breaks
next unless defined $case{$_};
$results_html .= qq|$case{$_} <br />\n|;
$results_stdout .= qq|$case{$_} \n|;
$results_xml .= qq| <$_>|._sub_xml_special($case{$_}).qq|</$_>\n|;
}
$results_html .= qq|<br />\n|;
return;
}
#------------------------------------------------------------------
sub output_assertions {
## display and log the verifications to do to stdout and html - xml output is done with the verification itself
## verifypositive, verifypositive1, ..., verifypositive9999 (or even higher)
## verifynegative, verifynegative2, ..., verifynegative9999 (or even higher)
foreach my $_case_attribute ( sort keys %{ $xml_test_cases->{case}->{$testnum} } ) {
if ( (substr $_case_attribute, 0, 14) eq 'verifypositive' || (substr $_case_attribute, 0, 14) eq 'verifynegative') {
my $_verifytype = substr $_case_attribute, 6, 8; ## so we get the word positive or negative
$_verifytype = ucfirst $_verifytype; ## change to Positive or Negative
my @_verifyparms = split /[|][|][|]/, $case{$_case_attribute} ; ## index 0 contains the actual string to verify
$results_html .= qq|Verify $_verifytype: "$_verifyparms[0]" <br />\n|;
$results_stdout .= qq|Verify $_verifytype: "$_verifyparms[0]" \n|;
}
}
if ($case{verifyresponsecode}) {
$results_html .= qq|Verify Response Code: "$case{verifyresponsecode}" <br />\n|;
$results_stdout .= qq|Verify Response Code: "$case{verifyresponsecode}" \n|;
$results_xml .= qq| <verifyresponsecode>$case{verifyresponsecode}</verifyresponsecode>\n|;
}
if ($case{verifyresponsetime}) {
$results_html .= qq|Verify Response Time: at most "$case{verifyresponsetime} seconds" <br />\n|;
$results_stdout .= qq|Verify Response Time: at most "$case{verifyresponsetime}" seconds\n|;
$results_xml .= qq| <verifyresponsetime>$case{verifyresponsetime}</verifyresponsetime>\n|;
}
return;
}
#------------------------------------------------------------------
sub execute_test_step {
if (not $opt_no_output) { print {*STDOUT} $results_stdout; }
undef $results_stdout;
if ($case{method}) {
if ($case{method} eq 'cmd') { cmd(); return; }
}
if (not $session_started) {
start_session();
}
if ($case{method}) {
if ($case{method} eq 'selenium') { WebInjectSelenium::selenium(); return; }
}
set_useragent($xml_test_cases->{case}->{$testnum}->{useragent});
set_max_redirect($xml_test_cases->{case}->{$testnum}->{maxredirect});
if ($case{method}) {
if ($case{method} eq 'get') { httpget(); return;}
if ($case{method} eq 'post') { httppost(); return;}
if ($case{method} eq 'delete') { httpdelete(); return;}
if ($case{method} eq 'put') { httpput(); return;}
}
else {
httpget(); #use "get" if no method is specified
}
return;
}
#------------------------------------------------------------------
sub pass_fail_or_retry {
$attempts_since_last_success++; ## assume failure, will be reset to 0 if that is not the case
## check max jumpbacks - globaljumpbacks - i.e. retryfromstep usages before we give up - otherwise we risk an infinite loop
#if ( (($is_failure > 0) && ($retry < 1) && !($case{retryfromstep})) || (($is_failure > 0) && ($case{retryfromstep}) && ($jumpbacks > ($config{globaljumpbacks}-1) )) || ($fast_fail_invoked eq 'true')) {
if ( ($is_failure && !( retry_available() || retry_from_step_available() || jump_back_to_checkpoint_available() ) ) || $fast_fail_invoked ) {
## if any verification fails, test case is considered a failure UNLESS there is at least one retry available, or it is a retryfromstep case
## however if a verifynegative fails then the case is always a failure
$results_xml .= qq| <success>false</success>\n|;
if ($case{errormessage}) { #Add defined error message to the output
$results_html .= qq|<b><span class="fail">TEST CASE FAILED : $case{errormessage}</span></b><br />\n|;
$results_xml .= ' <result-message>'._sub_xml_special($case{errormessage})."</result-message>\n";
$results_stdout .= qq|TEST CASE FAILED : $case{errormessage}\n|;
if (not $return_message) {
$return_message = $case{errormessage}; ## only return the first error message to nagios
}
}
else { #print regular error output
$results_html .= qq|<b><span class="fail">TEST CASE FAILED</span></b><br />\n|;
$results_xml .= qq| <result-message>TEST CASE FAILED</result-message>\n|;
$results_stdout .= qq|TEST CASE FAILED\n|;
if (not $return_message) {
$return_message = "Test case number $testnum failed"; ## only return the first test case failure to nagios
}
}
$case_failed_count++;
}
elsif (($is_failure > 0) && ($retry > 0)) {#Output message if we will retry the test case
$results_html .= qq|<b><span class="pass">RETRYING... $retry to go</span></b><br />\n|;
$results_stdout .= qq|RETRYING... $retry to go \n|;
$results_xml .= qq| <success>false</success>\n|;
$results_xml .= qq| <result-message>RETRYING... $retry to go</result-message>\n|;
## all this is for ensuring correct behaviour when retries occur
$retries_print = ".$retries";
$retries++;
$globalretries++;
$passed_count = $passed_count - $retry_passed_count;
$failed_count = $failed_count - $retry_failed_count;
}
elsif ( $is_failure && ($case{retryfromstep} || $checkpoint) ) {#Output message if we will retry the test case from step
my $_jump_back_to_step;
if ($case{retryfromstep}) {
$_jump_back_to_step = $case{retryfromstep};
} else {
$_jump_back_to_step = $checkpoint;
$results_stdout .= qq|RESTARTING SESSION BEFORE JUMPING BACK TO CHECKPOINT ... \n|;
start_session();
}
my $_jump_backs_left = $config{globaljumpbacks} - $jumpbacks;
$results_html .= qq|<b><span class="pass">RETRYING FROM STEP $_jump_back_to_step ... $_jump_backs_left tries left</span></b><br />\n|;
$results_stdout .= qq|RETRYING FROM STEP $_jump_back_to_step ... $_jump_backs_left tries left\n|;
$results_xml .= qq| <success>false</success>\n|;
$results_xml .= qq| <result-message>RETRYING FROM STEP $_jump_back_to_step ... $_jump_backs_left tries left</result-message>\n|;
$jumpbacks++; ## increment number of times we have jumped back - i.e. used retryfromstep
$jumpbacks_print = "-$jumpbacks";
$globalretries++;
$passed_count = $passed_count - $retry_passed_count;
$failed_count = $failed_count - $retry_failed_count;
## find the index for the test step we are retrying from
$step_index = 0;
my $_found_index = 'false';
foreach (@test_steps) {
if ($test_steps[$step_index] eq $_jump_back_to_step) {
$_found_index = 'true';
last;
}
$step_index++
}
if ($_found_index eq 'false') {
$results_stdout .= qq|ERROR - COULD NOT FIND STEP $_jump_back_to_step - TESTING STOPS \n|;
}
else
{
$step_index--; ## since we increment it at the start of the next loop / end of this loop
}
}
else {
$results_html .= qq|<b><span class="pass">TEST CASE PASSED</span></b><br />\n|;
$results_stdout .= qq|TEST CASE PASSED \n|;
$results_xml .= qq| <success>true</success>\n|;
$results_xml .= qq| <result-message>TEST CASE PASSED</result-message>\n|;
$case_passed_count++;
$retry = 0; # no need to retry when test case passes
$attempts_since_last_success = 0; # reset attempts for autoretry
}
return;
}
sub retry_available {
return ( ($retry > 0) && !($case{retryfromstep}) ); # retryfromstep ignored if a retry parameter is present
}
sub retry_from_step_available {
return $case{retryfromstep} && ( $jumpbacks < $config{globaljumpbacks} ); # retryfromstep takes priority over checkpoint
}
sub jump_back_to_checkpoint_available {
return $checkpoint && ( $jumpbacks < $config{globaljumpbacks} );
}
#------------------------------------------------------------------
sub output_test_step_latency {
$results_html .= qq|Response Time = $latency sec <br />\n|;
$results_stdout .= qq|Response Time = $latency sec \n|;
$results_xml .= qq| <responsetime>$latency</responsetime>\n|;
if ($case{method} eq 'selenium') {
$results_html .= qq|Verification Time = $verification_latency sec <br />\n|;
$results_html .= qq|Screenshot Time = $screenshot_latency sec <br />\n|;
$results_stdout .= qq|Verification Time = $verification_latency sec \n|;
$results_stdout .= qq|Screenshot Time = $screenshot_latency sec \n|;
$results_xml .= qq| <verificationtime>$verification_latency</verificationtime>\n|;
$results_xml .= qq| <screenshottime>$screenshot_latency</screenshottime>\n|;
}
return;
}
#------------------------------------------------------------------
sub output_test_step_results {
$results_xml .= qq| </testcase>\n\n|;
_write_xml (\$results_xml);
undef $results_xml;
$results_html .= qq|<br />\n------------------------------------------------------- <br />\n\n|;
_write_html (\$results_html);
undef $results_html;
$results_stdout .= qq|------------------------------------------------------- \n|;
if (not $opt_no_output) { print {*STDOUT} $results_stdout; }
undef $results_stdout;
return;
}
#------------------------------------------------------------------
sub increment_run_count {
if ( ( ($is_failure > 0) && ($retry > 0) && !($case{retryfromstep}) ) ||
( ($is_failure > 0) && $case{retryfromstep} && ($jumpbacks < $config{globaljumpbacks} ) && !$fast_fail_invoked )
) {
## do not count this in run count if we are retrying
}
else {
$run_count++;
$total_run_count++;
}
return;
}
#------------------------------------------------------------------
sub update_latency_statistics {
if ($latency > $max_response) { $max_response = $latency; } #set max response time
if ($latency < $min_response) { $min_response = $latency; } #set min response time
$total_response = ($total_response + $latency); #keep total of response times for calculating avg
$test_step_time{$testnum_display}=$latency; ## store latency for step
return;
}
#------------------------------------------------------------------
sub restart_browser {
if ($case{restartbrowseronfail} && ($is_failure > 0)) { ## restart the Selenium browser session and also the WebInject session
$results_stdout .= qq|RESTARTING SESSION DUE TO FAIL ... \n|;
start_session();
}
if ($case{restartbrowser}) { ## restart the Selenium browser session and also the WebInject session
$results_stdout .= qq|RESTARTING SESSION ... \n|;
start_session();
}
return;
}
#------------------------------------------------------------------
sub sleep_before_next_step {
if ( (($is_failure < 1) && ($case{retry})) || (($is_failure < 1) && ($case{retryfromstep})) || (($is_failure < 1) && $checkpoint) )
{
## ignore the sleep if the test case worked and it is a retry test case (including active checkpoint)
}
else
{
if ($case{sleep})
{
if ( (($is_failure > 0) && ($retry < 1)) || (($is_failure > 0) && ($jumpbacks > ($config{globaljumpbacks}-1))) )
{
## do not sleep if the test case failed and we have run out of retries or jumpbacks
}
else
{
## if a sleep value is set in the test case, sleep that amount
$results_html .= qq|INVOKED SLEEP of $case{sleep} seconds<br />\n|;
$results_stdout .= qq|INVOKED SLEEP of $case{sleep} seconds\n|;
sleep $case{sleep};
}
}
}
return;
}
#------------------------------------------------------------------
sub write_initial_html { #write opening tags for results file
$results_html .= qq|<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n|;
$results_html .= qq| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\n|;
$results_html .= qq|<html xmlns="http://www.w3.org/1999/xhtml">\n|;
$results_html .= qq|<head>\n|;
$results_html .= qq| <title>WebInject Test Results</title>\n|;
$results_html .= qq| <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n|;
$results_html .= qq| <style type="text/css">\n|;
$results_html .= qq| body {\n|;
$results_html .= qq| background-color: #F5F5F5;\n|;
$results_html .= qq| color: #000000;\n|;
$results_html .= qq| font-family: Verdana, Arial, Helvetica, sans-serif;\n|;
$results_html .= qq| font-size: 10px;\n|;
$results_html .= qq| }\n|;
$results_html .= qq| .pass {\n|;
$results_html .= qq| color: green;\n|;
$results_html .= qq| }\n|;
$results_html .= qq| .fail {\n|;
$results_html .= qq| color: red;\n|;
$results_html .= qq| }\n|;
$results_html .= qq| .skip {\n|;
$results_html .= qq| color: orange;\n|;
$results_html .= qq| }\n|;
$results_html .= qq| </style>\n|;
$results_html .= qq|</head>\n|;
$results_html .= qq|<body>\n|;
$results_html .= qq|<hr />\n|;
$results_html .= qq|-------------------------------------------------------<br />\n\n|;
return;
}
#------------------------------------------------------------------
sub _whack {
my ($_goner) = @_;
if (-e $_goner ) {
unlink $_goner or die "Could not unlink $_goner\n";
}
return;
}
#------------------------------------------------------------------
sub write_initial_xml { #write opening tags for results file
# put a reference to the stylesheet in the results file
my $_results_xml = '<?xml version="1.0" encoding="ISO-8859-1"?>'."\n";
$_results_xml .= '<?xml-stylesheet type="text/xsl" href="../../../../../../../content/Results.xsl"?>'."\n";
$_results_xml .= "<results>\n\n";
$results_xml_file_name = 'results.xml';
if ( defined $user_config->{wif}->{dd} && defined $user_config->{wif}->{run_number} ) { # presume if this info is present, webinject.pl has been called by wif.pl
$results_xml_file_name = 'results_'.$user_config->{wif}->{run_number}.'.xml';
$_results_xml .= " <wif>\n";
$_results_xml .= " <environment>$user_config->{wif}->{environment}</environment>\n";
$_results_xml .= " <yyyy>$user_config->{wif}->{yyyy}</yyyy>\n";
$_results_xml .= " <mm>$user_config->{wif}->{mm}</mm>\n";
$_results_xml .= " <dd>$user_config->{wif}->{dd}</dd>\n";
$_results_xml .= " <batch>$user_config->{wif}->{batch}</batch>\n";
$_results_xml .= " </wif>\n";
}
$_results_xml .= qq|\n <testcases file="$current_case_file">\n\n|;
_whack($opt_publish_full.$results_xml_file_name);
_write_xml(\$_results_xml);
return;
}
#------------------------------------------------------------------
sub _write_xml {
my ($_xml) = @_;
if (not $opt_no_output) {
open my $_RESULTS_XML, '>>', "$opt_publish_full".$results_xml_file_name or die "\nERROR: Failed to open results.xml file\n\n";
print {$_RESULTS_XML} ${$_xml};
close $_RESULTS_XML or die "\nCould not close xml results file\n\n";
}
return;
}
#------------------------------------------------------------------
sub _write_html {
my ($_html) = @_;
if (not $opt_no_output) {
open my $_RESULTS_HTML, '>>', $opt_publish_full.'results.html' or die "\nERROR: Failed to open results.html file\n\n";
print {$_RESULTS_HTML} ${$_html};
close $_RESULTS_HTML or die "\nCould not close html results file\n\n";
}
return;
}
#------------------------------------------------------------------
sub write_initial_stdout { #write initial text for STDOUT
$results_stdout .= "\n";
$results_stdout .= "Starting WebInject Engine...\n\n";
return;
}
#------------------------------------------------------------------
sub write_final_html { #write summary and closing tags for results file
$results_html .= qq|<br /><hr /><br />\n|;
$results_html .= qq|<b>\n|;
$results_html .= qq|Start Time: $current_date_time <br />\n|;
$results_html .= qq|Total Run Time: $total_run_time seconds <br />\n|;
$results_html .= qq|Total Response Time: $total_response seconds <br />\n|;
$results_html .= qq|<br />\n|;
$results_html .= qq|Test Cases Run: $total_run_count <br />\n|;
$results_html .= qq|Test Cases Passed: $case_passed_count <br />\n|;
$results_html .= qq|Test Cases Failed: $case_failed_count <br />\n|;
$results_html .= qq|Verifications Passed: $passed_count <br />\n|;
$results_html .= qq|Verifications Failed: $failed_count <br />\n|;
$results_html .= qq|<br />\n|;
$results_html .= qq|Average Response Time: $avg_response seconds <br />\n|;
$results_html .= qq|Max Response Time: $max_response seconds <br />\n|;
$results_html .= qq|Min Response Time: $min_response seconds <br />\n|;
$results_html .= qq|</b>\n|;
$results_html .= qq|<br />\n\n|;
$results_html .= qq|</body>\n|;
$results_html .= qq|</html>\n|;
_write_html(\$results_html);
undef $results_html;
return;
}
#------------------------------------------------------------------
sub write_final_xml { #write summary and closing tags for XML results file
if ($case{sanitycheck} && ($case_failed_count > 0)) { ## sanitycheck
$sanity_result = 'false';
}
else {
$sanity_result = 'true';
}
$results_xml .= qq| </testcases>\n\n|;
$results_xml .= qq| <test-summary>\n|;
$results_xml .= qq| <start-time>$current_date_time</start-time>\n|;
$results_xml .= qq| <start-seconds>$TIMESECONDS</start-seconds>\n|;
$results_xml .= qq| <start-date-time>$STARTDATE|;
$results_xml .= qq|T$HOUR:$MINUTE:$SECOND</start-date-time>\n|;
$results_xml .= qq| <total-run-time>$total_run_time</total-run-time>\n|;
$results_xml .= qq| <total-response-time>$total_response</total-response-time>\n|;
$results_xml .= qq| <test-cases-run>$total_run_count</test-cases-run>\n|;
$results_xml .= qq| <test-cases-passed>$case_passed_count</test-cases-passed>\n|;
$results_xml .= qq| <test-cases-failed>$case_failed_count</test-cases-failed>\n|;
$results_xml .= qq| <verifications-passed>$passed_count</verifications-passed>\n|;
$results_xml .= qq| <verifications-failed>$failed_count</verifications-failed>\n|;
$results_xml .= qq| <assertion-skips>$total_assertion_skips</assertion-skips>\n|;
$results_xml .= qq| <average-response-time>$avg_response</average-response-time>\n|;
$results_xml .= qq| <max-response-time>$max_response</max-response-time>\n|;
$results_xml .= qq| <min-response-time>$min_response</min-response-time>\n|;
$results_xml .= qq| <sanity-check-passed>$sanity_result</sanity-check-passed>\n|;
$results_xml .= qq| <test-file-name>$testfilename</test-file-name>\n|;
$results_xml .= qq| </test-summary>\n\n|;
$results_xml .= qq|</results>\n|;
_write_xml(\$results_xml);
undef $results_xml;
return;
}
#------------------------------------------------------------------
sub write_final_stdout { #write summary and closing text for STDOUT
$results_stdout .= qq|Start Time: $current_date_time\n|;