From 11408e53821607bbbc50f40c8af3caf99d3f4423 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 17 Feb 2022 12:38:29 +0100 Subject: [PATCH] Bug 30063: Add selenium tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Séverine Queune Signed-off-by: Martin Renvoize --- t/db_dependent/selenium/patrons_search.t | 121 +++++++++++++++++++++-- t/lib/Selenium.pm | 18 ++-- 2 files changed, 120 insertions(+), 19 deletions(-) diff --git a/t/db_dependent/selenium/patrons_search.t b/t/db_dependent/selenium/patrons_search.t index dc09d70113..8c232b2c15 100755 --- a/t/db_dependent/selenium/patrons_search.t +++ b/t/db_dependent/selenium/patrons_search.t @@ -17,6 +17,12 @@ use Modern::Perl; +our @cleanup; +END { + unless ( @cleanup ) { say "WARNING: Cleanup failed!" } + $_->delete for @cleanup; +}; + use C4::Context; use utf8; @@ -25,6 +31,7 @@ use Test::MockModule; use C4::Context; use Koha::AuthUtils; +use Koha::Patrons; use t::lib::Mocks; use t::lib::Selenium; use t::lib::TestBuilder; @@ -43,10 +50,12 @@ my $opac_base_url = $s->opac_base_url; my $base_url = $s->base_url; my $builder = t::lib::TestBuilder->new; -our @cleanup; subtest 'Search patrons' => sub { - plan tests => 12; + plan tests => 17; + if ( Koha::Patrons->search({surname => {-like => "test_patron_%"}})->count ) { + BAIL_OUT("Cannot run this test, data we need to create already exist in the DB"); + } my @patrons; my $borrowernotes = q|just 'a" note \123 ❤|; my $borrowernotes_displayed = q|just 'a" note \123 ❤|; @@ -64,6 +73,7 @@ subtest 'Search patrons' => sub { { class => 'Koha::Libraries', value => { branchname => $branchname } } ); my $default_patron_search_fields = C4::Context->preference('DefaultPatronSearchFields'); + my $default_patron_per_page = C4::Context->preference('PatronsPerPage'); for my $i ( 1 .. 25 ) { push @patrons, $builder->build_object( @@ -81,9 +91,55 @@ subtest 'Search patrons' => sub { } ); } + my $library_2 = $builder->build_object( + { class => 'Koha::Libraries', value => { branchname => 'X' . $branchname } } + ); + push @patrons, + $builder->build_object( + { + class => 'Koha::Patrons', + value => { + surname => "test_patron_26", + firstname => $firstname, + categorycode => $patron_category->categorycode, + branchcode => $library_2->branchcode, + borrowernotes => $borrowernotes, + address => $address, + email => $email, + } + } + ); + + my $attribute_type = Koha::Patron::Attribute::Type->new( + { + code => 'my code1', + description => 'my description1', + } + )->store; + my $attribute_type_searchable = Koha::Patron::Attribute::Type->new( + { + code => 'my code2', + description => 'my description2', + opac_display => 1, + staff_searchable => 1 + } + )->store; + $patrons[0]->extended_attributes([ + { code => $attribute_type->code, attribute => 'test_attr_1' }, + { code => $attribute_type_searchable->code, attribute => 'test_attr_2'}, + ]); + $patrons[1]->extended_attributes([ + { code => $attribute_type->code, attribute => 'test_attr_1' }, + { code => $attribute_type_searchable->code, attribute => 'test_attr_2'}, + ]); + + my $total_number_of_patrons = Koha::Patrons->search->count; + my $table_id = "memberresultst"; $s->auth; C4::Context->set_preference('DefaultPatronSearchFields',""); + my $PatronsPerPage = 15; + C4::Context->set_preference('PatronsPerPage', $PatronsPerPage); $driver->get( $base_url . "/members/members-home.pl" ); my @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option'); my @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option'); @@ -106,8 +162,8 @@ subtest 'Search patrons' => sub { $s->submit_form; my $first_patron = $patrons[0]; - $s->wait_for_datatable_visible('//table[@id="memberresultst"]'); - my @td = $driver->find_elements('//table[@id="memberresultst"]/tbody/tr/td'); + $s->wait_for_ajax; + my @td = $driver->find_elements('//table[@id="'.$table_id.'"]/tbody/tr/td'); like ($td[2]->get_text, qr[\Q$firstname\E], 'Column "Name" should be the 3rd and contain the firstname correctly filtered' ); @@ -136,14 +192,63 @@ subtest 'Search patrons' => sub { $first_patron->category->description, ) ); + + $driver->get( $base_url . "/members/members-home.pl" ); + $s->fill_form( { search_patron_filter => 'test_patron' } ); + $s->submit_form; + $s->wait_for_ajax; + + $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys('test_patron'); + $s->wait_for_ajax; + is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', $PatronsPerPage, 26, $total_number_of_patrons) ); + + $s->driver->find_element('//table[@id="'.$table_id.'"]//th[@data-filter="libraries"]/select/option[@value="'.$library->branchcode.'"]')->click; + $s->wait_for_ajax; + is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', $PatronsPerPage, 25, $total_number_of_patrons) ); + + # Reset the filters + $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click(); + $s->submit_form; + $s->wait_for_ajax; + + # And make sure all the patrons are present + is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries', $PatronsPerPage, $total_number_of_patrons) ); + + # Search on non-searchable attribute, we expect no result! + $s->fill_form( { search_patron_filter => 'test_attr_1' } ); + $s->submit_form; + $s->wait_for_ajax; + + is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('No entries to show (filtered from %s total entries)', $total_number_of_patrons) ); + + # clear form + $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click(); + # Search on searchable attribute, we expect 2 patrons + $s->fill_form( { search_patron_filter => 'test_attr_2' } ); + $s->submit_form; + $s->wait_for_ajax; + + is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', 2, 2, $total_number_of_patrons) ); + + # Refine search and search for test_patron in all the data using the DT global search + # No change in result expected, still 2 patrons + $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys('test_patron'); + $s->wait_for_ajax; + is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', 2, 2, $total_number_of_patrons) ); + + # Adding the surname of the first patron in the "Name" column + # We expect only 1 result + $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys($patrons[0]->surname); + $s->wait_for_ajax; + is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', 1, 1, $total_number_of_patrons) ); + push @cleanup, $_ for @patrons; push @cleanup, $library; + push @cleanup, $library_2; push @cleanup, $patron_category; + push @cleanup, $attribute_type, $attribute_type_searchable; C4::Context->set_preference('DefaultPatronSearchFields',$default_patron_search_fields); + C4::Context->set_preference('PatronsPerPage',$default_patron_per_page); $driver->quit(); }; - -END { - $_->delete for @cleanup; -} diff --git a/t/lib/Selenium.pm b/t/lib/Selenium.pm index 1b24e66975..72b55f5c82 100644 --- a/t/lib/Selenium.pm +++ b/t/lib/Selenium.pm @@ -193,23 +193,19 @@ sub wait_for_element_visible { return $elt; } -sub wait_for_datatable_visible { - my ( $self, $xpath_selector ) = @_; +sub wait_for_ajax { + my ( $self ) = @_; - my ($visible, $elt); - $self->remove_error_handler; + my $is_ready; my $max_retries = $self->max_retries; my $i; - while ( not $visible ) { - $elt = eval {$self->driver->find_element($xpath_selector . '//td[class="dataTables_empty"]')}; - $visible = $elt && $elt->get_text ne 'No data available in table'; - $self->driver->pause(1000) unless $visible; + while ( not $is_ready ) { + $is_ready = $self->driver->execute_script('return jQuery.active == 0'); + $self->driver->pause(1000) unless $is_ready; - die "Cannot wait more for element '$xpath_selector' to be visible" + die "Cannot wait more for jQuery to be active (wait_for_ajax)" if $max_retries <= ++$i } - $self->add_error_handler; - return $elt; } sub show_all_entries { -- 2.20.1