Fixes to work with Catalyst 5.9 engine.
[catagits/Test-WWW-Selenium-Catalyst.git] / lib / Test / WWW / Selenium / Catalyst.pm
index 9f333ea..c6379e3 100644 (file)
@@ -7,6 +7,7 @@ use Alien::SeleniumRC;
 use Test::WWW::Selenium;
 use Test::More;
 use Catalyst::Utils;
+use Catalyst::EngineLoader;
 
 BEGIN { $ENV{CATALYST_ENGINE} ||= 'HTTP'; }
 
@@ -24,7 +25,7 @@ Test::WWW::Selenium::Catalyst - Test your Catalyst application with Selenium
 
 =cut
 
-our $VERSION = '0.03';
+our $VERSION = '0.06';
 
 =head1 DEVELOPERISH RELEASE
 
@@ -73,6 +74,8 @@ application server will be started.
 
 =item port
 
+B<Default>: 3000
+
 Port on which to run the catalyst application server. The C<MYAPP_PORT>
 environment variable is also respected.
 
@@ -89,25 +92,25 @@ selenium to add custom logic.
 =item selenium_port
 
 Location of externally running selenium server if you do not wish this module
-to control one. See also below for details.
+to control one. See also for details.
 
 =back
 
 All other options passed verbatim to the selenium constructor.
 
-B<NOTE>: By default a selenium server is started when you C<use> this module, and
-it's killed when your test exits. If wish to manage a selenium server yourself,
-(for instance you wish to start up a server once and run a number of tests
-against it) pass C<-no_selenium_server> to import:
+B<NOTE>: By default a selenium server is started when you C<use> this module,
+and it's killed when your test exits. If wish to manage a selenium server
+yourself, (for instance you wish to start up a server once and run a number of
+tests against it) pass C<-no_selenium_server> to import:
 
- use Test::WWW::Selenium 'MyApp'
-   -no_selenium_server = 1
+ use Test::WWW::Selenium 'MyApp',
+   -no_selenium_server => 1
 
 Along a similar vein you can also pass command line arguments to the selenium
 server via C<-selenium_args>:
 
- use Test::WWW::Selenium 'MyApp'
-   -selenium_args = "-singleWindow -port 4445"
+ use Test::WWW::Selenium 'MyApp',
+   -selenium_args => "-singleWindow -port 4445"
 
 =head2 sel_pid
 
