diff --git a/.travis.yml b/.travis.yml index ea4f85a..eebbc06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ services: - docker sudo: required perl: + - '5.32' - '5.30' - '5.28' - '5.26' diff --git a/Changes b/Changes index e993990..e73bb46 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ {{ $NEXT }} + - Add $drv->print for PDF printing + 0.103 2020-06-21 18:49:25+01:00 Europe/London - Remove documentation for methods removed previously. diff --git a/Dockerfile b/Dockerfile index 78d14e8..d47ca54 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ -FROM perl:5.30 +FROM perl:5.32 + +WORKDIR /root RUN cpanm -n Test::Deep URI diff --git a/docker-compose.yml b/docker-compose.yml index fb6075d..af030e8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '2' services: geckodriver: - image: quay.io/cvlibrary/geckodriver:0.24.0 + image: quay.io/cvlibrary/geckodriver:0.28.0 logging: driver: none httpd: @@ -20,4 +20,3 @@ services: - httpd volumes: - .:/root - diff --git a/lib/WebDriver/Tiny.pm b/lib/WebDriver/Tiny.pm index d108677..e81e315 100644 --- a/lib/WebDriver/Tiny.pm +++ b/lib/WebDriver/Tiny.pm @@ -228,11 +228,11 @@ sub get($self, $url) { $self; } -sub screenshot($self, $file = undef) { +my $mime_data = sub ($method, $path, $self, $file, %args) { require MIME::Base64; my $data = MIME::Base64::decode_base64( - $self->_req( GET => '/screenshot' ) + $self->_req( $method, $path, %args ? \%args : () ) ); if ( defined $file ) { @@ -244,6 +244,19 @@ sub screenshot($self, $file = undef) { } $data; +}; + +sub print { + my $self = shift; + unshift @_, undef unless @_ % 2; + unshift @_, 'POST' => '/print', $self; + goto $mime_data; +} + +sub screenshot { + push @_, undef if @_ == 1; + unshift @_, 'GET' => '/screenshot'; + goto $mime_data; } sub user_agent($self) { $js->( '/execute/sync', $self, 'return window.navigator.userAgent') } diff --git a/lib/WebDriver/Tiny.pod b/lib/WebDriver/Tiny.pod index fa3fed2..a488976 100644 --- a/lib/WebDriver/Tiny.pod +++ b/lib/WebDriver/Tiny.pod @@ -403,6 +403,70 @@ Get the URL of the current page. =head2 Window Management +=head3 print + + my $pdf_blob = $drv->print(%args); + + $drv->print( '/tmp/foo.pdf', %args ); + +Accepts an optional path to store the resulting PDF, and zero or more of the +following key/value options: + +=over + +=item orientation + +The page orientation to use when printing. Possible values are 'portrait' and +'landscape'. Defaults to 'portrait'. + +=item scale + +Page scale. Defaults to 1; + +=item background + +Whether background images should be included in the output. Defaults to false. + +=item width + +Page width in centimetres. Defaults to 21.59. + +=item height + +Page height in centimetres. Defaults to 27.94. + +=item top + +Page margin in centimetres from the top. Defaults to 1. + +=item bottom + +Page margin in centimetres from the bottom. Defaults to 1. + +=item left + +Page margin in centimetres from the left. Defaults to 1. + +=item right + +Page margin in centimetres from the right. Defaults to 1. + +=item shrinkToFit + +Whether the PDF should be shrunk to fit the page. Defaults to true. + +Setting this overrides any specified page width. + +=item pageRanges + +The range of pages to print, either as an array or as a string with the lower +and upper bounds separated by a hyphen. When using the string format, one of +the ends might be left blank to indicate that there is no such bound, as in +C<-5> to print the first five pages, or C<5-> to print from the 5th page +onwards. + +=back + =head3 screenshot my $png_blob = $drv->screenshot; diff --git a/t/namespace.t b/t/namespace.t index 6e1ba77..0019794 100644 --- a/t/namespace.t +++ b/t/namespace.t @@ -32,6 +32,7 @@ is_deeply [ sort keys %WebDriver::Tiny:: ], [ qw/ js js_async new + print refresh screenshot status @@ -46,7 +47,7 @@ is_deeply [ sort keys %WebDriver::Tiny:: ], [ qw/ window_rect window_switch windows -/ ], "WebDriver::Tiny has the correct stuff in it's namespace"; +/ ], "WebDriver::Tiny has the correct stuff in its namespace"; is_deeply [ sort keys %WebDriver::Tiny::Elements:: ], [ qw/ BEGIN @@ -76,4 +77,4 @@ is_deeply [ sort keys %WebDriver::Tiny::Elements:: ], [ qw/ text uniq visible -/ ], "WebDriver::Tiny::Elements has the correct stuff in it's namespace"; +/ ], "WebDriver::Tiny::Elements has the correct stuff in its namespace"; diff --git a/t/print.t b/t/print.t new file mode 100644 index 0000000..e137774 --- /dev/null +++ b/t/print.t @@ -0,0 +1,8 @@ +use lib 't'; +use t '2'; + +$content = '{"value":""}'; + +$drv->print; + +reqs_are [ [ POST => '/print' ] ], '$drv->print'; diff --git a/xt/cookies.t b/xt/cookies.t index c27a1e2..8a061d3 100644 --- a/xt/cookies.t +++ b/xt/cookies.t @@ -25,6 +25,7 @@ my $cookie = { httpOnly => $JSON::PP::false, name => 'foo', path => '/', + sameSite => 'None', secure => $JSON::PP::false, value => 'bar', }; @@ -34,6 +35,7 @@ is_deeply $drv->cookie('foo'), $cookie, 'Cookie "foo" exists'; is_deeply $drv->cookies, { foo => $cookie, baz => { + sameSite => 'None', domain => 'httpd', expiry => $expiry, httpOnly => $JSON::PP::false, diff --git a/xt/print.t b/xt/print.t new file mode 100644 index 0000000..fde3440 --- /dev/null +++ b/xt/print.t @@ -0,0 +1,28 @@ +use strict; +use warnings; + +use File::Temp; +use Test::More; +use WebDriver::Tiny; + +my $drv = WebDriver::Tiny->new( + capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, + host => 'geckodriver', + port => 4444, +); + +$drv->get('http://httpd'); + +my $pdf = $drv->print; + +is substr( $pdf, 0, 5 ), '%PDF-', 'output looks like a PDF'; + +my $file = File::Temp->new; + +$drv->print( $file->filename ); + +local ( @ARGV, $/ ) = $file->filename; + +is <>, $pdf, 'print("file") matches PDF'; + +done_testing;