소스 검색

Fix for "execute_script" to convert elements into WebElement objects regardless of whether they are nested in the return structure

Ken Swanson 14 년 전
부모
커밋
ac2990445c
1개의 변경된 파일34개의 추가작업 그리고 7개의 파일을 삭제
  1. 34 7
      lib/Selenium/Remote/Driver.pm

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

@@ -897,19 +897,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: