1 package Test::WWW::Selenium::Catalyst;
7 use Test::WWW::Selenium;
11 BEGIN { $ENV{CATALYST_ENGINE} ||= 'HTTP'; }
13 local $SIG{CHLD} = 'IGNORE';
15 my $DEBUG = $ENV{CATALYST_DEBUG};
16 my $app; # app name (MyApp)
17 my $sel_pid; # pid of selenium server
18 my $app_pid; # pid of myapp server
22 Test::WWW::Selenium::Catalyst - Test your Catalyst application with Selenium
26 our $VERSION = '0.00_01';
28 =head1 DEVELOPER RELEASE
30 This is a developer release. It's working for me in production, but
31 it depends on a Java application (SeleniumRC), which can be
32 unreliable. On my Debian system, I had to put C<firefox-bin> in my
33 path, and add C</usr/lib/firefox> to C<LD_LIBRARY_PATH>. Every distro
34 and OS is different, so I'd like some feedback on how this works on
35 your system. I would like to find a clean solution that lets this
36 module "Just Work" for everyone, but I have a feeling that it's going
37 to look more like C<if(gentoo){ ... } elsif (debian) { ... }> and so
38 on. I can live with that, but I need your help to get to that stage!
40 Please report any problems to RT, the Catalyst mailing list, or the
41 #catalyst IRC channel on L<irc.perl.org>. Thanks!
45 Sometimes the Selenium server doesn't die after the tests when running
46 under C<make test>. C<prove> works fine, so until I (or you!) figure
47 out the problem, use C<prove>.
51 use Test::WWW::Selenium::Catalyst 'MyApp';
52 use Test::More tests => 2;
54 my $sel = Test::WWW::Selenium::Catalyst->start;
56 $sel->is_text_present_ok('Welcome to MyApp');
58 This module starts the SeleniumRC server and your Catalyst app so that
59 you can test it with SeleniumRC. Once you've called
60 C<Test::WWW::Selenium::Catalyst->start>, everything is just like
61 L<Test::WWW::Selenium|Test::WWW:Selenium>.
67 Starts the Selenium and Catalyst servers, and returns a
68 pre-initialized, ready-to-use Test::WWW::Selenium object.
70 [NOTE] The selenium server is actually started when you C<use> this
71 module, and it's killed when your test exits.
75 Returns the process ID of the Selenium Server.
79 Returns the process ID of the Catalyst server.
85 # fork off a selenium server
87 if(0 == ($pid = fork())){
88 local $SIG{TERM} = sub {
89 diag("Selenium server $$ going down (TERM)");
101 diag("Selenium running in $$") if $DEBUG;
102 Alien::SeleniumRC->start()
103 or croak "Can't start Selenium server";
104 diag("Selenium server $$ going down") if $DEBUG;
119 my ($class, $appname) = @_;
120 croak q{Specify your app's name} if !$appname;
123 my $d = $ENV{Catalyst::Utils::class2env($appname). "_DEBUG"}; # MYAPP_DEBUG
124 if(defined $d && $d){
127 elsif(defined $d && $d == 0){
130 # if it's something else, leave the CATALYST_DEBUG setting in tact
132 _start_server() or croak "Couldn't start selenium server";
138 my $args = shift || {};
140 # start a Catalyst MyApp server
142 croak "Couldn't load $app: $@" if $@;
145 if(0 == ($pid = fork())){
146 local $SIG{TERM} = sub {
147 diag("Catalyst server $$ going down (TERM)") if $DEBUG;
150 diag("Catalyst server running in $$") if $DEBUG;
151 $app->run('3000', 'localhost');
159 while(!$sel && $tries--){
161 diag("Waiting for selenium server to start")
165 $sel = Test::WWW::Selenium->
166 new(host => 'localhost',
168 browser => $args->{browser} || '*firefox',
169 browser_url => 'http://localhost:3000/'
176 or croak "Can't start selenium: $@ (previous error: $error)";
183 diag("Killing Selenium Server $sel_pid") if $DEBUG;
184 kill 15, $sel_pid or diag "Killing Selenium: $!";
188 diag("Killing catalyst server $app_pid") if $DEBUG;
189 kill 15, $app_pid or diag "Killing MyApp: $!";
192 diag("Waiting for children to die") if $DEBUG;
193 waitpid $sel_pid, 0 if $sel_pid;
194 waitpid $app_pid, 0 if $app_pid;
200 Debugging messages are shown if C<CATALYST_DEBUG> or C<MYAPP_DEBUG>
201 are set. C<MYAPP> is the name of your application, uppercased. (This
202 is the same syntax as Catalyst itself.)
206 =head2 Specify your app's name
208 You need to pass your Catalyst app's name as the argument to the use
211 use Test::WWW::Selenium::Catalyst 'MyApp'
213 C<MyApp> is the name of your Catalyst app.
221 Selenium website: L<http://www.openqa.org/>
225 Description of what you can do with the C<$sel> object: L<Test::WWW::Selenium>
229 If you don't need a real web browser: L<Test::WWW::Mechanize::Catalyst>
235 Jonathan Rockway, C<< <jrockway at cpan.org> >>
239 Please report any bugs or feature requests to
240 C<bug-test-www-selenium-catalyst at rt.cpan.org>, or through the web interface at
241 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-WWW-Selenium-Catalyst>.
242 I will be notified, and then you'll automatically be notified of progress on
243 your bug as I make changes.
247 You can find documentation for this module with the perldoc command.
249 perldoc Test::WWW::Selenium::Catalyst
251 You can also look for information at:
255 =item * AnnoCPAN: Annotated CPAN documentation
257 L<http://annocpan.org/dist/Test-WWW-Selenium-Catalyst>
261 L<http://cpanratings.perl.org/d/Test-WWW-Selenium-Catalyst>
263 =item * RT: CPAN's request tracker
265 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-WWW-Selenium-Catalyst>
269 L<http://search.cpan.org/dist/Test-WWW-Selenium-Catalyst>
273 =head1 ACKNOWLEDGEMENTS
275 Thanks for mst for getting on my case to actually write this thing :)
277 =head1 COPYRIGHT & LICENSE
279 Copyright 2006 Jonathan Rockway, all rights reserved.
281 This program is free software; you can redistribute it and/or modify it
282 under the same terms as Perl itself.
286 1; # End of Test::WWW::Selenium::Catalyst