new API spec, first shot at implementation
Chris Nehren [Fri, 8 Apr 2011 09:55:36 +0000 (05:55 -0400)]
lib/Test/Harness/Selenium.pm

index 678556f..fa7130b 100644 (file)
@@ -7,6 +7,7 @@ use HTML::TableExtract;
 use IO::All;
 use Alien::SeleniumRC;
 use LWP::Simple;
+use Child;
 
 use Test::More;
 BEGIN {
@@ -30,62 +31,77 @@ BEGIN {
 }
 
 sub new {
-  my ($class, $self) = @_;
-  if($self->{xvnc}) {
-    $ENV{DISPLAY} = $self->{xvnc};
-    my $xvnc_pid = fork();
-    if(!defined $xvnc_pid) {
-      die "couldn't fork xvnc: $!";
-    }
-    elsif($xvnc_pid) {
-      $self->{xvnc_pid} = $xvnc_pid;
-    }
-    else {
-      exec("vncserver", $self->{xvnc});
-    }
+  my $class = shift;
+  my %args = @_;
+  if( $ENV{SELENIUM_RC_HOST} && 
+      $ENV{SELENIUM_RC_PORT} &&
+      $ENV{SELENIUM_RC_START} ) {
+    $args{selenium_rc}{host} = $ENV{SELENIUM_RC_HOST};
+    $args{selenium_rc}{port} = $ENV{SELENIUM_RC_PORT};
+    $args{selenium_rc}{start} = $ENV{SELENIUM_RC_START};
   }
+  my $self = \%args;
   bless $self, $class;
 }
 
-sub start_server {
+sub start_selenium_server {
   my($self) = @_;
-  $self->{server_pid} = fork();
-  if(!defined $self->{server_pid}) {
-    die "can't fork: $!";
-  }
-  if($self->{server_pid} > 0) {
-    my $tries = 0;
-    while($tries < 5) {
-      eval {
-        $self->{src} = Socialtext::WikiFixture::Selenese->new(
-          host => $self->{host},
-          port => $self->{port},
-          browser => $self->{browser},
-          browser_url => $self->{browser_url},
-        );
-      };
-      $tries++;
-      if(!defined $self->{src}) {
-        sleep 10;
+  if($self->{selenium_rc}{start}) {
+    $self->{selenium_rc}{xvnc_server_proc} = Child->new(sub {
+        system('ssh', $self->{selenium_rc}{host}, 'vncserver');
       }
-      else {
-        last;
+    );
+    $self->{selenium_rc}{xvnc_server_proc}->start;
+    $self->{selenium_rc}{selenium_server_proc} = Child->new(sub {
+        system('ssh', $self->{selenium_rc}{host}, 'env',
+          "DISPLAY=:$self->{selenium_rc}{xvnc_display}", 'selenium-rc', '-port',
+          $self->{selenium_rc}{port} );
       }
-    }
-    die "timed out waiting for selenium server to start" if $tries == 5;
+    );
+    $self->{selenium_rc}{selenium_server_proc}->start;
   }
-  elsif($self->{server_pid} == 0) {
-    # muttermutter, can't specify a host for selenium
-    exec("$^X -MAlien::SeleniumRC -e 'Alien::SeleniumRC::start(q{-port $self->{port}})'");
+  my $tries = 0;
+  while($tries < 5) {
+    eval {
+      $self->{src} = Socialtext::WikiFixture::Selenese->new(
+        host => $self->{selenium_rc}{host},
+        port => $self->{selenium_rc}{port},
+        browser => $self->{browser},
+        browser_url => $self->{app_base},
+      );
+    };
+    $tries++;
+    if(!defined $self->{src}) {
+      sleep 10;
+    }
+    else {
+      last;
+    }
   }
+  die "timed out waiting for selenium server to start" if $tries == 5;
 }
 
-sub stop_server {
+sub stop_selenium_server {
   my($self) = @_;
   # okay, we're done, kill the server.
-  get("http://localhost:$self->{port}/selenium-server/driver/?cmd=shutDownSeleniumServer");
+  my $url = sprintf
+    "http://%s:%s/selenium-server/driver/?cmd=shutDownSeleniumServer",
+    $self->{selenium_rc}{host}, $self->{selenium_rc}{port};
+  get($url);
   delete $self->{src};
-  wait;
+  $self->{selenium_rc}{selenium_server_proc}->wait;
+  $self->{selenium_rc}{xvnc_server_proc}->wait;
+}
+
+sub start_app_server {
+  my($self) = @_;
+  $self->{app_server_proc} = Child->new(sub { exec($self->{app_server_cmd}) } );
+  $self->{app_server_proc}->start;
+}
+
+sub stop_app_server {
+  my($self) = @_;
+  $self->{app_server_proc}->complete || $self->{app_server_proc}->kill(9);
 }
 
 sub test_directory {