Prechádzať zdrojové kódy

Add geolocation endpoints, although they're not widely implemented

I had trouble getting `set_geolocation` and `get_geolocation` to work
against any of the desktop browsers - I tried in Firefox, Chrome, and
Firefox with a few geolocation permissions tweaked in the
preferences. That's not too surprising, as they're really more useful on
mobile devices. Nevertheless, they're in now for use in Appium if
needed, or to be ready whenever the drivers end up implementing them.

For #113.
Daniel Gempesaw 11 rokov pred
rodič
commit
1f672d0542

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

@@ -364,6 +364,16 @@ has '_cmds' => (
                 'url'                => 'session/:sessionId/application_cache/status',
                 'no_content_success' => 0
             },
+            'setGeolocation' => {
+                'method'             => 'POST',
+                'url'                => "session/:sessionId/location",
+                'no_content_success' => 1
+            },
+            'getGeolocation'   => {
+                'method'             => 'GET',
+                'url'                => "session/:sessionId/location",
+                'no_content_success' => 0
+            },
 
             # /session/:sessionId/local_storage
             # /session/:sessionId/local_storage/key/:key

+ 52 - 0
lib/Selenium/Remote/Driver.pm

@@ -2049,6 +2049,58 @@ sub cache_status {
     my $res = { 'command' => 'cacheStatus' };
     return $self->_execute_command($res);
 }
+
+=head2 set_geolocation
+
+ Description:
+    Set the current geographic location - note that your driver must
+    implement this endpoint, or else it will crash your session. At the
+    very least, it works in v2.12 of Chromedriver.
+
+ Input:
+    Required:
+        HASH: A hash with key C<location> whose value is a Location hashref. See
+        usage section for example.
+
+ Usage:
+    $driver->set_geolocation( location => {
+        latitude  => 40.714353,
+        longitude => -74.005973,
+        altitude  => 0.056747
+    });
+
+ Output:
+    BOOLEAN - success or failure
+
+=cut
+
+sub set_geolocation {
+    my ( $self, %params ) = @_;
+    my $res = { 'command' => 'setGeolocation' };
+    return $self->_execute_command( $res, \%params );
+}
+
+=head2 get_geolocation
+
+ Description:
+    Get the current geographic location. Note that your webdriver must
+    implement this endpoint - otherwise, it will crash your session. At
+    the time of release, we couldn't get this to work on the desktop
+    FirefoxDriver or desktop Chromedriver.
+
+ Usage:
+    print $driver->get_geolocation;
+
+ Output:
+    { latitude: number, longitude: number, altitude: number } - The current geo location.
+
+=cut
+
+sub get_geolocation {
+    my ($self) = @_;
+    my $res = { 'command' => 'getGeolocation' };
+    return $self->_execute_command($res);
+}
 =head2 send_modifier
 
  Description:

+ 24 - 5
t/01-driver.t

@@ -23,6 +23,12 @@ my $driver = Selenium::Remote::Driver->new(%selenium_args);
 my $website = 'http://localhost:63636';
 my $ret;
 
+my $chrome;
+eval { $chrome = Selenium::Remote::Driver->new(
+    %selenium_args,
+    browser_name => 'chrome'
+); };
+
 DESIRED_CAPABILITIES: {
     # We're using a different test method for these because we needed
     # to inspect payload of the POST to /session, and the method of
@@ -424,13 +430,8 @@ USER_AGENT: {
 }
 
 STORAGE: {
-    my $chrome;
-    my %selenium_chrome_args = ( browser_name => 'chrome');
-    $selenium_chrome_args{remote_conn} = $selenium_args{remote_conn};
-
   SKIP: {
         eval {
-            $chrome = Selenium::Remote::Driver->new(%selenium_chrome_args);
             $chrome->get($website);
         };
 
@@ -457,6 +458,24 @@ HTML5: {
         my $status = $driver->cache_status;
         ok($status, 'we can get application cache status');
     }
+
+  SKIP: {
+        skip 'Geolocation requires Chrome to test', 2 unless $chrome;
+
+        my $ret = $chrome->set_geolocation( location => {
+            latitude => 40.714353,
+            longitude => -74.005973,
+            altitude => 0.056747
+        });
+        ok($ret, 'can set geolocation');
+
+      TODO: {
+            local $TODO = 'GET geolocation has a cast Long to Double error in Chromedriver';
+            my $ret = {};
+            eval { $ret = $chrome->get_geolocation };
+            ok(exists $ret->{location}, 'get_geolocation returns a location dictionary.');
+        }
+    }
 }
 
 QUIT: {

+ 0 - 2
t/03-spec-coverage.t

@@ -34,8 +34,6 @@ my $todo_list = {
    'GET session/:sessionId/window/:windowHandle/position'  => 1,
    'POST session/:sessionId/keys'                          => 1,
    'POST session/:sessionId/window/:windowHandle/maximize' => 1,
-   'GET session/:sessionId/location'                       => 1,
-   'POST session/:sessionId/location'                      => 1,
    'GET session/:sessionId/local_storage'                  => 1,
    'POST session/:sessionId/local_storage'                 => 1,
    'DELETE session/:sessionId/local_storage'               => 1,