Bug 24545: Fix license statements
[koha-ffzg.git] / t / lib / Selenium.pm
index 6741f62..29f7ef4 100644 (file)
@@ -18,11 +18,23 @@ package t::lib::Selenium;
 
 use Modern::Perl;
 use Carp qw( croak );
+use JSON qw( from_json );
 
 use C4::Context;
 
 use base qw(Class::Accessor);
-__PACKAGE__->mk_accessors(qw(login password base_url selenium_addr selenium_port driver));
+__PACKAGE__->mk_accessors(qw(login password base_url opac_base_url selenium_addr selenium_port driver));
+
+sub capture {
+    my ( $class, $driver ) = @_;
+
+    my $lutim_server = q|https://framapic.org|; # Thanks Framasoft!
+    $driver->capture_screenshot('selenium_failure.png');
+    my $from_json = from_json qx{curl -s -F "format=json" -F "file=\@selenium_failure.png" -F "delete-day=1" $lutim_server};
+    if ( $from_json ) {
+        print STDERR "\nSCREENSHOT: $lutim_server/" . $from_json->{msg}->{short} . "\n";
+    }
+}
 
 sub new {
     my ( $class, $params ) = @_;
@@ -31,22 +43,38 @@ sub new {
     $self->{login}    = $params->{login}    || $config->{login};
     $self->{password} = $params->{password} || $config->{password};
     $self->{base_url} = $params->{base_url} || $config->{base_url};
+    $self->{opac_base_url} = $params->{opac_base_url} || $config->{opac_base_url};
     $self->{selenium_addr} = $params->{selenium_addr} || $config->{selenium_addr};
     $self->{selenium_port} = $params->{selenium_port} || $config->{selenium_port};
     $self->{driver} = Selenium::Remote::Driver->new(
         port               => $self->{selenium_port},
         remote_server_addr => $self->{selenium_addr},
-        error_handler => sub {
-            my $selenium_error = $_[1];
+    );
+    bless $self, $class;
+    $self->add_error_handler;
+    return $self;
+}
+
+sub add_error_handler {
+    my ( $self ) = @_;
+    $self->{driver}->error_handler(
+        sub {
+            my ( $driver, $selenium_error ) = @_;
             print STDERR "\nSTRACE:";
             my $i = 1;
             while ( (my @call_details = (caller($i++))) ){
                 print STDERR "\t" . $call_details[1]. ":" . $call_details[2] . " in " . $call_details[3]."\n";
             }
             print STDERR "\n";
-            croak $selenium_error; }
+            $self->capture( $driver );
+            croak $selenium_error;
+        }
     );
-    return bless $self, $class;
+}
+
+sub remove_error_handler {
+    my ( $self ) = @_;
+    $self->{driver}->error_handler( sub {} );
 }
 
 sub config {
@@ -54,6 +82,7 @@ sub config {
         login    => $ENV{KOHA_USER} || 'koha',
         password => $ENV{KOHA_PASS} || 'koha',
         base_url => ( $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseURL") ) . "/cgi-bin/koha/",
+        opac_base_url => ( $ENV{KOHA_OPAC_URL} || C4::Context->preference("OPACBaseURL") ) . "/cgi-bin/koha/",
         selenium_addr => $ENV{SELENIUM_ADDR} || 'localhost',
         selenium_port => $ENV{SELENIUM_PORT} || 4444,
     };
@@ -72,6 +101,19 @@ sub auth {
     $login_button->submit();
 }
 
+sub opac_auth {
+    my ( $self, $login, $password ) = @_;
+
+    $login ||= $self->login;
+    $password ||= $self->password;
+    my $mainpage = $self->opac_base_url . 'opac-main.pl';
+
+    $self->driver->get($mainpage . q|?logout.x=1|); # Logout before, to make sure we will see the login form
+    $self->driver->get($mainpage);
+    $self->fill_form( { userid => $login, password => $password } );
+    $self->submit_form;
+}
+
 sub fill_form {
     my ( $self, $values ) = @_;
     while ( my ( $id, $value ) = each %$values ) {
@@ -97,6 +139,8 @@ sub click {
     my $xpath_selector;
     if ( exists $params->{main} ) {
         $xpath_selector = '//div[@id="'.$params->{main}.'"]';
+    } elsif ( exists $params->{main_class} ) {
+        $xpath_selector = '//div[@class="'.$params->{main_class}.'"]';
     }
     if ( exists $params->{href} ) {
         if ( ref( $params->{href} ) ) {
@@ -151,6 +195,7 @@ t::lib::Selenium - Selenium helper module
 
 The goal of this module is to group the different actions we need
 when we use automation test using Selenium
+
 =head1 METHODS
 
 =head2 new
@@ -203,8 +248,26 @@ when we use automation test using Selenium
 
     Should always be called to avoid the "An element could not be located on the page" error
 
-=head2
+=head2 capture
+    $c->capture
+
+Capture a screenshot and upload it using the excellent lut.im service provided by framasoft
+The url of the image will be printed on STDERR (it should be better to return it instead)
+
+=head2 add_error_handler
+    $c->add_error_handler
+
+Add our specific error handler to the driver.
+It will displayed a trace as well as capture a screenshot of the current screen.
+So only case you should need it is after you called remove_error_handler
+
+=head2 remove_error_handler
+    $c->remove_error_handler
 
+Do *not* call this method if you are not aware of what it will do!
+It will remove any kinds of error raised by the driver.
+It can be useful in some cases, for instance if you want to make sure something will not happen and that could make the driver exploses otherwise.
+You certainly should call it for only one statement then must call add_error_handler right after.
 
 =head1 AUTHORS