瀏覽代碼

Merge remote-tracking branch 'swandog/master' into swandog-js-return-array-web-elements

Aditya Ivaturi 13 年之前
父節點
當前提交
a8ce0ae9d5
共有 3 個文件被更改,包括 48 次插入7 次删除
  1. 34 7
      lib/Selenium/Remote/Driver.pm
  2. 14 0
      t/01-driver.t
  3. 0 0
      t/mock-recordings/01-driver-mock-linux.json

+ 34 - 7
lib/Selenium/Remote/Driver.pm

@@ -922,19 +922,46 @@ sub execute_script {
         my $params = {'script' => $script, 'args' => [@args]};
         my $ret = $self->_execute_command($res, $params);
         
-        # replace any ELEMENTS with WebElement
-        if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
-            $ret =
-                new Selenium::Remote::WebElement(
-                                        $ret->{ELEMENT}, $self);
-        }
-        return $ret;
+        return $self->_convert_to_webelement($ret);
     }
     else {
         croak 'Javascript is not enabled on remote driver instance.';
     }
 }
 
+# _convert_to_webelement
+# An internal method used to traverse a data structure
+# and convert any ELEMENTS with WebElements
+
+sub _convert_to_webelement {
+    my $self=shift;
+    my $ret=shift;
+
+    if (ref($ret) and (ref($ret) eq 'HASH')) {
+        if((keys %$ret==1) and exists $ret->{'ELEMENT'}) {
+            # replace an ELEMENT with WebElement
+            return
+              new Selenium::Remote::WebElement(
+                                               $ret->{ELEMENT}, $self);
+        }
+
+        my %hash;
+        foreach my $key (keys %$ret) {
+            $hash{$key}=$self->_convert_to_webelement($ret->{$key});
+        }
+        return \%hash;
+    }
+
+    if(ref($ret) and (ref($ret) eq 'ARRAY')) {
+        my @array=
+          map {$self->_convert_to_webelement($_)}
+            @$ret;
+        return \@array;
+    }
+
+    return $ret;
+}
+
 =head2 screenshot
 
  Description:

+ 14 - 0
t/01-driver.t

@@ -177,6 +177,20 @@ EXECUTE: {
         my $elem = $driver->execute_script($script,'checky');
         ok($elem->isa('Selenium::Remote::WebElement'), 'Executed script');
         is($elem->get_attribute('id'),'checky','Execute found proper element');
+        $script = q{
+          var links = window.document.links
+          var length = links.length
+          var results = new Array(length)
+          while(length--) results[length] = links[length];
+          return results;
+        };
+        $elem = $driver->execute_script($script);
+        ok($elem, 'Got something back from execute_script');
+        isa_ok($elem, 'ARRAY', 'What we got back is an ARRAY ref');
+        ok(scalar(@$elem), 'There are elements in our array ref');
+        foreach my $element (@$elem) {
+            isa_ok($element, 'Selenium::Remote::WebElement', 'Element was converted to a WebElement object');
+        }
         $script = q{
           var arg1 = arguments[0];
           var callback = arguments[arguments.length-1];

File diff suppressed because it is too large
+ 0 - 0
t/mock-recordings/01-driver-mock-linux.json


Some files were not shown because too many files changed in this diff