Просмотр исходного кода

Merge remote-tracking branch 'phil-kania/master'

Gordon Child 14 лет назад
Родитель
Сommit
ce204dc437

+ 4 - 0
lib/Selenium/Remote/Commands.pm

@@ -209,6 +209,10 @@ sub new {
                'method' => 'POST',
                'url' => "session/:sessionId/element/:id/hover"
         },
+        'mouseMoveToLocation' => {
+               'method' => 'POST',
+               'url' => "session/:sessionId/moveto"
+        },
         #'setVisible' => {
         #               'method' => 'POST',
         #               'url' => "session/:sessionId/visible"

+ 59 - 9
lib/Selenium/Remote/Driver.pm

@@ -148,7 +148,8 @@ created when you use the find_* methods.
         'javascript' - <boolean> - whether javascript should be supported
         'auto_close' - <boolean> - whether driver should end session on remote
                                    server on close.
-        
+        'extra_capabilities' - HASH of extra capabilities
+
         If no values are provided, then these defaults will be assumed:
             'remote_server_addr' => 'localhost'
             'port'         => '4444'
@@ -165,13 +166,17 @@ created when you use the find_* methods.
     my $driver = new Selenium::Remote::Driver;
     or
     my $driver = new Selenium::Remote::Driver('browser_name' => 'firefox',
-                                              'platform' => 'MAC')
+                                              'platform' => 'MAC');
     or
     my $driver = new Selenium::Remote::Driver('remote_server_addr' => '10.10.1.1',
                                               'port' => '2222',
                                               auto_close => 0
-                                              )
-
+                                              );
+    or
+    my $driver = new Selenium::Remote::Driver('browser_name'       => 'chrome',
+                                              'platform'           => 'VISTA',
+                                              'extra_capabilities' => {'chrome.switches' => ["--user-data-dir=$ENV{LOCALAPPDATA}\\Google\\Chrome\\User Data"],},
+                                              );
 =cut
 
 sub new {
@@ -208,7 +213,7 @@ sub new {
     $self->{remote_conn} =
       new Selenium::Remote::RemoteConnection( $self->{remote_server_addr},
         $self->{port} );
-    $self->new_session();
+    $self->new_session(delete $args{extra_capabilities});
 
     if ( !( defined $self->{session_id} ) ) {
         croak "Could not establish a session with the remote server\n";
@@ -242,14 +247,16 @@ sub _execute_command {
 # A method that is used by the Driver itself. It'll be called to set the
 # desired capabilities on the server.
 sub new_session {
-    my $self = shift;
+    my ($self, $extra_capabilities) = @_;
+    $extra_capabilities ||= {};
     my $args = {
         'desiredCapabilities' => {
             'browserName'       => $self->{browser_name},
             'platform'          => $self->{platform},
             'javascriptEnabled' => $self->{javascript},
             'version'           => $self->{version},
-        }
+            %$extra_capabilities,
+        },
     };
     my $resp =
       $self->{remote_conn}
@@ -263,6 +270,34 @@ sub new_session {
     }
 }
 
+=head2 mouse_move_to_location
+
+ Description:
+    Move the mouse by an offset of the specificed element. If no
+    element is specified, the move is relative to the current mouse
+    cursor. If an element is provided but no offset, the mouse will be
+    moved to the center of the element. If the element is not visible,
+    it will be scrolled into view.
+
+ Output:
+    STRING - 
+
+ Usage:
+    # element - the element to move to. If not specified or is null, the offset is relative to current position of the mouse.
+    # xoffset - X offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
+    # yoffset - Y offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
+
+    print $driver->mouse_move_to_location(element => e, xoffset => x, yoffset => y);
+
+=cut
+
+sub mouse_move_to_location {
+    my ($self, %params) = @_;
+    $params{element} = $params{element}{id} if exists $params{element};
+    my $res = { 'command' => 'mouseMoveToLocation' };
+    return $self->_execute_command($res, \%params);
+}
+
 =head2 get_capabilities
 
  Description:
@@ -491,6 +526,21 @@ sub refresh {
     return $self->_execute_command($res);
 }
 
+=head2 javascript
+
+ Description:
+    returns true if javascript is enabled in the driver.
+
+ Usage:
+    if ($driver->javascript) { ...; }
+
+=cut
+
+sub javascript {
+    my $self = shift;
+    return $self->{javascript} == JSON::true;
+}
+
 =head2 execute_script
 
  Description:
@@ -529,11 +579,11 @@ sub execute_script {
             }
         }
         
-        my $params = {'args' => @args};
+        my $params = {'script' => $script, 'args' => [@args]};
         my $ret = $self->_execute_command($res, $params);
         
         # replace any ELEMENTS with WebElement
-        if (exists $ret->{'cmd_return'}->{'ELEMENT'}) {
+        if (ref($ret) and (ref($ret->{'cmd_return'}) eq 'HASH') and exists $ret->{'cmd_return'}->{'ELEMENT'}) {
             $ret->{'cmd_return'} =
                 new Selenium::Remote::WebElement(
                                         $ret->{'cmd_return'}->{ELEMENT}, $self);

+ 1 - 1
lib/Selenium/Remote/RemoteConnection.pm

@@ -76,7 +76,7 @@ sub _process_response {
     }
     else {
         my $decoded_json = undef; 
-        if ($response->message ne 'No Content') {
+        if (($response->message ne 'No Content') && ($response->content ne '')) {
             $decoded_json = $json->allow_nonref(1)->utf8(1)->decode($response->content);
             $data->{'sessionId'} = $decoded_json->{'sessionId'};
         }

+ 4 - 11
lib/Selenium/Remote/WebElement.pm

@@ -101,23 +101,16 @@ sub submit {
         {ARRAY | STRING} - Array of strings or a string.
 
  Usage:
-    my @arr_str = ['abcd'];
-    $elem->send_keys(@arr_str);
+    $elem->send_keys('abcd', 'efg');
+    $elem->send_keys('hijk');
 
 =cut
 
 sub send_keys {
-    my ($self, $string) = @_;
+    my ($self, @strings) = @_;
     my $res = { 'command' => 'sendKeysToElement', 'id' => $self->{id} };
-    my @arr_str;
-    if (ref $string ne 'ARRAY') {
-        $arr_str[0] = $string;
-    }
-    else {
-        @arr_str = $string;
-    }
     my $params = {
-        'value' => @arr_str
+        'value' => \@strings,
     };
     return $driver->_execute_command($res, $params);
 }