Parcourir la source

Merge pull request #307 from vitstradal/master

Reuse old session by setting session_id
Daniel Gempesaw il y a 8 ans
Parent
commit
945c27c208

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

@@ -210,6 +210,7 @@ you please.
         'error_handler'        - CODEREF     - A CODEREF that we will call in event of any exceptions. See L</error_handler> for more details.
         'webelement_class'     - <string>    - sub-class of Selenium::Remote::WebElement if you wish to use an alternate WebElement class.
         'ua'                   - LWP::UserAgent instance - if you wish to use a specific $ua, like from Test::LWP::UserAgent
+        'session_id'           - <string>    - prevent create new session, reuse previous (but not finished) session, previous session should have 'auto_close' => 0
 
     If no values are provided, then these defaults will be assumed:
         'remote_server_addr' => 'localhost'
@@ -220,6 +221,7 @@ you please.
         'javascript'         => 1
         'auto_close'         => 1
         'default_finder'     => 'xpath'
+        'session_id'         => undef
 
  Output:
     Remote Driver object
@@ -567,12 +569,14 @@ with 'Selenium::Remote::Driver::CanSetWebdriverContext';
 sub BUILD {
     my $self = shift;
 
-    if ($self->has_desired_capabilities) {
-        $self->new_desired_session( $self->desired_capabilities );
-    }
-    else {
-        # Connect to remote server & establish a new session
-        $self->new_session( $self->extra_capabilities );
+    if ( !( defined $self->session_id ) ) {
+        if ($self->has_desired_capabilities) {
+            $self->new_desired_session( $self->desired_capabilities );
+        }
+        else {
+            # Connect to remote server & establish a new session
+            $self->new_session( $self->extra_capabilities );
+        }
     }
 
     if ( !( defined $self->session_id ) ) {

+ 56 - 0
t/12-reuse-session.t

@@ -0,0 +1,56 @@
+use strict;
+use warnings;
+
+use Carp::Always;
+use Test::More;
+use Test::Selenium::Remote::Driver;
+use Selenium::Remote::Mock::RemoteConnection;
+
+use FindBin;
+use lib $FindBin::Bin . '/lib';
+use TestHarness;
+
+my $harness = TestHarness->new(
+    this_file => $FindBin::Script
+);
+
+my @browsers = qw/chrome firefox/;
+
+foreach (@browsers) {
+    my %selenium_args = (
+        default_finder => 'css',
+        javascript     => 1,
+        %{ $harness->base_caps },
+        browser_name   => $_,
+    );
+
+    my $s1 = Test::Selenium::Remote::Driver->new(
+        %selenium_args
+    );
+    my $s2 = Test::Selenium::Remote::Driver->new(
+        %selenium_args,
+        auto_close => 0,
+        session_id => $s1->session_id,
+    );
+
+    my $s3 = Test::Selenium::Remote::Driver->new(
+        %selenium_args,
+    );
+
+    is($s1->session_id, $s2->session_id, "session_id is reused when specified");
+    isnt($s1->session_id, $s3->session_id, "session_id not reused");
+    pass("session_id.1=". $s2->session_id);
+    pass("session_id.2=". $s2->session_id);
+    pass("session_id.3=". $s3->session_id);
+
+    my $perl_title = 'The Perl Programming Language - www.perl.org';
+    my $cpan_title = 'The Comprehensive Perl Archive Network - www.cpan.org';
+
+    $s1->get_ok('http://perl.org/');
+    $s1->title_is($perl_title, 'perl.org title matches correctly');
+
+    $s3->get_ok('http://perl.org/');
+    $s3->title_is($perl_title, 'perl.org title matches correctly');
+}
+
+done_testing;

Fichier diff supprimé car celui-ci est trop grand
+ 2 - 0
t/mock-recordings/12-reuse-session-mock.json


Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff