Browse Source

Add new capture_screenshot() method

This is a shortcut for a common use of screenshot(),
and also increases compatibility with WWW::Selenium.
Mark Stosberg 12 năm trước cách đây
mục cha
commit
c0f0cd07d2
3 tập tin đã thay đổi với 38 bổ sung8 xóa
  1. 6 0
      Changes
  2. 1 0
      dist.ini
  3. 31 8
      lib/Selenium/Remote/Driver.pm

+ 6 - 0
Changes

@@ -26,10 +26,16 @@ Revision history for Selenium-Remote-Driver
           get_body() Returns the text for the whole body.
           get_body() Returns the text for the whole body.
           get_path() Returns the path part of the current URL.
           get_path() Returns the path part of the current URL.
 
 
+        - New short method to save a screenshot to a file: $driver->capture_screenshot($filename).
+          The name was chosen for compatibility with a WWW::Selenium method of the same name.  
+
         [DOCUMENTATION]
         [DOCUMENTATION]
         - Updated ChangeLog to reflect that debug_on() and debug_off() methods were added in 0.16.
         - Updated ChangeLog to reflect that debug_on() and debug_off() methods were added in 0.16.
 
 
         [INTERNALS]
         [INTERNALS]
+        - MIME::Base64 is now only loaded if it used for a file upload or taking a screenshot.
+          This will create a minor speed-up for common cases which don't use these features.
+
         - Explained why the code does 'use 5.006; use v5.10.0;', and it's not pretty.
         - Explained why the code does 'use 5.006; use v5.10.0;', and it's not pretty.
            The short version: before perl 5.006, the v-string 'v5.10.0' would not be understood.
            The short version: before perl 5.006, the v-string 'v5.10.0' would not be understood.
            The page http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/
            The page http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/

+ 1 - 0
dist.ini

@@ -38,6 +38,7 @@ HTTP::Request = 0
 JSON = 0
 JSON = 0
 LWP::UserAgent = 0
 LWP::UserAgent = 0
 Net::Ping = 0
 Net::Ping = 0
+MIME::Base64 = 0
 
 
 [Prereqs / TestRequires]
 [Prereqs / TestRequires]
 Carp = 1.25
 Carp = 1.25

+ 31 - 8
lib/Selenium/Remote/Driver.pm

@@ -10,7 +10,6 @@ use 5.006; use v5.10.0;  # Before 5.006, v5.10.0 would not be understood.
 use Carp;
 use Carp;
 our @CARP_NOT;
 our @CARP_NOT;
 
 
-use MIME::Base64;
 use IO::Compress::Zip qw(zip $ZipError) ;
 use IO::Compress::Zip qw(zip $ZipError) ;
 use Scalar::Util;
 use Scalar::Util;
 use Selenium::Remote::RemoteConnection;
 use Selenium::Remote::RemoteConnection;
@@ -1112,13 +1111,8 @@ sub _convert_to_webelement {
 
 
  Usage:
  Usage:
     print $driver->screenshot();
     print $driver->screenshot();
- or
-    require MIME::Base64;
-    open(FH,'>','screenshot.png');
-    binmode FH;
-    my $png_base64 = $driver->screenshot();
-    print FH MIME::Base64::decode_base64($png_base64);
-    close FH;
+
+To conveniently write the screenshot to a file, see L<capture_screenshot()>.
 
 
 =cut
 =cut
 
 
@@ -1128,6 +1122,33 @@ sub screenshot {
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
+=head2 capture_screenshot
+
+ Description:
+    Capture a screenshot and save as a PNG to provided file name.
+    (The method is compatible with the WWW::Selenium method fo the same name)
+
+ Output:
+    TRUE - (Screenshot is written to file)
+
+ Usage:
+    $driver->capture_screenshot($filename);
+
+=cut
+
+sub capture_screenshot {
+    my ($self, $filename) = @_;
+    croak '$filename is required' unless $filename;
+
+    require MIME::Base64;
+    open(my $fh,'>',$filename);
+    binmode $fh;
+    print $fh MIME::Base64::decode_base64($self->screenshot());
+    CORE::close $fh;
+    return 1;
+}
+
+
 =head2 available_engines
 =head2 available_engines
 
 
  Description:
  Description:
@@ -1889,6 +1910,8 @@ sub upload_file {
     zip $filename => \$string
     zip $filename => \$string
         or die "zip failed: $ZipError\n";    # compress the file into string
         or die "zip failed: $ZipError\n";    # compress the file into string
     my $res = { 'command' => 'uploadFile' }; # /session/:SessionId/file
     my $res = { 'command' => 'uploadFile' }; # /session/:SessionId/file
+    require MIME::Base64;
+
     my $params = {
     my $params = {
         file => encode_base64($string)       # base64-encoded string
         file => encode_base64($string)       # base64-encoded string
     };
     };