Переглянути джерело

Begin adding localStorage API endpoints

Daniel Gempesaw 11 роки тому
батько
коміт
b52f22ac8f
3 змінених файлів з 89 додано та 10 видалено
  1. 17 8
      lib/Selenium/Remote/Commands.pm
  2. 47 2
      lib/Selenium/Remote/Driver.pm
  3. 25 0
      t/01-driver.t

+ 17 - 8
lib/Selenium/Remote/Commands.pm

@@ -349,15 +349,24 @@ has '_cmds' => (
                 'url'                => 'session/:sessionId/file',
                 'no_content_success' => 1
             },
+            'getLocalStorageItem' => {
+                'method'             => 'GET',
+                'url'                => '/session/:sessionId/local_storage/key/:key',
+                'no_content_success' => 0
+            },
+            'deleteLocalStorageItem' => {
+                'method'             => 'DELETE',
+                'url'                => '/session/:sessionId/local_storage/key/:key',
+                'no_content_success' => 1
+            }
+
+            # /session/:sessionId/local_storage
+            # /session/:sessionId/local_storage/key/:key
+            # /session/:sessionId/local_storage/size
+            # /session/:sessionId/session_storage
+            # /session/:sessionId/session_storage/key/:key
+            # /session/:sessionId/session_storage/size
 
-            #'setVisible' => {
-            #               'method' => 'POST',
-            #               'url' => 'session/:sessionId/visible'
-            #},
-            #'getVisible' => {
-            #               'method' => 'GET',
-            #               'url' => 'session/:sessionId/visible'
-            #},
         };
     }
 );

+ 47 - 2
lib/Selenium/Remote/Driver.pm

@@ -2021,7 +2021,7 @@ sub get_active_element {
  Description:
     Send an event to the active element to depress or release a modifier key.
 
-  Input: 2
+ Input: 2
     Required:
       value - String - The modifier key event to be sent. This key must be one 'Ctrl','Shift','Alt',' or 'Command'/'Meta' as defined by the send keys command
       isdown - Boolean/String - Whether to generate a key down or key up
@@ -2253,7 +2253,6 @@ sub get_path {
 =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.
@@ -2293,6 +2292,52 @@ sub set_inner_window_size {
     return $self->execute_script(join(';', @resize)) ? 1 : 0;
 }
 
+=head2 get_local_storage_item
+
+ Description:
+     Get the value of a local storage item specified by the given key.
+
+ Input: 1
+    Required:
+        STRING - name of the key to be retrieved
+
+ Output:
+     STRING - value of the local storage item
+
+ Usage:
+     $driver->get_local_storage_item('key')
+
+=cut
+
+sub get_local_storage_item {
+    my ($self, $key) = @_;
+    my $res = { 'command' => 'getLocalStorageItem' };
+    my $params = { 'key' => $key };
+    return $self->_execute_command($res, $params);
+}
+
+=head2 delete_local_storage_item
+
+ Description:
+     Get the value of a local storage item specified by the given key.
+
+ Input: 1
+    Required
+        STRING - name of the key to be deleted
+
+ Usage:
+     $driver->delete_local_storage_item('key')
+
+=cut
+
+sub delete_local_storage_item {
+    my ($self, $key) = @_;
+    my $res = { 'command' => 'deleteLocalStorageItem' };
+    my $params = { 'key' => $key };
+    return $self->_execute_command($res, $params);
+}
+
+
 1;
 
 __END__

+ 25 - 0
t/01-driver.t

@@ -434,4 +434,29 @@ NO_SERVER_ERROR_MESSAGE: {
     unlike($@, qr/Use of uninitialized value/, "Error message for no server at host/port combination is helpful");
 }
 
+STORAGE: {
+    my $chrome;
+
+  SKIP: {
+        eval {
+            $chrome = Selenium::Remote::Driver->new(browser_name => 'chrome');
+            $chrome->get($website);
+        };
+
+        if ($@ || !defined $chrome) {
+            skip 'FirefoxDriver does not support Storage APIs; Chromedriver must be configured to perform storage tests', 3;
+        }
+
+        my ($key, $value) = ('testKey', 'testValue');
+        $chrome->execute_script('localStorage.' . $key . ' = "' . $value . '"; return 1');
+
+        my $actual = $chrome->get_local_storage_item($key);
+        ok($actual eq $value, 'can retrieve local storage by key');
+
+        ok($chrome->delete_local_storage_item($key), 'can delete local storage by key');
+        my $now_empty = $chrome->get_local_storage_item($key);
+        ok(!(defined $now_empty), 'retrieving an empty or deleted local storage key returns undef');
+    }
+}
+
 done_testing;