Prechádzať zdrojové kódy

Added a new test that does not connect to a seleniumrc

  - it uses a recorded mock connection
  - added dependency for LWP::Protocol::PSGI
  - added new MockSeleniumRC module which uses LWP::Protocol::PSGI
    for overriding LWP::UserAgent

    this allows us to test the module without actually opening a
    single port
Gordon Child 14 rokov pred
rodič
commit
58a349cae0

+ 3 - 0
MANIFEST

@@ -12,7 +12,10 @@ t/01-driver-live.t
 t/02-webelement-live.t
 t/03-spec-coverage.t
 t/04-commands-implemented.t
+t/05-driver-mock.t
+t/mock-recordings/05-driver-mock-recording.json
 t/http-server.pl
+t/lib/MockSeleniumRC.pm
 inc/Module/AutoInstall.pm
 inc/Module/Install/AutoInstall.pm
 inc/Module/Install/Base.pm

+ 1 - 0
Makefile.PL

@@ -7,6 +7,7 @@ license  'perl';
 
 build_requires 'Test::More';
 build_requires 'LWP::UserAgent';
+build_requires 'LWP::Protocol::PSGI';
 build_requires 'HTTP::Headers';
 build_requires 'HTTP::Request';
 build_requires 'Carp';

+ 45 - 0
t/05-driver-mock.t

@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use Test::More;
+use Data::Dumper;
+use t::lib::MockSeleniumRC;
+
+use_ok('Selenium::Remote::Driver');
+
+
+
+# Start our local http server only if release testing
+if ($^O eq 'MSWin32' && $ENV{RELEASE_TESTING})
+{
+   system("start \"TEMP_HTTP_SERVER\" /MIN perl t/http-server.pl");
+} elsif($ENV{RELEASE_TESTING})
+{
+    system("perl t/http-server.pl > /dev/null &");
+}
+
+my $website = 'http://localhost:63636';
+
+my $record = $ENV{RELEASE_TESTING};
+t::lib::MockSeleniumRC::register($record,'t/mock-recordings/05-driver-mock-recording.json');
+
+my $driver = Selenium::Remote::Driver->new;
+isa_ok($driver,'Selenium::Remote::Driver');
+$driver->get("$website/alerts.html");
+
+$driver->quit;
+
+done_testing;
+
+# Kill our HTTP Server
+if ($^O eq 'MSWin32' && $ENV{RELEASE_TESTING})
+{
+   system("taskkill /FI \"WINDOWTITLE eq TEMP_HTTP_SERVER\"");
+}
+elsif($ENV{RELEASE_TESTING})
+{
+    `ps aux | grep http-server\.pl | grep perl | awk '{print \$2}' | xargs kill`;
+}
+
+0;

+ 102 - 0
t/lib/MockSeleniumRC.pm

@@ -0,0 +1,102 @@
+package t::lib::MockSeleniumRC;
+use strict;
+use warnings;
+
+use LWP::Protocol::PSGI;
+use JSON;
+
+our $MockSeleniumRCObj;
+
+sub save_recording {
+  my ($self) = @_;
+  open(my $fh, '>', $self->{file});
+  print $fh encode_json($self->{req_resp});
+  close $fh;
+}
+
+sub load_recording {
+  my ($self) = @_;
+  open(my $fh, '<', $self->{file});
+  my @lines = <$fh>;
+  $self->{req_resp} = decode_json(join('', @lines));
+  close $fh;
+}
+
+sub register {
+  my $record = shift;
+  $record = 1 if !defined $record;
+  my $file = shift;
+  my $self = {record => $record,
+              req_index => 0,
+              file => $file};
+  bless $self,__PACKAGE__;
+  if ($record) {
+    require LWP::UserAgent;
+    require HTTP::Headers;
+    require HTTP::Request;
+    $self->{req_resp} = [];
+  } else {
+    $self->load_recording;
+  }
+  LWP::Protocol::PSGI->register(\&t::lib::MockSeleniumRC::psgi_app);
+  $MockSeleniumRCObj = $self;
+}
+
+sub psgi_app {
+  my $env = shift;
+  my $self = $MockSeleniumRCObj;
+  my $uri =
+      $env->{'psgi.url_scheme'} . '://'
+    . $env->{SERVER_NAME} . ':'
+    . $env->{SERVER_PORT}
+    . $env->{REQUEST_URI};
+  my $content = '';
+  my $s;
+  while (read($env->{'psgi.input'}, $s, 100)) {
+    $content .= $s;
+  }
+  my $req_index = \$self->{req_index};
+  if (!$self->{record}) {
+    if (  $self->{req_resp}->[$$req_index]->{request}->{verb} eq $env->{REQUEST_METHOD}
+      and $self->{req_resp}->[$$req_index]->{request}->{uri} eq $uri
+      and $self->{req_resp}->[$$req_index]->{request}->{content} eq $content) {
+      return $self->{req_resp}->[$$req_index++]->{response};
+    } else {
+      die
+"Request information has changed since recording... do you need to record webdriver responses again?";
+    }
+  } else {
+    my $ua = LWP::UserAgent->new;
+    my $h  = HTTP::Headers->new;
+    $h->header('Content-Type' => $env->{CONTENT_TYPE});
+    $h->header('Accept'       => $env->{HTTP_ACCEPT});
+    my $req = HTTP::Request->new($env->{REQUEST_METHOD}, $uri, $h, $content);
+    LWP::Protocol::PSGI->unregister;
+    my $res = $ua->request($req);
+    LWP::Protocol::PSGI->register(\&psgi_app);
+    my $head    = $res->{_headers}->clone;
+    my $newhead = [];
+
+    for my $key (keys %{$head}) {
+      push @{$newhead}, $key;
+      push @{$newhead}, $head->{$key};
+    }
+    my $response = [$res->code, $newhead, [$res->content]];
+    my $request = {
+      verb    => $env->{REQUEST_METHOD},
+      uri     => $uri,
+      content => $content
+    };
+    push @{$self->{req_resp}}, {request => $request, response => $response};
+    return $response;
+  }
+}
+
+sub DESTROY {
+  my ($self) = @_;
+  if($self->{record}) {
+    $self->save_recording;
+  }
+}
+
+1;

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
t/mock-recordings/05-driver-mock-recording.json


Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov