Ver Fonte

Fix # 419: Support mozilla full page screenshots

Original patch by hornschorsch
George S. Baugh há 6 anos atrás
pai
commit
88880d90e8
2 ficheiros alterados com 15 adições e 4 exclusões
  1. 14 4
      lib/Selenium/Remote/Driver.pm
  2. 1 0
      lib/Selenium/Remote/Spec.pm

+ 14 - 4
lib/Selenium/Remote/Driver.pm

@@ -2151,20 +2151,27 @@ sub _convert_to_webelement {
 
  Description:
     Get a screenshot of the current page as a base64 encoded image.
+    Optionally pass {'full' => 1} as argument to take a full screenshot and not
+    only the viewport. (Works only with firefox and geckodriver >= 0.24.0)
 
  Output:
     STRING - base64 encoded image
 
  Usage:
     print $driver->screenshot();
+    print $driver->screenshot({'full' => 1});
 
 To conveniently write the screenshot to a file, see L</capture_screenshot>.
 
 =cut
 
 sub screenshot {
-    my ($self) = @_;
-    my $res = { 'command' => 'screenshot' };
+    my ($self, $params) = @_;
+    $params //= { full => 0 };
+
+    croak "Full page screenshot only supported on geckodriver" if $params->{full} && ( $self->{browser} ne 'firefox' );
+
+    my $res = { 'command' => $params->{'full'} == 1 ? 'mozScreenshotFull' : 'screenshot' };
     return $self->_execute_command($res);
 }
 
@@ -2173,22 +2180,25 @@ sub screenshot {
  Description:
     Capture a screenshot and save as a PNG to provided file name.
     (The method is compatible with the WWW::Selenium method of the same name)
+    Optionally pass {'full' => 1} as second argument to take a full screenshot
+    and not only the viewport. (Works only with firefox and geckodriver >= 0.24.0)
 
  Output:
     TRUE - (Screenshot is written to file)
 
  Usage:
     $driver->capture_screenshot($filename);
+    $driver->capture_screenshot($filename, {'full' => 1});
 
 =cut
 
 sub capture_screenshot {
-    my ( $self, $filename ) = @_;
+    my ( $self, $filename, $params ) = @_;
     croak '$filename is required' unless $filename;
 
     open( my $fh, '>', $filename );
     binmode $fh;
-    print $fh MIME::Base64::decode_base64( $self->screenshot() );
+    print $fh MIME::Base64::decode_base64( $self->screenshot($params) );
     CORE::close $fh;
     return 1;
 }

+ 1 - 0
lib/Selenium/Remote/Spec.pm

@@ -83,6 +83,7 @@ POST    session/:sessionId/alert/accept                      1 acceptAlert
 GET     session/:sessionId/alert/text                        0 getAlertText                 Get Alert Text
 POST    session/:sessionId/alert/text                        1 sendKeysToPrompt             Send Alert Text
 GET     session/:sessionId/screenshot                        0 screenshot                   Take Screenshot
+GET     session/:sessionId/moz/screenshot/full               0 mozScreenshotFull            Take Full Screenshot
 GET     session/:sessionId/element/:id/screenshot            0 elementScreenshot            Take Element Screenshot
 };