Эх сурвалжийг харах

moved mock class to t/lib, refactored a bit S::R::RC->request prototype

Emmanuel 'BHS_error' Peroumalnaik 11 жил өмнө
parent
commit
f72178f0c9

+ 10 - 11
lib/Selenium/Remote/Driver.pm

@@ -465,17 +465,11 @@ sub DEMOLISH {
 sub _execute_command {
     my ( $self, $res, $params ) = @_;
     $res->{'session_id'} = $self->session_id;
-    $DB::single = 1;
     my $resource = $self->commands->get_params($res);
 
     if ($resource) {
         $params = {} unless $params;
-        my $resp = $self->remote_conn->request(
-            $resource->{method},
-            $resource->{url},
-            $resource->{no_content_success},
-            $params
-        );
+        my $resp = $self->remote_conn->request( $resource, $params);
         if ( ref($resp) eq 'HASH' ) {
             if ( $resp->{cmd_status} && $resp->{cmd_status} eq 'OK' ) {
                 return $resp->{cmd_return};
@@ -547,10 +541,15 @@ sub _request_new_session {
 
     # command => 'newSession' to fool the tests of commands implemented
     # TODO: rewrite the testing better, this is so fragile.
-    my $resp = $self->remote_conn->request(
-        $self->commands->get_method('newSession'),
-        $self->commands->get_url('newSession'),
-        $self->commands->get_no_content_success('newSession'),
+    my $resource_new_session = { 
+        method => $self->commands->get_method('newSession'),
+        url => $self->commands->get_url('newSession'),
+        no_content_success => $self->commands->get_no_content_success('newSession'),
+    };
+    my $rc = $self->remote_conn;
+    $DB::single = 1;
+    my $resp = $rc->request(
+        $resource_new_session, 
         $args,
     );
     if ( ( defined $resp->{'sessionId'} ) && $resp->{'sessionId'} ne '' ) {

+ 6 - 4
lib/Selenium/Remote/RemoteConnection.pm

@@ -35,7 +35,7 @@ sub BUILD {
     my $self = shift;
     my $status;
     try {
-        $status = $self->request('GET','status');
+        $status = $self->request({method => 'GET', url => 'status'});
     }
     catch {
         croak "Could not connect to SeleniumWebDriver: $_" ;
@@ -44,7 +44,7 @@ sub BUILD {
     if($status->{cmd_status} ne 'OK') {
         # Could be grid, see if we can talk to it
         $status = undef;
-        $status = $self->request('GET', 'grid/api/hub/status');
+        $status = $self->request({method => 'GET', url => 'grid/api/hub/status'});
     }
 
     unless ($status->{cmd_status} eq 'OK') {
@@ -55,8 +55,10 @@ sub BUILD {
 
 # This request method is tailored for Selenium RC server
 sub request {
-    my ($self, $method, $url, $no_content_success, $params) = @_;
-    $no_content_success = $no_content_success // 0;
+    my ($self,$resource,$params) = @_;
+    my $method =        $resource->{method};
+    my $url =        $resource->{url};
+    my $no_content_success =        $resource->{no_content_success} // 0;
 
     my $content = '';
     my $fullurl = '';

+ 7 - 5
t/Test-Selenium-Remote-Driver.t

@@ -1,25 +1,27 @@
 #!/usr/bin/env perl
+use lib 't/lib';
 use Test::More;
 use Test::Exception;
 use Test::Selenium::Remote::Driver;
 use Selenium::Remote::WebElement;
-use Selenium::Remote::MockCommands;
-use Selenium::Remote::MockRemoteConnection;
+use MockCommands;
+use MockRemoteConnection;
 
 my $spec = {
     findElement => sub {
-        my $searched_item = shift;
+        my (undef,$searched_item) = @_;
+        $DB::single = 1;
         return { status => 'OK', return => { ELEMENT => '123456' } }
           if ( $searched_item->{value} eq 'q' );
         return { status => 'NOK', return => 0, error => 'element not found' };
     },
     getPageSource => sub { return 'this output matches regex'},
 };
-my $mock_commands = Selenium::Remote::MockCommands->new;
+my $mock_commands = MockCommands->new;
 
 my $successful_driver =
   Test::Selenium::Remote::Driver->new(
-    remote_conn => Selenium::Remote::MockRemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
+    remote_conn => MockRemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
     commands => $mock_commands,
 );
 $successful_driver->find_element_ok('q','find_element_ok works');

+ 7 - 5
t/Test-Selenium-Remote-WebElement.t

@@ -1,12 +1,13 @@
 #!perl
+use lib 't/lib';
 use Test::More;
-use Selenium::Remote::MockCommands;
-use Selenium::Remote::MockRemoteConnection;
+use MockCommands;
+use MockRemoteConnection;
 use Test::Selenium::Remote::Driver;
 use Test::Selenium::Remote::WebElement;
 
 # Start off by faking a bunch of Selenium::Remote::WebElement calls succeeding
-my $mock_commands = Selenium::Remote::MockCommands->new;
+my $mock_commands = MockCommands->new;
 my $spec = { };
 
 foreach my $k (
@@ -17,11 +18,11 @@ foreach my $k (
 $spec->{getElementTagName} = sub { return { status => 'OK', return => 'iframe' }}; 
 $spec->{getElementValue} = sub { return { status => 'OK', return => 'my_value' }};
 $spec->{getElementText} = sub { return { status => 'OK', return => "my_text\nis fantastic" }};
-$spec->{getElementAttribute}  = sub { my $name = $_[1]; return { status => 'OK', return => "my_$name" }};
+$spec->{getElementAttribute}  = sub { my @args = @_; my $name = $args[0]->{name};  return { status => 'OK', return => "my_$name" }};
 
 my $driver =
   Test::Selenium::Remote::Driver->new(
-    remote_conn => Selenium::Remote::MockRemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
+    remote_conn => MockRemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
     commands => $mock_commands,
 );
 
@@ -37,6 +38,7 @@ $successful_element->send_keys_ok('Hello World');
 $successful_element->tag_name_is( 'iframe', 'we got an iframe tag' );
 $successful_element->tag_name_isnt( 'BOOM', 'tag name is not boom' ); 
 $successful_element->tag_name_unlike( qr/BOOM/, "tag_name doesn't match BOOM" );
+            $successful_element->value_is( 'my_value', 'Got an my_value value?' );
 
 #     check_test(
 #         sub {

+ 3 - 2
lib/Selenium/Remote/MockCommands.pm → t/lib/MockCommands.pm

@@ -1,4 +1,4 @@
-package Selenium::Remote::MockCommands; 
+package MockCommands; 
 use Moo; 
 extends 'Selenium::Remote::Commands';
 
@@ -9,10 +9,11 @@ sub get_params {
     my $self = shift;
     my $args = shift;
     my $data = {};
-    my $command = $args->{command};
+    my $command = delete $args->{command};
     $data->{'url'} = $self->get_url($command);
     $data->{'method'} = $self->get_method($command);
     $data->{'no_content_success'} = $self->get_no_content_success($command);
+    $data->{'url_params'}  = $args;
     return $data; 
 }
 

+ 7 - 3
lib/Selenium/Remote/MockRemoteConnection.pm → t/lib/MockRemoteConnection.pm

@@ -1,4 +1,4 @@
-package Selenium::Remote::MockRemoteConnection;
+package MockRemoteConnection;
 
 # ABSTRACT: utility class to mock the responses from Selenium server
 
@@ -26,7 +26,11 @@ has 'session_id' => (
 
 sub request { 
     my $self = shift;
-    my ($method, $url, $no_content_success, $params) = @_;
+    my ($resource, $params) = @_;
+    my $method =        $resource->{method};
+    my $url =        $resource->{url};
+    my $no_content_success =        $resource->{no_content_success} // 0;
+    my $url_params = $resource->{url_params};
     my $mock_cmds = $self->mock_cmds;
     my $spec = $self->spec; 
     my $cmd = $mock_cmds->get_method_name_from_parameters({method => $method,url => $url});
@@ -37,7 +41,7 @@ sub request {
             $ret->{cmd_return} = 1;
         }
         else { 
-            my $mock_return = $return_sub->($params);
+            my $mock_return = $return_sub->($url_params,$params);
             if (ref($mock_return) eq 'HASH') { 
                 $ret->{cmd_status} = $mock_return->{status};
                 $ret->{cmd_return} = $mock_return->{return};