Bug 29368: Fix remove_from_cart
[koha.git] / t / db_dependent / selenium / remove_from_cart.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use File::Basename qw(dirname );
20
21 use Test::WWW::Mechanize;
22 use Test::More tests => 1;
23 use Test::MockModule;
24
25 use C4::Context;
26 use t::lib::Mocks;
27 use t::lib::Selenium;
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
30 use t::lib::Mocks::Zebra;
31
32 eval { require Selenium::Remote::Driver; };
33 skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
34
35 my $s = t::lib::Selenium->new;
36
37 my $driver = $s->driver;
38 my $opac_base_url = $s->opac_base_url;
39 my $base_url = $s->base_url;
40 my $builder = t::lib::TestBuilder->new;
41
42 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
43
44 my $SearchEngine_value = C4::Context->preference('SearchEngine');
45 C4::Context->set_preference('SearchEngine', 'Zebra');
46
47 my $mock_zebra = t::lib::Mocks::Zebra->new(
48     {
49         koha_conf   => $ENV{KOHA_CONF},
50         marcflavour => $marcflavour,
51     }
52 );
53
54 subtest 'OPAC - Remove from cart' => sub {
55     plan tests => 4;
56
57     my $sourcedir = dirname(__FILE__) . "/../data";
58     $mock_zebra->load_records(
59         sprintf( "%s/%s/zebraexport/biblio", $sourcedir, lc($marcflavour) ),
60         'iso2709', 'biblios', 1 );
61     $mock_zebra->launch_zebra;
62
63     # We need to prevent scrolling to prevent the floating toolbar from overlapping buttons we are testing
64     my $window_size = $driver->get_window_size();
65     $driver->set_window_size(1920,10800);
66
67     $driver->get( $opac_base_url . "opac-search.pl?q=d" );
68
69     # A better way to do that would be to modify the way we display the basket count
70     # We should show/hide the count instead or recreate the node
71     my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span');
72     is( scalar(@basket_count_elts), 0, 'Basket should be empty');
73
74     # This will fail if nothing is indexed, but at this point we should have everything setup correctly
75     my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]');
76     my $biblionumber1 = $checkboxes[0]->get_value();
77     my $biblionumber3 = $checkboxes[2]->get_value();
78     my $biblionumber5 = $checkboxes[4]->get_value();
79
80     $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber1.'"]')->click;
81     my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
82     is( $basket_count_elt->get_text(),
83         1, 'One element should have been added to the cart' );
84
85     $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber3.'"]')->click;
86     $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber5.'"]')->click;
87     $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
88     is( $basket_count_elt->get_text(),
89         3, '3 elements should have been added to the cart' );
90
91     $driver->find_element('//a[@class="btn btn-link btn-sm remove cartRemove cartR'.$biblionumber3.'"]')->click;
92     $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
93     is( $basket_count_elt->get_text(),
94         2, '1 element should have been removed from the cart' );
95
96     # Reset window size
97     $driver->set_window_size($window_size->{'height'}, $window_size->{'width'});
98
99     $mock_zebra->cleanup;
100 };
101
102 END {
103     C4::Context->set_preference('SearchEngine', $SearchEngine_value);
104     $mock_zebra->cleanup;
105 };