@@ -172,8 +175,17 @@ sub import {
     if(defined $d){
         $DEBUG = $d;
     }
+
+    $args{-selenium_args} ||= '-singleWindow';
+
+    if ($ENV{SELENIUM_SERVER}) {
+        $args{-no_selenium_server} = 1;
+    }
+    elsif ($ENV{SELENIUM_PORT}) {
+        $args{-selenium_args} .= " -port " . $ENV{SELENIUM_PORT};
+    }
    
-    unless ($args{-no_seleniun_server}) {
+    unless ($args{-no_selenium_server}) {
       $class->_start_server($args{-selenium_args}) or croak "Couldn't start selenium server";
     }
     return 1;
@@ -205,8 +217,16 @@ sub start {
               diag("Catalyst server $$ going down (TERM)") if $DEBUG;
               exit 0;
           };
-          diag("Catalyst server running in $$") if $DEBUG;
-          $app->run($port, 'localhost');
+          diag("Catalyst server running in pid $$ with port $port") if $DEBUG;
+          my $loader = Catalyst::EngineLoader->new(application_name => $app);
+          my $server = $loader->auto(port => $port, host => 'localhost',
+              server_ready => sub {
+                  diag("Server started on port $port") if $DEBUG;
+              },
+          );
+          $app->run($port, 'localhost', $server);
+
+          diag("Process $$ (catalyst server) exiting.") if $DEBUG;
           exit 1;
       }
       $uri = 'http://localhost:' . $port;
@@ -218,6 +238,19 @@ sub start {
     my $sel_class = delete $args->{selenium_class} || 'Test::WWW::Selenium';
     my $sel;
 
+    if ($ENV{SELENIUM_SERVER}) {
+        my $uri = $ENV{SELENIUM_SERVER};
+        $uri =~ s!^(?:http://)?!http://!;
+        $uri = new URI($uri);
+        $args->{selenium_host} = $uri->host;
+        $args->{selenium_port} = $uri->port;
+    }
+    elsif ($ENV{SELENIUM_PORT}) {
+        $args->{selenium_port} = $ENV{SELENIUM_PORT};
+    }
+
+    my $sel_host = delete $args->{selenium_host} || 'localhost';
+    my $sel_port = delete $args->{selenium_port} || 4444;
     while(!$sel && $tries--){ 
         sleep 1;
         diag("Waiting for selenium server to start")
@@ -225,8 +258,8 @@ sub start {
         
         eval {
             $sel = $sel_class->new(
-                host => delete $args->{selenium_host} || 'localhost',
-                port => delete $args->{selenium_port} || 4444,
+                host => $sel_host,
+                port => $sel_port,
                 browser => '*firefox',
                 browser_url => $uri,
                 auto_stop => 0,
@@ -244,13 +277,20 @@ END {
     if($sel_pid){
         if($www_selenium){
             diag("Shutting down Selenium Server $sel_pid") if $DEBUG;
-            $www_selenium->do_command('shutDown');
+            $www_selenium->stop();
+            # This can fail if a page hasn't been requested yet.
+            eval { $www_selenium->do_command('shutDownSeleniumServer') };
             undef $www_selenium;
         }
         diag("Killing Selenium Server $sel_pid") if $DEBUG;
         kill 15, $sel_pid or diag "Killing Selenium: $!";
         undef $sel_pid;
+
+    } elsif ($www_selenium) {
+        diag("Using external Selenium server. Don't shut it down.") if $DEBUG;
+        undef $www_selenium;
     }
+
     if($app_pid){
         diag("Killing catalyst server $app_pid") if $DEBUG;
         kill 15, $app_pid or diag "Killing MyApp: $!";
@@ -291,11 +331,12 @@ C<MyApp> is the name of your Catalyst app.
 
 =item * 
 
-Selenium website: L<http://www.openqa.org/>
+Selenium website: L<http://seleniumhq.org/>
 
 =item * 
 
 Description of what you can do with the C<$sel> object: L<Test::WWW::Selenium>
+and L<WWW::Selenium>
 
 =item * 
 
@@ -305,6 +346,8 @@ If you don't need a real web browser: L<Test::WWW::Mechanize::Catalyst>
 
 =head1 AUTHOR
 
+Ash Berlin C<< <ash@cpan.org> >>
+
 Jonathan Rockway, C<< <jrockway at cpan.org> >>
 
 =head1 BUGS
@@ -319,20 +362,23 @@ your bug as I make changes.
 
 Send me unified diffs against the git HEAD at:
 
-    git://git.jrock.us/Test-WWW-Selenium-Catalyst
+    git://github.com/jrockway/test-www-selenium-catalyst.git
 
 You can view the repository online at 
 
-    http://git.jrock.us/?p=Test-WWW-Selenium-Catalyst.git;a=summary
+    http://github.com/jrockway/test-www-selenium-catalyst/tree/master
 
 Thanks in advance for your contributions!
 
 =head1 ACKNOWLEDGEMENTS
 
-Thanks for mst for getting on my case to actually write this thing :)
+Thanks for mst for getting on my (jrockway's) case to actually write this thing
+:)
 
 =head1 COPYRIGHT & LICENSE
 
+Copyright 2009 Ash Berlin, all rights reserved.
+
 Copyright 2006 Jonathan Rockway, all rights reserved.
 
 This program is free software; you can redistribute it and/or modify it