Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ services:
- docker
sudo: required
perl:
- '5.32'
- '5.30'
- '5.28'
- '5.26'
Expand Down
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM perl:5.30
FROM perl:5.32

WORKDIR /root

RUN cpanm -n Test::Deep URI

Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -20,4 +20,3 @@ services:
- httpd
volumes:
- .:/root

17 changes: 15 additions & 2 deletions lib/WebDriver/Tiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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') }
Expand Down
64 changes: 64 additions & 0 deletions lib/WebDriver/Tiny.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions t/namespace.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ is_deeply [ sort keys %WebDriver::Tiny:: ], [ qw/
js
js_async
new
print
refresh
screenshot
status
Expand All @@ -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
Expand Down Expand Up @@ -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";
8 changes: 8 additions & 0 deletions t/print.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use lib 't';
use t '2';

$content = '{"value":""}';

$drv->print;

reqs_are [ [ POST => '/print' ] ], '$drv->print';
2 changes: 2 additions & 0 deletions xt/cookies.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ my $cookie = {
httpOnly => $JSON::PP::false,
name => 'foo',
path => '/',
sameSite => 'None',
secure => $JSON::PP::false,
value => 'bar',
};
Expand All @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions xt/print.t
Original file line number Diff line number Diff line change
@@ -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;