Saturday, September 15, 2012

Another method for running Selenium tests in parallel using Perl

Here is another quick tutorial on how to implement a parallel test runner (for Selenium or any other type of test).

First the code:
1:  #!C:/Perl64/bin/perl  
2:  use strict;  
3:  use warnings;  
4:  use File::Find::Rule;  
5:    
6:  my $start_dir = shift || 'C:\Tests';  
7:  my $count = 0;  
8:    
9:  my @subdirs = File::Find::Rule->directory->in($start_dir);  
10:    
11:  my @files = File::Find::Rule->file()  
12:                ->name( '*.pl' ) # Only get perl files  
13:                ->in( $subdirs[0] );  
14:  foreach my $test (@files) {  
15:    system(1, $test);  
16:    print "Executing Test: " . $test . " Sequence #: " . $count . "\n";  
17:    print "My Father Is: " . $$ . "\n"; # process that started me  
18:    $count++;  
19:  }  
20:  exit(0);  
The magic here is being performed by line 15. Using the perl function system in this special form we can force it to not wait for the process it is launching before continuing, effectively allowing us to spawn multiple tests at the [almost] same time.

From the Perl docs:
system(1, @args)
spawns an external process and immediately returns its process designator, without waiting for it to terminate.
The above script will output the following:
 Executing Test: C:\Temp\test_template.pl Sequence #: 0  
 My Father Is: 8692  
 Executing Test: C:\Temp\test_template2.pl Sequence #: 1  
 My Father Is: 8692  
 Executing Test: C:\Temp\test_template3.pl Sequence #: 2  
 My Father Is: 8692  
 Executing Test: C:\Temp\test_template4.pl Sequence #: 3  
 My Father Is: 8692  
The below image shows how, in my debugger, the test runner has stopped but the 4 spawned processes are still running (theoretically running automated tests) as indicated in my Windows Task Manager by the running perl.exe processes.



That's all it takes to launch multiple automated tests at the same time, using Perl's system function.

No comments:

Post a Comment

Creative Commons License
VGP-Miami Web and Mobile Automation Blog by Alfred Vega is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.