blib/lib/Test/WWW/WebKit/Catalyst.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 76 CC 6 SCAR 26.6
CC
6
Cov
0%
start_catalyst_server
30.0
test_port
6.0
load_application
2.0
CRAP 14.3
1 package Test::WWW::WebKit::Catalyst;
2  
3 =head1 NAME
4  
5 Test::WWW::WebKit::Catalyst - Perl extension for using an embedding WebKit engine for Catalyst tests
6  
7 =head1 SYNOPSIS
8  
9 use Test::WWW::WebKit::Catalyst;
10  
11 my $webkit = Test::WWW::WebKit::Catalyst->new(app => 'TestApp', xvfb => 1);
12 $webkit->init;
13  
14 $webkit->open_ok("http://localhost:$ENV{CATALYST_PORT}/index");
15 $webkit->type_ok("q", "hello world");
16 $webkit->click_ok("xpath=//button");
17 $webkit->wait_for_page_to_load_ok(5000);
18 $webkit->title_is("foo");
19  
20 =head1 DESCRIPTION
21  
22 Test::WWW::WebKit::Catalyst is a drop-in replacement for Test::WWW::Selenium::Catalyst using Gtk3::WebKit as browser instead of relying on an external Java server and an installed browser.
23  
24 =head2 EXPORT
25  
26 None by default.
27  
28 =cut
29  
30 use 5.10.0;
31 use Moose;
32 use IO::Socket::INET;
33 use Catalyst::EngineLoader;
34  
35 extends 'Test::WWW::WebKit' => { -version => 0.03 };
36  
37 our $VERSION = '0.02';
38  
39 has app => (
40 is => 'ro',
41 isa => 'ClassName',
42 required => 1,
43 );
44  
45 has server_pid => (
46 is => 'rw',
47 isa => 'Int',
48 );
49  
50 has server => (
51 is => 'rw',
52 );
53  
54 before DESTROY => sub {
55 my ($self) = @_;
56 return unless $self->server_pid;
57  
58 local $SIG{PIPE} = 'IGNORE';
59 kill 15, $self->server_pid;
60 close $self->server;
61 };
62  
63 sub test_port {
64 my ($port) = @_;
65 return IO::Socket::INET->new(
66 Listen => 5,
67 Proto => 'tcp',
68 Reuse => 1,
69 LocalPort => $port
70 ) ? 1 : 0;
71 }
72  
73 sub start_catalyst_server {
74 my ($self) = @_;
75  
76 my $pid;
77 if (my $pid = open my $server, '-|') {
78 $self->server_pid($pid);
79 $self->server($server);
80 my $port = <$server>;
81 chomp $port;
82 return $port;
83 }
84 else {
85 local $SIG{TERM} = sub {
86 exit 0;
87 };
88  
89 my ($port, $catalyst);
90 while (1) {
91 $port = 1024 + int(rand(65535 - 1024));
92 next unless test_port($port);
93  
94 my $loader = Catalyst::EngineLoader->new(application_name => $self->app);
95 eval {
96 $catalyst = $self->load_application($loader, $port);
97 };
98 warn $@ if $@;
99 last unless $@;
100 warn "retrying...";
101 }
102 say $port;
103 $self->app->run($port, 'localhost', $catalyst);
104  
105 exit 1;
106 }
107 }
108  
109 sub load_application {
110 my ($self, $loader, $port) = @_;
111  
112 return $loader->auto(port => $port, host => 'localhost');
113 }
114  
115 before init => sub {
116 my ($self) = @_;
117  
118 $ENV{CATALYST_PORT} = $self->start_catalyst_server;
119 };
120  
121 1;
122  
123 =head1 SEE ALSO
124  
125 L<WWW::Selenium> and L<Test::WWW::Selenium> for the base packages.
126 See L<Test::WWW::Selenium> for API documentation.
127  
128 =head1 AUTHOR
129  
130 Stefan Seifert, E<lt>nine@cpan.orgE<gt>
131  
132 =head1 COPYRIGHT AND LICENSE
133  
134 Copyright (C) 2011 by Stefan Seifert
135  
136 This library is free software; you can redistribute it and/or modify
137 it under the same terms as Perl itself, either Perl version 5.12.3 or,
138 at your option, any later version of Perl 5 you may have available.
139  
140 =cut