Skip to content

Commit 88880d9

Browse files
committed
Fix # 419: Support mozilla full page screenshots
Original patch by hornschorsch
1 parent 0a4b993 commit 88880d9

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

lib/Selenium/Remote/Driver.pm

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,20 +2151,27 @@ sub _convert_to_webelement {
21512151
21522152
Description:
21532153
Get a screenshot of the current page as a base64 encoded image.
2154+
Optionally pass {'full' => 1} as argument to take a full screenshot and not
2155+
only the viewport. (Works only with firefox and geckodriver >= 0.24.0)
21542156
21552157
Output:
21562158
STRING - base64 encoded image
21572159
21582160
Usage:
21592161
print $driver->screenshot();
2162+
print $driver->screenshot({'full' => 1});
21602163
21612164
To conveniently write the screenshot to a file, see L</capture_screenshot>.
21622165
21632166
=cut
21642167

21652168
sub screenshot {
2166-
my ($self) = @_;
2167-
my $res = { 'command' => 'screenshot' };
2169+
my ($self, $params) = @_;
2170+
$params //= { full => 0 };
2171+
2172+
croak "Full page screenshot only supported on geckodriver" if $params->{full} && ( $self->{browser} ne 'firefox' );
2173+
2174+
my $res = { 'command' => $params->{'full'} == 1 ? 'mozScreenshotFull' : 'screenshot' };
21682175
return $self->_execute_command($res);
21692176
}
21702177

@@ -2173,22 +2180,25 @@ sub screenshot {
21732180
Description:
21742181
Capture a screenshot and save as a PNG to provided file name.
21752182
(The method is compatible with the WWW::Selenium method of the same name)
2183+
Optionally pass {'full' => 1} as second argument to take a full screenshot
2184+
and not only the viewport. (Works only with firefox and geckodriver >= 0.24.0)
21762185
21772186
Output:
21782187
TRUE - (Screenshot is written to file)
21792188
21802189
Usage:
21812190
$driver->capture_screenshot($filename);
2191+
$driver->capture_screenshot($filename, {'full' => 1});
21822192
21832193
=cut
21842194

21852195
sub capture_screenshot {
2186-
my ( $self, $filename ) = @_;
2196+
my ( $self, $filename, $params ) = @_;
21872197
croak '$filename is required' unless $filename;
21882198

21892199
open( my $fh, '>', $filename );
21902200
binmode $fh;
2191-
print $fh MIME::Base64::decode_base64( $self->screenshot() );
2201+
print $fh MIME::Base64::decode_base64( $self->screenshot($params) );
21922202
CORE::close $fh;
21932203
return 1;
21942204
}

lib/Selenium/Remote/Spec.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ POST session/:sessionId/alert/accept 1 acceptAlert
8383
GET session/:sessionId/alert/text 0 getAlertText Get Alert Text
8484
POST session/:sessionId/alert/text 1 sendKeysToPrompt Send Alert Text
8585
GET session/:sessionId/screenshot 0 screenshot Take Screenshot
86+
GET session/:sessionId/moz/screenshot/full 0 mozScreenshotFull Take Full Screenshot
8687
GET session/:sessionId/element/:id/screenshot 0 elementScreenshot Take Element Screenshot
8788
};
8889

0 commit comments

Comments
 (0)