Bladeren bron

Merge pull request #288 from Adzuna/vkatsikaros/firefox_context

Add command for Firefox context switching
Daniel Gempesaw 9 jaren geleden
bovenliggende
commit
65b1bcf35d
3 gewijzigde bestanden met toevoegingen van 73 en 1 verwijderingen
  1. 60 0
      lib/Selenium/Firefox.pm
  2. 11 0
      lib/Selenium/Remote/Commands.pm
  3. 2 1
      t/04-commands-implemented.t

+ 60 - 0
lib/Selenium/Firefox.pm

@@ -2,6 +2,7 @@ package Selenium::Firefox;
 
 # ABSTRACT: Use FirefoxDriver without a Selenium server
 use Moo;
+use Carp;
 use Selenium::Firefox::Binary qw/firefox_path/;
 use Selenium::CanStartBinary::FindBinary qw/coerce_simple_binary coerce_firefox_binary/;
 extends 'Selenium::Remote::Driver';
@@ -244,6 +245,65 @@ has 'firefox_binary' => (
     builder => 'firefox_path'
 );
 
+=head2 get_context
+
+ Description:
+    Firefox extension: Retrieve browser's scope (chrome or content).
+    Chrome is a privileged scope where you can access things like the
+    Firefox UI itself. Content scope is where things like webpages live.
+
+ Output:
+    STRING - context {CHROME|CONTENT}
+
+ Usage:
+    print $firefox_driver->get_context();
+
+=cut
+
+sub get_context {
+    my $self = shift;
+
+    if ( $self->_is_old_ff ) {
+        return 0;
+    }
+    my $res = { 'command' => 'getContext' };
+    return $self->_execute_command($res);
+}
+
+=head2 set_context
+
+ Description:
+    Firefox extension: Set browser's scope (chrome or content).
+    Chrome is a privileged scope where you can access things like the
+    Firefox UI itself. Content scope is where things like webpages live.
+
+ Input:
+    Required:
+        <STRING> - context {CHROME|CONTENT}
+
+ Usage:
+    $firefox_driver->set_context( $context );
+
+ Output:
+    BOOLEAN - success or failure
+
+=cut
+
+sub set_context {
+    my ( $self, $context ) = @_;
+
+    if ( $self->_is_old_ff ) {
+        return 0;
+    }
+    if ( not defined $context ) {
+        croak "Expecting context";
+    }
+    if ( $context !~ m/chrome|content/i ) {
+        croak "Expecting context value: chrome or content";
+    }
+    my $res = { 'command' => 'setContext' };
+    return $self->_execute_command( $res, { context => $context } );
+}
 
 with 'Selenium::CanStartBinary';
 

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

@@ -394,6 +394,17 @@ has '_cmds' => (
                 'url'                => 'session/:sessionId/orientation',
                 'no_content_success' => 0
             },
+            # firefox extension
+            'setContext' => {
+                'method'             => 'POST',
+                'url'                => 'session/:sessionId/moz/context',
+                'no_content_success' => 1
+            },
+            'getContext' => {
+                'method'             => 'GET',
+                'url'                => 'session/:sessionId/moz/context',
+                'no_content_success' => 0
+            },
 
             # /session/:sessionId/local_storage
             # /session/:sessionId/local_storage/key/:key

+ 2 - 1
t/04-commands-implemented.t

@@ -14,7 +14,8 @@ for my $command (keys %{$comm}) {
   my $found_command = 0;
   for my $file (
     qw{lib/Selenium/Remote/Driver.pm
-    lib/Selenium/Remote/WebElement.pm}
+    lib/Selenium/Remote/WebElement.pm
+    lib/Selenium/Firefox.pm}
     ) {
     open(my $fh, '<', $file) or die "Couldn't open file $file";
     for (<$fh>) {