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
23 Test::WWW::Selenium::Catalyst - Test your Catalyst application with Selenium
27 our $VERSION = '0.01';
29 =head1 DEVELOPERISH RELEASE
31 This is still a test release. It's working for me in production, but
32 it depends on a Java application (SeleniumRC), which can be
33 unreliable. On my Debian system, I had to put C<firefox-bin> in my
34 path, and add C</usr/lib/firefox> to C<LD_LIBRARY_PATH>. Every distro
35 and OS is different, so I'd like some feedback on how this works on
36 your system. I would like to find a clean solution that lets this
37 module "Just Work" for everyone, but I have a feeling that it's going
38 to look more like C<if(gentoo){ ... } elsif (debian) { ... }> and so
39 on. I can live with that, but I need your help to get to that stage!
41 Please report any problems to RT, the Catalyst mailing list, or the
42 #catalyst IRC channel on L<irc.perl.org>. Thanks!
46 use Test::WWW::Selenium::Catalyst 'MyApp';
47 use Test::More tests => 2;
49 my $sel = Test::WWW::Selenium::Catalyst->start;
51 $sel->is_text_present_ok('Welcome to MyApp');
53 This module starts the SeleniumRC server and your Catalyst app so that
54 you can test it with SeleniumRC. Once you've called
55 C<< Test::WWW::Selenium::Catalyst->start >>, everything is just like
56 L<Test::WWW::Selenium|Test::WWW:Selenium>.
62 Starts the Selenium and Catalyst servers, and returns a
63 pre-initialized, ready-to-use Test::WWW::Selenium object.
65 [NOTE] The selenium server is actually started when you C<use> this
66 module, and it's killed when your test exits.
70 Returns the process ID of the Selenium Server.
74 Returns the process ID of the Catalyst server.
80 # fork off a selenium server
82 if(0 == ($pid = fork())){
83 local $SIG{TERM} = sub {
84 diag("Selenium server $$ going down (TERM)") if $DEBUG;
96 diag("Selenium running in $$") if $DEBUG;
97 Alien::SeleniumRC::start()
98 or croak "Can't start Selenium server";
99 diag("Selenium server $$ going down") if $DEBUG;
114 my ($class, $appname) = @_;
115 croak q{Specify your app's name} if !$appname;
118 my $d = $ENV{Catalyst::Utils::class2env($appname). "_DEBUG"}; # MYAPP_DEBUG
119 if(defined $d && $d){
122 elsif(defined $d && $d == 0){
125 # if it's something else, leave the CATALYST_DEBUG setting in tact
127 _start_server() or croak "Couldn't start selenium server";
133 my $args = shift || {};
135 # start a Catalyst MyApp server
137 croak "Couldn't load $app: $@" if $@;
140 if(0 == ($pid = fork())){
141 local $SIG{TERM} = sub {
142 diag("Catalyst server $$ going down (TERM)") if $DEBUG;
145 diag("Catalyst server running in $$") if $DEBUG;
146 $app->run('3000', 'localhost');
154 while(!$sel && $tries--){
156 diag("Waiting for selenium server to start")
160 $sel = Test::WWW::Selenium->
161 new(host => 'localhost',
163 browser => $args->{browser} || '*firefox',
164 browser_url => 'http://localhost:3000/',
170 croak "Can't start selenium: $error" if $error;
172 return $www_selenium = $sel;
177 diag("Shutting down Selenium Server $sel_pid") if $DEBUG;
178 $www_selenium->do_command('shutDown');
182 diag("Killing Selenium Server $sel_pid") if $DEBUG;
183 kill 15, $sel_pid or diag "Killing Selenium: $!";
187 diag("Killing catalyst server $app_pid") if $DEBUG;
188 kill 15, $app_pid or diag "Killing MyApp: $!";
191 diag("Waiting for children to die") if $DEBUG;
192 waitpid $sel_pid, 0 if $sel_pid;
193 waitpid $app_pid, 0 if $app_pid;
199 Debugging messages are shown if C<CATALYST_DEBUG> or C<MYAPP_DEBUG>
200 are set. C<MYAPP> is the name of your application, uppercased. (This
201 is the same syntax as Catalyst itself.)
205 =head2 Specify your app's name
207 You need to pass your Catalyst app's name as the argument to the use
210 use Test::WWW::Selenium::Catalyst 'MyApp'
212 C<MyApp> is the name of your Catalyst app.
220 Selenium website: L<http://www.openqa.org/>
224 Description of what you can do with the C<$sel> object: L<Test::WWW::Selenium>
228 If you don't need a real web browser: L<Test::WWW::Mechanize::Catalyst>
234 Jonathan Rockway, C<< <jrockway at cpan.org> >>
238 Please report any bugs or feature requests to
239 C<bug-test-www-selenium-catalyst at rt.cpan.org>, or through the web interface at
240 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-WWW-Selenium-Catalyst>.
241 I will be notified, and then you'll automatically be notified of progress on
242 your bug as I make changes.
246 Send me unified diffs against the git HEAD at:
248 git://git.jrock.us/Test-WWW-Selenium-Catalyst
250 You can view the repository online at
252 http://git.jrock.us/?p=Test-WWW-Selenium-Catalyst.git;a=summary
254 Thanks in advance for your contributions!
256 =head1 ACKNOWLEDGEMENTS
258 Thanks for mst for getting on my case to actually write this thing :)
260 =head1 COPYRIGHT & LICENSE
262 Copyright 2006 Jonathan Rockway, all rights reserved.
264 This program is free software; you can redistribute it and/or modify it
265 under the same terms as Perl itself.
269 1; # End of Test::WWW::Selenium::Catalyst