Ver Fonte

Merge branch 'inner_window_size'

Daniel Gempesaw há 11 anos atrás
pai
commit
7e78ddcad0

+ 69 - 3
lib/Selenium/Remote/Driver.pm

@@ -143,11 +143,11 @@ available here.
     are also optional.
 
         'auto_close'           - <boolean>  - whether driver should end session on remote server on close.
+        'base_url'             - <string>   - OPTIONAL, base url for the website Selenium acts on. This can save you from repeating the domain in every call to $driver->get()
         'default_finder'       - <string>   - choose default finder used for find_element* {class|class_name|css|id|link|link_text|name|partial_link_text|tag_name|xpath}
+        'inner_window_size'    - <aref[Int]>- An array ref [ height, width ] that the browser window should use as its initial size immediately after instantiation
         'webelement_class'     - <string>   - sub-class of Selenium::Remote::WebElement if you wish to use an alternate WebElement class.
         'ua'                   - LWP::UserAgent instance - if you wish to use a specific $ua, like from Test::LWP::UserAgent
-        'base_url'             - <string>   - OPTIONAL, base url for the website Selenium acts on. This can save you from repeating the domain in every call to $driver->get()
-
 
     If no values are provided, then these defaults will be assumed:
         'remote_server_addr' => 'localhost'
@@ -395,6 +395,26 @@ has 'desired_capabilities' => (
     predicate => 'has_desired_capabilities'
 );
 
+has 'inner_window_size' => (
+    is        => 'rw',
+    lazy      => 1,
+    predicate => 1,
+    coerce    => sub {
+        my $size = shift;
+
+        croak "inner_window_size must have two elements: [ height, width ]"
+          unless scalar @$size == 2;
+
+        foreach my $dim (@$size) {
+            croak 'inner_window_size only accepts integers, not: ' . $dim
+              unless Scalar::Util::looks_like_number($dim);
+        }
+
+        return $size;
+    },
+
+);
+
 has 'testing' => (
     is => 'rw',
     default => sub { 0 },
@@ -417,6 +437,10 @@ sub BUILD {
         if ( !( defined $self->session_id ) ) {
             croak "Could not establish a session with the remote server\n";
         }
+        elsif ($self->has_inner_window_size) {
+            my $size = $self->inner_window_size;
+            $self->set_inner_window_size(@$size);
+        }
     }
 }
 
@@ -503,7 +527,7 @@ sub new_session {
     }
 
     if ($args->{desiredCapabilities}->{browserName} =~ /firefox/i
-        && $self->has_firefox_profile) {
+          && $self->has_firefox_profile) {
         $args->{desiredCapabilities}->{firefox_profile} = $self->firefox_profile;
     }
 
@@ -2226,6 +2250,48 @@ sub get_path {
     return $location;
 }
 
+=head2 set_inner_window_size
+
+ Description:
+
+     Set the inner window size by closing the current window and
+     reopening the current page in a new window. This can be useful
+     when using browsers to mock as mobile devices.
+
+     This sub will be fired automatically if you set the
+     C<inner_window_size> hash key option during instantiation.
+
+ Input:
+     INT - height of the window
+     INT - width of the window
+
+ Output:
+     BOOLEAN - Success or failure
+
+ Usage:
+     $driver->set_inner_window_size(640, 480)
+
+=cut
+
+sub set_inner_window_size {
+    my $self = shift;
+    my $height = shift;
+    my $width = shift;
+    my $location = $self->get_current_url;
+
+    $self->execute_script('window.open("' . $location . '", "_blank")');
+    $self->close;
+    my @handles = @{ $self->get_window_handles };
+    $self->switch_to_window(pop @handles);
+
+    my @resize = (
+        'window.innerHeight = ' . $height,
+        'window.innerWidth  = ' . $width,
+        'return 1'
+    );
+
+    return $self->execute_script(join(';', @resize)) ? 1 : 0;
+}
 
 1;
 

+ 1 - 0
lib/Selenium/Remote/Driver/Firefox/Profile.pm

@@ -249,4 +249,5 @@ __END__
 
 =head1 SEE ALSO
 
+http://kb.mozillazine.org/About:config_entries
 https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/A_brief_guide_to_Mozilla_preferences

+ 11 - 0
t/01-driver.t

@@ -354,6 +354,17 @@ AUTO_CLOSE: {
     $driver->auto_close(1);
 }
 
+INNER_WINDOW_SIZE: {
+    my $normal = Selenium::Remote::Driver->new->get_window_size;
+
+    my $resized = Selenium::Remote::Driver->new(
+        inner_window_size => [ 640, 480 ]
+    )->get_window_size;
+
+    ok($normal->{height} != $resized->{height}, 'inner window size: height is immediately changed');
+    ok($normal->{width} != $resized->{width}, 'inner window size: width is immediately changed');
+}
+
 BASE_URL: {
     {
         package MySeleniumRemoteDriver;

+ 21 - 12
t/bin/record.pl

@@ -2,40 +2,49 @@
 
 use strict;
 use warnings;
+use Cwd qw/abs_path/;
 
-unless (-d "t" && -f "dist.ini" && -f "t/01-driver.t" && -f "t/02-webelement.t") {
-    die "Please run this from the root of the repo.";
-}
+my $this_file = abs_path(__FILE__);
+my $srd_folder = $this_file;
+$srd_folder =~ s/t\/bin\/record\.pl//;
 
 resetEnv();
 startServer();
 
 print 'Cleaning...and building...
 ';
-print `dzil clean`;
-print `dzil build`;
+print `cd $srd_folder && dzil build`;
 
 if ($^O eq 'linux') {
     print "Headless and need a webdriver server started? Try\n\n\tDISPLAY=:1 xvfb-run --auto-servernum java -jar /usr/lib/node_modules/protractor/selenium/selenium-server-standalone-2.42.2.jar\n\n";
 }
 
-my $srdLib = glob('Selenium-Remote-Driver*/lib');
-my @files = (
+my @files = map {
+    $srd_folder . $_
+} (
     't/01-driver.t',
     't/02-webelement.t',
     't/Firefox-Profile.t'
 );
 
+my $srdLib = glob($srd_folder . 'Selenium-Remote-Driver*/lib');
+my $tLib = glob($srd_folder . 'Selenium-Remote-Driver*');
+my $executeTests = join( ' && ', map {
+    'perl -I' . $srdLib
+      . ' -I' . $tLib
+      . ' ' . $_
+  } @files);
+
 my $export = $^O eq 'MSWin32' ? 'set' : 'export';
-my $executeTests = join( ' && ', map { 'perl -I' . $srdLib . ' ' . $_ } @files);
 print `$export WD_MOCKING_RECORD=1 && $executeTests`;
 resetEnv();
 
 sub startServer {
     if ($^O eq 'MSWin32') {
-        system("start \"TEMP_HTTP_SERVER\" /MIN perl t/http-server.pl");
-    } else {
-        system("perl t/http-server.pl > /dev/null &");
+        system('start "TEMP_HTTP_SERVER" /MIN perl ' . $srd_folder . 't/http-server.pl');
+    }
+    else {
+        system('perl ' . $srd_folder . 't/http-server.pl > /dev/null &');
     }
 }
 
@@ -49,6 +58,6 @@ sub killServer {
 }
 
 sub resetEnv {
-    `dzil clean`;
+    `cd $srd_folder && dzil clean`;
     killServer();
 }

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
t/mock-recordings/01-driver-mock-MSWin32.json


Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
t/mock-recordings/01-driver-mock-darwin.json


Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
t/mock-recordings/01-driver-mock-linux.json


Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff