From 9e26821cea59b6200d5f327875281adc6feff966 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Tue, 7 Jul 2015 14:12:29 +0000 Subject: [PATCH] Bug 13691 - PageObjectPattern implementation. --- t/db_dependent/Koha/Auth.t | 68 +++++++++++++++++++++++ t/lib/Page.pm | 136 +++++++++++++++++++++++++++++++++++++++++++++ t/lib/Page/Mainpage.pm | 45 +++++++++++++++ 3 files changed, 249 insertions(+) create mode 100644 t/db_dependent/Koha/Auth.t create mode 100644 t/lib/Page.pm create mode 100644 t/lib/Page/Mainpage.pm diff --git a/t/db_dependent/Koha/Auth.t b/t/db_dependent/Koha/Auth.t new file mode 100644 index 0000000..061e54d --- /dev/null +++ b/t/db_dependent/Koha/Auth.t @@ -0,0 +1,68 @@ +#!/usr/bin/env perl + +# Copyright 2015 Open Source Freedom Fighters +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More; #Please don't set the test count here. It is nothing but trouble when rebasing against master + #and is of dubious help, especially since we are running dynamic tests here which are triggered + #based on the reported test infrastucture capabilities. +use Try::Tiny; #Even Selenium::Remote::Driver uses Try::Tiny :) + +use t::lib::Page::Mainpage; + +use C4::Members; +use Koha::Database; +use Koha::Borrower; + +#my $builder = t::lib::TestBuilder->new(); +#$builder->build({ +# source => 'UserPermission', +# value => {}, +# only_fk => undef, +#}); + +my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode(); +my $password = '1234'; +##Setting up test context Koha-style! +#This should be refactored when a proper automagic setUp-tearDown framework for integration tests is deployed. +my $borrower = C4::Members::GetMember(cardnumber => '11A001'); #These tests can crash and leave the context behind, +my $borrowernumber = $borrower->{borrowernumber} if $borrower; #so when we rerun them, we get nasty issues with existing DB objects. +unless ($borrower) { + $borrowernumber = C4::Members::AddMember( + firstname => 'Olli', + lastname => 'Kivi', + categorycode => 'PT', + userid => '11Aadmin', + password => $password, + cardnumber => '11A001', + dateofbirth => DateTime->now(time_zone => C4::Context->tz)->subtract(years => 21), #I am always 21 :) + flags => '1', #Giving specific permission flags here is REALLY HARD! Just giving superlibrarian-permission. + branchcode => 'CPL', + ); + $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber); +} + +my $mainpage = t::lib::Page::Mainpage->new(); +$mainpage->isPasswordLoginAvailable()->doPasswordLogin($borrower->{userid}, $password); +#my $ss = $mainpage->getDriver()->get_page_source(); + + +#TearDown test context.. +Koha::Database->new()->schema()->resultset('Borrower')->search({borrowernumber => $borrower->{borrowernumber}})->delete_all(); +done_testing; \ No newline at end of file diff --git a/t/lib/Page.pm b/t/lib/Page.pm new file mode 100644 index 0000000..f101944 --- /dev/null +++ b/t/lib/Page.pm @@ -0,0 +1,136 @@ +package t::lib::Page; + +# Copyright 2015 Open Source Freedom Fighters +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Test::More; + +use t::lib::WebDriverFactory; + +use Koha::Exception::BadParameter; + +=head NAME t::lib::Page + +=head SYNOPSIS + +PageObject-pattern parent class + +=head Class variables + +Selenium::Remote::Driver driver, contains the driver implementation used to run these tests +t::Page::Common::Header header, the header page component +t::Page::Common::Footer footer, the footer page component +=cut + +our $hostProtocol = 'http'; +our $hostAddress = 'localhost'; +our $hostPort = '443'; + +sub new { + my ($class, $params) = @_; + $params = _mergeDefaultConfig($params); + + my $self = {}; + bless($self, $class); + unless ($params->{driver}) { + my ($driver) = t::lib::WebDriverFactory::getUserAgentDrivers({phantomjs => $params}); + $self->{driver} = $driver; + } + $self->{type} = $params->{type}; #This parameter is mandatory. _mergeDefaultConfig() dies without it. + $self->{resource} = $params->{resource} || '/'; + $self->{header} = $params->{header} || undef; + $self->{footer} = $params->{footer} || undef; + + $self->{driver}->get( $params->{resource} ); + return $self; +} + +=head _mergeDefaultConfig + +@THROWS Koha::Exception::BadParameter +=cut + +sub _mergeDefaultConfig { + my ($params) = @_; + unless (ref($params) eq 'HASH' && $params->{type}) { + Koha::Exception::BadParameter->throw(error => "t::lib::Page:> When instantiating Page-objects, you must define the 'type'-parameter."); + } + + my $testServerConfigs = C4::Context->config('testservers'); + my $conf = $testServerConfigs->{ $params->{type} }; + Koha::Exception::BadParameter->throw(error => "t::lib::Page:> Unknown 'type'-parameter '".$params->{type}."'. Values 'opac', 'staff' and 'rest' are supported.") + unless $conf; + #Merge given $params-config on top of the $KOHA_CONF's testservers-directives + @$conf{keys %$params} = values %$params; + return $conf; +} + +=head isPasswordLoginAvailable + + $page->isPasswordLoginAvailable(); + +@RETURN t::lib::Page-object +@CROAK if password login is unavailable. +=cut + +sub isPasswordLoginAvailable { + my $self = shift; + my $d = $self->getDriver(); + + _getPasswordLoginElements($d); + ok(($d->get_title() =~ /Log in to Koha/), "PasswordLoginAvailable"); + return $self; +} +sub doPasswordLogin { + my ($self, $username, $password) = @_; + my $d = $self->getDriver(); + + my ($submitButton, $useridInput, $passwordInput) = _getPasswordLoginElements($d); + $useridInput->send_keys($username); + $passwordInput->send_keys($password); + $submitButton->click(); + + my $cookies = $d->get_all_cookies(); + my @cgisessid = grep {$_->{name} eq 'CGISESSID'} @$cookies; + + ok(($d->get_title() !~ /Log in to Koha/ && #No longer in the login page + $cgisessid[0]) #Cookie CGISESSID defined! + , "PasswordLoginSucceeded"); + + return $self; #After a succesfull password login, we are directed to the same page we tried to access. +} +sub _getPasswordLoginElements { + my $d = shift; + my $submitButton = $d->find_element('#submit'); + my $useridInput = $d->find_element('#userid'); + my $passwordInput = $d->find_element('#password'); + return ($submitButton, $useridInput, $passwordInput); +} + +################################################ + ## INTRODUCING OBJECT ACCESSORS ## +################################################ +sub setDriver { + my ($self, $driver) = @_; + $self->{driver} = $driver; +} +sub getDriver { + my ($self) = @_; + return $self->{driver}; +} +1; #Make the compiler happy! \ No newline at end of file diff --git a/t/lib/Page/Mainpage.pm b/t/lib/Page/Mainpage.pm new file mode 100644 index 0000000..2da4bf9 --- /dev/null +++ b/t/lib/Page/Mainpage.pm @@ -0,0 +1,45 @@ +package t::lib::Page::Mainpage; + +# Copyright 2015 Open Source Freedom Fighters +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use base qw(t::lib::Page); + +=head NAME t::lib::Page::Mainpage + +=head SYNOPSIS + +Mainpage PageObject providing page functionality as a service! + +=cut + +sub new { + my ($class, $params) = @_; + unless (ref($params) eq 'HASH') { + $params = {}; + } + $params->{resource} = '/cgi-bin/koha/mainpage.pl'; + $params->{type} = 'staff'; + my $self = $class->SUPER::new($params); + + return $self; +} + + +1; #Make the compiler happy! \ No newline at end of file -- 1.9.1