Ver código fonte

Fix #110: implement a desired_capabilities option

Daniel Gempesaw 11 anos atrás
pai
commit
59394699ca
2 arquivos alterados com 70 adições e 5 exclusões
  1. 28 3
      lib/Selenium/Remote/Driver.pm
  2. 42 2
      t/01-driver.t

+ 28 - 3
lib/Selenium/Remote/Driver.pm

@@ -362,8 +362,8 @@ has 'extra_capabilities' => (
 );
 
 has 'firefox_profile' => (
-    is => 'rw',
-    coerce => sub {
+    is        => 'rw',
+    coerce    => sub {
         my $profile = shift;
         unless (Scalar::Util::blessed($profile)
           && $profile->isa('Selenium::Remote::Driver::Firefox::Profile')) {
@@ -375,11 +375,22 @@ has 'firefox_profile' => (
     predicate => 'has_firefox_profile'
 );
 
+has 'desired_capabilities' => (
+    is        => 'rw',
+    lazy      => 1,
+    predicate => 'has_desired_capabilities'
+);
+
 sub BUILD {
     my $self = shift;
 
     # Connect to remote server & establish a new session
-    $self->new_session( $self->extra_capabilities );
+    if ($self->has_desired_capabilities) {
+        $self->new_desired_session( $self->desired_capabilities );
+    }
+    else {
+        $self->new_session( $self->extra_capabilities );
+    }
 
     if ( !( defined $self->session_id ) ) {
         croak "Could not establish a session with the remote server\n";
@@ -458,6 +469,20 @@ sub new_session {
         $args->{desiredCapabilities}->{firefox_profile} = $self->firefox_profile;
     }
 
+    $self->_request_new_session($args);
+}
+
+sub new_desired_session {
+    my ( $self, $caps ) = @_;
+
+    $self->_request_new_session({
+        desiredCapabilities => $caps
+    });
+}
+
+sub _request_new_session {
+    my ( $self, $args ) = @_;
+
     # command => 'newSession' to fool the tests of commands implemented
     # TODO: rewrite the testing better, this is so fragile.
     my $resp = $self->remote_conn->request(

+ 42 - 2
t/01-driver.t

@@ -1,8 +1,10 @@
 use strict;
 use warnings;
 
-use Test::More;
+use JSON;
 use Net::Ping;
+use Test::More;
+use Test::LWP::UserAgent;
 use Selenium::Remote::Driver;
 
 BEGIN {
@@ -29,10 +31,48 @@ if (!$record && !(-e "t/mock-recordings/$mock_file")) {
 }
 t::lib::MockSeleniumWebDriver::register($record,"t/mock-recordings/$mock_file");
 
-my $driver = new Selenium::Remote::Driver(browser_name => 'firefox');
+my $driver = Selenium::Remote::Driver->new(browser_name => 'firefox');
 my $website = 'http://localhost:63636';
 my $ret;
 
+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
+    # recording the RES/REQ pairs doesn't provide any easy way to do
+    # that.
+    my $tua = Test::LWP::UserAgent->new;
+    my $res = {
+        cmd_return => {},
+        cmd_status => 'OK',
+        sessionId => '123124123'
+    };
+
+    $tua->map_response(qr{status|quit}, HTTP::Response->new(200, 'OK'));
+
+    $tua->map_response(qr{session}, sub {
+                           my $request = shift;
+                           my $caps = from_json($request->decoded_content)->{desiredCapabilities};
+                           my @keys = keys %$caps;
+                           ok(scalar @keys == 2, 'exactly 2 keys passed in if we use desired_capabilities');
+                           ok($keys[0] eq 'browserName', 'and it is the right one');
+                           ok($caps->{superfluous} eq 'thing', 'and we pass through anything else');
+                           ok($caps->{browserName} eq 'firefox', 'and we override the "normal" caps');
+                           ok(!exists $caps->{platform}, 'or ignore them entirely');
+                           HTTP::Response->new(204, 'OK', ['Content-Type' => 'application/json'], to_json($res));
+                       });
+
+    my $driver = Selenium::Remote::Driver->new(
+        auto_close => 0,
+        browser_name => 'not firefox',
+        platform => 'WINDOWS',
+        desired_capabilities => {
+            'browserName' => 'firefox',
+            'superfluous' => 'thing'
+        },
+        ua => $tua
+    );
+}
+
 CHECK_DRIVER: {
     ok(defined $driver, 'Object loaded fine...');
     ok($driver->isa('Selenium::Remote::Driver'), '...and of right type');