| 1 | package Jifty::Test::WWW::WebDriver; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use base 'Test::WebDriver'; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Jifty::Test::WebDriver - Subclass of L<Test::WebDriver> with | ||
| 10 | extra Jifty integration | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use Jifty::Test::WWW::WebDriver; | ||
| 15 | my $server = Jifty::Test->make_server; | ||
| 16 | my $sel = Jifty::Test::WWW::WebDriver->rc_ok( $server, lang => 'en_US.UTF-8' ); | ||
| 17 | my $URL = $server->started_ok; | ||
| 18 | |||
| 19 | $sel->get_ok( $URL )); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | L<Jifty::Test::WWW::WebDriver> creates a L<Test::WebDriver> object | ||
| 24 | associated with your jifty application to test. In addition, it | ||
| 25 | starts Selenium server for you, unless it finds a server already running. | ||
| 26 | |||
| 27 | You can specify where to look for the Selenium server using environment | ||
| 28 | variables: C<$TWD_HOST>, C<$TWD_PORT>, C<$TWD_BROWSER>. See | ||
| 29 | L<Test::WebDriver> for all the options, and see L<Selenium::Remote::Driver> | ||
| 30 | for their meanings. | ||
| 31 | |||
| 32 | You may want to set C<TEST_CLASS> to a subclass of L<Test::WebDriver> to | ||
| 33 | refactor methods common to your application. | ||
| 34 | |||
| 35 | =head2 rc_ok | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub rc_ok { | ||
| 40 | my $class = shift; | ||
| 41 | my $server = shift; | ||
| 42 | my %args = @_; | ||
| 43 | |||
| 44 | $class->_skip_rest("live test doesn't work on Win32 at the moment") | ||
| 45 | if $^O eq 'MSWin32'; | ||
| 46 | |||
| 47 | $ENV{JIFTY_OPENID_WHITELIST_HOST} = $ENV{SELENIUM_RC_TEST_AGAINST} || 'localhost'; | ||
| 48 | |||
| 49 | my ($host, $port) = $class->server_is_running; | ||
| 50 | unless ($host and $port) { | ||
| 51 | $class->_skip_rest("No selenium server was found."); | ||
| 52 | } | ||
| 53 | |||
| 54 | $args{browser_url} ||= 'http://'.($ENV{SELENIUM_RC_TEST_AGAINST} || $args{test_server} || 'localhost').':'.$server->port; | ||
| 55 | |||
| 56 | $args{browser_name} ||= $ENV{TWD_BROWSER}; | ||
| 57 | $args{test_class} ||= 'Test::WebDriver'; | ||
| 58 | |||
| 59 | # This module does not yet launch selenium instances in the background, as | ||
| 60 | # Selenium is not packaged in a way we can count on - eg: there is no | ||
| 61 | # Alien package yet. | ||
| 62 | my $sel = eval { $args{test_class}->new( %args, auto_close => 1 ) }; | ||
| 63 | if ($@) { | ||
| 64 | $class->_skip_rest("Died trying to create a $args{test_class} object: $@"); | ||
| 65 | } | ||
| 66 | if (!$sel) { | ||
| 67 | $class->_skip_rest("No selenium server was found."); | ||
| 68 | } | ||
| 69 | |||
| 70 | $sel->{browser_url} = $args{browser_url}; | ||
| 71 | return $sel; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _skip_rest { | ||
| 75 | my $self = shift; | ||
| 76 | my $why = shift; | ||
| 77 | |||
| 78 | # Schwern: i want skip_rest | ||
| 79 | my $Tester = Test::Builder->new; | ||
| 80 | $Tester->skip($why); | ||
| 81 | |||
| 82 | unless ($Tester->{No_Plan}) { | ||
| 83 | for my $ct ($Tester->{Curr_Test} + 1 .. $Tester->{Expected_Tests}) { | ||
| 84 | $Tester->skip($why); # skip rest of the test | ||
| 85 | } | ||
| 86 | } | ||
| 87 | $Tester->done_testing(); | ||
| 88 | exit(0); | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; |