add another test, almost working support for selenium RC forking and running multiple...
Chris Nehren [Wed, 12 Jan 2011 11:10:26 +0000 (06:10 -0500)]
lib/Test/Harness/Selenium.pm
t/basic.t [moved from t with 73% similarity]
t/corpus/basic.html [copied from basic.html with 100% similarity]
t/corpus/no_delay.html [moved from basic.html with 78% similarity]

index ecaf665..4766a28 100644 (file)
@@ -1,10 +1,12 @@
 package Test::Harness::Selenium;
 use strictures 1;
 
-use File::Find;
+use File::Find::Rule;
 use Socialtext::WikiFixture::Selenese;
 use HTML::TableExtract;
 use IO::All;
+use Alien::SeleniumRC;
+use LWP::Simple;
 
 use Test::More;
 BEGIN {
@@ -32,23 +34,63 @@ sub new {
   bless $self, $class;
 }
 
+sub start_server {
+  my($self) = @_;
+  my $server_pid = fork();
+  if($server_pid > 0) {
+    $self->{server_pid} = $server_pid;
+    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;
+      }
+      else {
+        last;
+      }
+    }
+    die "timed out waiting for selenium server to start" if $tries == 5;
+  }
+  elsif($server_pid == 0) {
+    close STDOUT;
+    close STDERR;
+    # muttermutter, can't specify a host for selenium
+    Alien::SeleniumRC::start("-port $self->{port}");
+  }
+  else {
+    die "can't fork: $!";
+  }
+}
+
+sub stop_server {
+  my($self) = @_;
+  # okay, we're done, kill the server.
+  get("http://localhost:$self->{port}/selenium-server/driver/?cmd=shutDownSeleniumServer");
+  wait;
+}
+
 sub test_directory {
   my ($self, $dir) = @_;
   my @tests = File::Find::Rule->file()->name('*.html')->in($dir);
-  $self->run_tests_for($_) for @tests;
+  for my $test (@tests) {
+    $self->start_server;
+    $self->run_tests_for($test);
+    $self->stop_server;
+  }
 }
 
 sub run_tests_for {
   my ($self, $html_file) = @_;
   my $rows = $self->get_rows_for($html_file);
-  my $src = Socialtext::WikiFixture::Selenese->new(
-    host => $self->{host},
-    port => $self->{port},
-    browser => $self->{browser},
-    browser_url => $self->{browser_url},
-  );
-  $src->run_test_table($rows);
-  done_testing;
+  eval { $self->{src}->run_test_table($rows); };
 }
 
 my $te = HTML::TableExtract->new;
diff --git a/t b/t/basic.t
similarity index 73%
rename from t
rename to t/basic.t
index 6bae426..303599b 100644 (file)
--- a/t
+++ b/t/basic.t
@@ -4,11 +4,13 @@ use warnings;
 
 use lib 'lib';
 use Test::Harness::Selenium;
+use Test::More;
 
 my $s = Test::Harness::Selenium->new({
     host => 'localhost',
-    port => 4444,
+    port => $< + 60_000,
     browser_url => 'http://www.google.com',
     browser => shift,
 });
-$s->run_tests_for(shift);
+$s->test_directory(shift);
+done_testing;
similarity index 100%
copy from basic.html
copy to t/corpus/basic.html
similarity index 78%
rename from basic.html
rename to t/corpus/no_delay.html
index 28e8497..8e74511 100644 (file)
 <td>q</td>
 <td>selenium rc</td>
 </tr>
-<tr><td>clickAndWait</td>
-<td>btnG</td>
-<td>&nbsp;</td>
-</tr>
 <tr><td>textLike</td>
 
-<td>Searches related to selenium rc</td>
+<td>Searches related to rc</td>
 <td>&nbsp;</td>
 </tr>
 </tbody>