Sfoglia il codice sorgente

Add inner_window_size sub and init hash key option

The optional hash key can be used to immediately resize your browser
window directly after instantiation of the browser.

The hash key calls out to the similarly named sub routine which opens a
new window, closes the old window, resizes the new one, and switches
handles appropriately.
Daniel Gempesaw 11 anni fa
parent
commit
13544ff7c8
2 ha cambiato i file con 79 aggiunte e 3 eliminazioni
  1. 68 3
      lib/Selenium/Remote/Driver.pm
  2. 11 0
      t/01-driver.t

+ 68 - 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,47 @@ 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;
+    $self->switch_to_window(shift $self->get_window_handles);
+
+    my @resize = (
+        'window.innerHeight = ' . $height,
+        'window.innerWidth  = ' . $width,
+        'return 1'
+    );
+
+    return $self->execute_script(join(';', @resize)) ? 1 : 0;
+}
 
 1;
 

+ 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;