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 use Test::WWW::Selenium::Catalyst 'MyApp';
46 use Test::More tests => 2;
48 my $sel = Test::WWW::Selenium::Catalyst->start;
50 $sel->is_text_present_ok('Welcome to MyApp');
52 This module starts the SeleniumRC server and your Catalyst app so that
53 you can test it with SeleniumRC. Once you've called
54 C<< Test::WWW::Selenium::Catalyst->start >>, everything is just like
55 L<Test::WWW::Selenium|Test::WWW:Selenium>.
61 Starts the Selenium and Catalyst servers, and returns a
62 pre-initialized, ready-to-use Test::WWW::Selenium object.
64 [NOTE] The selenium server is actually started when you C<use> this
65 module, and it's killed when your test exits.
69 Returns the process ID of the Selenium Server.
73 Returns the process ID of the Catalyst server.
79 # fork off a selenium server
81 if(0 == ($pid = fork())){
82 local $SIG{TERM} = sub {
83 diag("Selenium server $$ going down (TERM)") if $DEBUG;
95 diag("Selenium running in $$") if $DEBUG;
96 Alien::SeleniumRC->start()
97 or croak "Can't start Selenium server";
98 diag("Selenium server $$ going down") if $DEBUG;
113 my ($class, $appname) = @_;
114 croak q{Specify your app's name} if !$appname;
117 my $d = $ENV{Catalyst::Utils::class2env($appname). "_DEBUG"}; # MYAPP_DEBUG
118 if(defined $d && $d){
121 elsif(defined $d && $d == 0){
124 # if it's something else, leave the CATALYST_DEBUG setting in tact
126 _start_server() or croak "Couldn't start selenium server";
132 my $args = shift || {};
134 # start a Catalyst MyApp server
136 croak "Couldn't load $app: $@" if $@;
139 if(0 == ($pid = fork())){
140 local $SIG{TERM} = sub {
141 diag("Catalyst server $$ going down (TERM)") if $DEBUG;
144 diag("Catalyst server running in $$") if $DEBUG;
145 $app->run('3000', 'localhost');
153 while(!$sel && $tries--){
155 diag("Waiting for selenium server to start")
159 $sel = Test::WWW::Selenium->
160 new(host => 'localhost',
162 browser => $args->{browser} || '*firefox',
163 browser_url => 'http://localhost:3000/'
170 or croak "Can't start selenium: $@ (previous error: $error)";
177 diag("Killing Selenium Server $sel_pid") if $DEBUG;
178 kill 15, $sel_pid or diag "Killing Selenium: $!";
182 diag("Killing catalyst server $app_pid") if $DEBUG;
183 kill 15, $app_pid or diag "Killing MyApp: $!";
186 diag("Waiting for children to die") if $DEBUG;
187 waitpid $sel_pid, 0 if $sel_pid;
188 waitpid $app_pid, 0 if $app_pid;
194 Debugging messages are shown if C<CATALYST_DEBUG> or C<MYAPP_DEBUG>
195 are set. C<MYAPP> is the name of your application, uppercased. (This
196 is the same syntax as Catalyst itself.)
200 =head2 Specify your app's name
202 You need to pass your Catalyst app's name as the argument to the use
205 use Test::WWW::Selenium::Catalyst 'MyApp'
207 C<MyApp> is the name of your Catalyst app.
215 Selenium website: L<http://www.openqa.org/>
219 Description of what you can do with the C<$sel> object: L<Test::WWW::Selenium>
223 If you don't need a real web browser: L<Test::WWW::Mechanize::Catalyst>
229 Jonathan Rockway, C<< <jrockway at cpan.org> >>
233 Please report any bugs or feature requests to
234 C<bug-test-www-selenium-catalyst at rt.cpan.org>, or through the web interface at
235 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-WWW-Selenium-Catalyst>.
236 I will be notified, and then you'll automatically be notified of progress on
237 your bug as I make changes.
241 You can find documentation for this module with the perldoc command.
243 perldoc Test::WWW::Selenium::Catalyst
245 You can also look for information at:
249 =item * AnnoCPAN: Annotated CPAN documentation
251 L<http://annocpan.org/dist/Test-WWW-Selenium-Catalyst>
255 L<http://cpanratings.perl.org/d/Test-WWW-Selenium-Catalyst>
257 =item * RT: CPAN's request tracker
259 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-WWW-Selenium-Catalyst>
263 L<http://search.cpan.org/dist/Test-WWW-Selenium-Catalyst>
267 =head1 ACKNOWLEDGEMENTS
269 Thanks for mst for getting on my case to actually write this thing :)
271 =head1 COPYRIGHT & LICENSE
273 Copyright 2006 Jonathan Rockway, all rights reserved.
275 This program is free software; you can redistribute it and/or modify it
276 under the same terms as Perl itself.
280 1; # End of Test::WWW::Selenium::Catalyst