Skip tests when java isn't installed/runable (IPC::Cmd)
[catagits/Test-WWW-Selenium-Catalyst.git] / lib / Test / WWW / Selenium / Catalyst.pm
CommitLineData
4f6d213e 1package Test::WWW::Selenium::Catalyst;
2
3use warnings;
4use strict;
5use Carp;
6use Alien::SeleniumRC;
7use Test::WWW::Selenium;
8use Test::More;
9use Catalyst::Utils;
10
11BEGIN { $ENV{CATALYST_ENGINE} ||= 'HTTP'; }
12
13local $SIG{CHLD} = 'IGNORE';
14
374611f4 15our $DEBUG = $ENV{CATALYST_DEBUG};
16our $app; # app name (MyApp)
17our $sel_pid; # pid of selenium server
18our $app_pid; # pid of myapp server
19our $www_selenium;
4f6d213e 20
21=head1 NAME
22
1cdeb21f 23Test::WWW::Selenium::Catalyst - Test your Catalyst application with Selenium
4f6d213e 24
25=cut
26
0744bdf6 27our $VERSION = '0.05';
4f6d213e 28
de35f86c 29=head1 DEVELOPERISH RELEASE
1cdeb21f 30
de35f86c 31This is still a test release. It's working for me in production, but
1cdeb21f 32it depends on a Java application (SeleniumRC), which can be
33unreliable. On my Debian system, I had to put C<firefox-bin> in my
34path, and add C</usr/lib/firefox> to C<LD_LIBRARY_PATH>. Every distro
35and OS is different, so I'd like some feedback on how this works on
36your system. I would like to find a clean solution that lets this
37module "Just Work" for everyone, but I have a feeling that it's going
38to look more like C<if(gentoo){ ... } elsif (debian) { ... }> and so
39on. I can live with that, but I need your help to get to that stage!
40
41Please report any problems to RT, the Catalyst mailing list, or the
42#catalyst IRC channel on L<irc.perl.org>. Thanks!
43
4f6d213e 44=head1 SYNOPSIS
45
374611f4 46 use Test::WWW::Selenium::Catalyst 'MyApp', 'command line to selenium';
4f6d213e 47 use Test::More tests => 2;
48
49 my $sel = Test::WWW::Selenium::Catalyst->start;
50 $sel->open_ok('/');
51 $sel->is_text_present_ok('Welcome to MyApp');
52
1cdeb21f 53This module starts the SeleniumRC server and your Catalyst app so that
54you can test it with SeleniumRC. Once you've called
6fd21f2e 55C<< Test::WWW::Selenium::Catalyst->start >>, everything is just like
1cdeb21f 56L<Test::WWW::Selenium|Test::WWW:Selenium>.
57
cba20015 58=head1 METHODS
4f6d213e 59
374611f4 60=head2 start(\%args)
4f6d213e 61
374611f4 62Starts the Selenium and Catalyst servers, and returns a pre-initialized,
63ready-to-use Test::WWW::Selenium object.
4f6d213e 64
374611f4 65Arguments:
66
67=over
68
69=item app_uri
70
71URI at which the application can be reached. If this is specified then no
72application server will be started.
73
74=item port
75
8f552b3b 76B<Default>: 3000
77
374611f4 78Port on which to run the catalyst application server. The C<MYAPP_PORT>
79environment variable is also respected.
80
81
82=item selenium_class
83
84B<Default>: Test::WWW::Selenium
85
86Classname of Selenium object to create. Use this if you want to subclass
87selenium to add custom logic.
88
89=item selenium_host
90
91=item selenium_port
92
93Location of externally running selenium server if you do not wish this module
8f552b3b 94to control one. See also for details.
374611f4 95
96=back
97
98All other options passed verbatim to the selenium constructor.
99
8f552b3b 100B<NOTE>: By default a selenium server is started when you C<use> this module,
101and it's killed when your test exits. If wish to manage a selenium server
102yourself, (for instance you wish to start up a server once and run a number of
103tests against it) pass C<-no_selenium_server> to import:
374611f4 104
105 use Test::WWW::Selenium 'MyApp'
8f552b3b 106 -no_selenium_server => 1
374611f4 107
108Along a similar vein you can also pass command line arguments to the selenium
109server via C<-selenium_args>:
110
111 use Test::WWW::Selenium 'MyApp'
8f552b3b 112 -selenium_args => "-singleWindow -port 4445"
4f6d213e 113
114=head2 sel_pid
115
116Returns the process ID of the Selenium Server.
117
118=head2 app_pid
119
120Returns the process ID of the Catalyst server.
121
122=cut
123
124
125sub _start_server {
374611f4 126 my ($class, $args) = @_;
4f6d213e 127 # fork off a selenium server
128 my $pid;
129 if(0 == ($pid = fork())){
374611f4 130 local $SIG{TERM} = sub {
131 diag("Selenium server $$ going down (TERM)") if $DEBUG;
132 exit 0;
133 };
134
135 chdir '/';
136
137 if(!$DEBUG){
138 close *STDERR;
139 close *STDOUT;
140 #close *STDIN;
141 }
142
143 diag("Selenium running in $$") if $DEBUG;
144 $class->_start_selenium($args);
145 diag("Selenium server $$ going down") if $DEBUG;
146 exit 1;
4f6d213e 147 }
148 $sel_pid = $pid;
149}
150
374611f4 151# Moved out to be subclassable seperately to the fork logic
152sub _start_selenium {
153 my ($class, $arg) = @_;
154 $arg = '' unless defined $arg;
155 Alien::SeleniumRC::start($arg)
156 or croak "Can't start Selenium server";
157}
158
4f6d213e 159sub sel_pid {
160 return $sel_pid;
161}
162
163sub app_pid {
164 return $app_pid;
165}
166
167sub import {
374611f4 168 my ($class, $appname, %args) = @_;
169
4f6d213e 170 croak q{Specify your app's name} if !$appname;
171 $app = $appname;
172
173 my $d = $ENV{Catalyst::Utils::class2env($appname). "_DEBUG"}; # MYAPP_DEBUG
374611f4 174 if(defined $d){
175 $DEBUG = $d;
4f6d213e 176 }
374611f4 177
0744bdf6 178 unless ($args{-no_selenium_server}) {
374611f4 179 $class->_start_server($args{-selenium_args}) or croak "Couldn't start selenium server";
4f6d213e 180 }
4f6d213e 181 return 1;
182}
183
184sub start {
185 my $class = shift;
186 my $args = shift || {};
374611f4 187
188 my $port = delete $args->{port};
189 $port ||= $ENV{Catalyst::Utils::class2env($app). "_PORT"} # MYAPP_PORT
190 || 3000;
191
192 my $uri;
193
194 # Check for CATALYST_SERVER env var like TWMC does.
195 if ( $ENV{CATALYST_SERVER} ) {
196 $uri = $ENV{CATALYST_SERVER};
197 } elsif ( $args->{app_uri} ) {
198 $uri = delete $args->{app_uri}
199 } else {
200 # start a Catalyst MyApp server
201 eval("use $app");
202 croak "Couldn't load $app: $@" if $@;
203
204 my $pid;
205 if(0 == ($pid = fork())){
206 local $SIG{TERM} = sub {
207 diag("Catalyst server $$ going down (TERM)") if $DEBUG;
208 exit 0;
209 };
210 diag("Catalyst server running in $$") if $DEBUG;
211 $app->run($port, 'localhost');
212 exit 1;
213 }
214 $uri = 'http://localhost:' . $port;
215 $app_pid = $pid;
4f6d213e 216 }
4f6d213e 217
218 my $tries = 5;
219 my $error;
374611f4 220 my $sel_class = delete $args->{selenium_class} || 'Test::WWW::Selenium';
4f6d213e 221 my $sel;
374611f4 222
4f6d213e 223 while(!$sel && $tries--){
374611f4 224 sleep 1;
225 diag("Waiting for selenium server to start")
226 if $DEBUG;
227
228 eval {
229 $sel = $sel_class->new(
230 host => delete $args->{selenium_host} || 'localhost',
231 port => delete $args->{selenium_port} || 4444,
232 browser => '*firefox',
233 browser_url => $uri,
234 auto_stop => 0,
235 %$args
236 );
237 };
238 $error = $@;
4f6d213e 239 }
38cd3a5c 240 croak "Can't start selenium: $error" if $error;
4f6d213e 241
38cd3a5c 242 return $www_selenium = $sel;
4f6d213e 243}
244
245END {
246 if($sel_pid){
374611f4 247 if($www_selenium){
248 diag("Shutting down Selenium Server $sel_pid") if $DEBUG;
0744bdf6 249 $www_selenium->stop();
374611f4 250 $www_selenium->do_command('shutDown');
251 undef $www_selenium;
252 }
253 diag("Killing Selenium Server $sel_pid") if $DEBUG;
254 kill 15, $sel_pid or diag "Killing Selenium: $!";
255 undef $sel_pid;
8f552b3b 256
257 } elsif ($www_selenium) {
258 diag("Stopping Selenium Session $sel_pid") if $DEBUG;
259 $www_selenium->stop();
260 undef $www_selenium;
4f6d213e 261 }
8f552b3b 262
4f6d213e 263 if($app_pid){
374611f4 264 diag("Killing catalyst server $app_pid") if $DEBUG;
265 kill 15, $app_pid or diag "Killing MyApp: $!";
266 undef $app_pid;
4f6d213e 267 }
268 diag("Waiting for children to die") if $DEBUG;
269 waitpid $sel_pid, 0 if $sel_pid;
270 waitpid $app_pid, 0 if $app_pid;
271}
272
273
274=head1 ENVIRONMENT
275
276Debugging messages are shown if C<CATALYST_DEBUG> or C<MYAPP_DEBUG>
277are set. C<MYAPP> is the name of your application, uppercased. (This
278is the same syntax as Catalyst itself.)
279
374611f4 280C<CATALYST_SERVER> can be set to test against an externally running server,
281in a similar manner to how L<Test::WWW::Mechanize::Catalyst> behaves.
282
283The port that the application sever runs on can be affected by C<MYAPP_PORT>
284in addition to being specifiable in the arguments passed to start.
285
4f6d213e 286=head1 DIAGNOSTICS
287
288=head2 Specify your app's name
289
290You need to pass your Catalyst app's name as the argument to the use
291statement:
292
293 use Test::WWW::Selenium::Catalyst 'MyApp'
294
295C<MyApp> is the name of your Catalyst app.
296
1cdeb21f 297=head1 SEE ALSO
298
299=over 4
300
301=item *
302
8f552b3b 303Selenium website: L<http://seleniumhq.org/>
1cdeb21f 304
305=item *
306
307Description of what you can do with the C<$sel> object: L<Test::WWW::Selenium>
8f552b3b 308and L<WWW::Selenium>
1cdeb21f 309
310=item *
311
312If you don't need a real web browser: L<Test::WWW::Mechanize::Catalyst>
313
314=back
315
4f6d213e 316=head1 AUTHOR
317
8f552b3b 318Ash Berlin C<< <ash@cpan.org> >>
319
4f6d213e 320Jonathan Rockway, C<< <jrockway at cpan.org> >>
321
322=head1 BUGS
323
324Please report any bugs or feature requests to
325C<bug-test-www-selenium-catalyst at rt.cpan.org>, or through the web interface at
326L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-WWW-Selenium-Catalyst>.
327I will be notified, and then you'll automatically be notified of progress on
328your bug as I make changes.
329
cba20015 330=head1 PATCHES
4f6d213e 331
cba20015 332Send me unified diffs against the git HEAD at:
4f6d213e 333
8f552b3b 334 git://github.com/jrockway/test-www-selenium-catalyst.git
4f6d213e 335
cba20015 336You can view the repository online at
4f6d213e 337
8f552b3b 338 http://github.com/jrockway/test-www-selenium-catalyst/tree/master
4f6d213e 339
cba20015 340Thanks in advance for your contributions!
4f6d213e 341
342=head1 ACKNOWLEDGEMENTS
343
1cdeb21f 344Thanks for mst for getting on my case to actually write this thing :)
345
4f6d213e 346=head1 COPYRIGHT & LICENSE
347
8f552b3b 348Copyright 2009 Ash Berlin, all rights reserved.
349
4f6d213e 350Copyright 2006 Jonathan Rockway, all rights reserved.
351
352This program is free software; you can redistribute it and/or modify it
353under the same terms as Perl itself.
354
355=cut
356
3571; # End of Test::WWW::Selenium::Catalyst