From a6ece539c212c6d0deebe67ed7edb9e172b99956 Mon Sep 17 00:00:00 2001
From: Lari Taskula <larit@student.uef.fi>
Date: Wed, 19 Aug 2015 14:13:12 +0000
Subject: [PATCH] Bug 14536: (follow-up) Adds polling to Page.pm and enhances
 navigation.

This follow-up adds a subroutine poll to superclass Page.pm. Polling will
execute an anonymous subroutine for as long as defined or until the subroutine
is successful.

Also adds enchancements to navigation success confirmations. In earlier versions,
the modify patron page (memberentry.pl) defines page title as follows:
Modify <category> patron <name>

But in current version it is defined as follows:
Modify patron <name> (<category>)

Instead of checking for title "Modify patron", we now check for "Modify(.*)patron"
for determining if we are on the right memberentry.pl page.
---
 t/lib/Page.pm                    | 39 +++++++++++++++++++++++++++++++++++++++
 t/lib/Page/Members/Moremember.pm | 10 +++++-----
 t/lib/Page/Members/Toolbar.pm    | 14 +++++++++++---
 3 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/t/lib/Page.pm b/t/lib/Page.pm
index 595eab0..5f112db 100644
--- a/t/lib/Page.pm
+++ b/t/lib/Page.pm
@@ -150,6 +150,45 @@ sub pause {
     return $self;
 }
 
+=head poll
+Polls anonymous subroutine $func at given rate $pauseMillis for given times $polls or
+until $func succeeds without exceptions.
+
+In case of an exception, optional anonymous subroutine $success is called to confirm
+whether or not the action was successful. If this subroutine is not defined or it returns
+false, polling continues.
+
+Default pause for polling is 50ms and the polling runs by default for 20 times.
+
+@PARAM1 $func                  Anonymous subroutine to be polled
+@PARAM2 $success      OPTIONAL Success function to check if action was successful
+@PARAM3 $polls        OPTIONAL Defines the times polling will be ran
+@PARAM4 $pauseMillis  OPTIONAL Defines the wait between two polls
+
+@RETURNS 1 if polling was success, otherwise die
+=cut
+
+sub poll {
+    my ($self, $func, $success, $polls, $pauseMillis) = @_;
+    
+    # initialize default values if not given
+    $polls = 20 unless defined $polls;
+    $pauseMillis = 50 unless defined $pauseMillis;
+    
+    for (my $i = 0; $i < $polls; $i++){
+        eval {
+            &$func();
+        };
+        if ($@) {
+            return 1 if defined $success and &$success();
+            $self->getDriver()->pause($pauseMillis);
+            next;
+        }
+        return 1 unless $@; # if no errors, return true
+    }
+    die $@;
+}
+
 =head mockConfirmPopup
 
 Workaround to a missing feature in PhantomJS v1.9
diff --git a/t/lib/Page/Members/Moremember.pm b/t/lib/Page/Members/Moremember.pm
index 0a6cd6d..c55e5d7 100644
--- a/t/lib/Page/Members/Moremember.pm
+++ b/t/lib/Page/Members/Moremember.pm
@@ -139,7 +139,7 @@ sub navigateToPatronInformationEdit {
 
     my $elements = $self->_getEditLinks();
     $elements->{patron_information}->click();
-    ok($d->get_title() =~ m/Modify patron/, "Intra Navigate to Modify patron information");
+    ok($d->get_title() =~ m/Modify(.*)patron/, "Intra Navigate to Modify patron information");
 
     $self->debugTakeSessionSnapshot();
 
@@ -153,7 +153,7 @@ sub navigateToSMSnumberEdit {
 
     my $elements = $self->_getEditLinks();
     $elements->{smsnumber}->click();
-    ok($d->get_title() =~ m/Modify patron/, "Intra Navigate to Modify patron SMS number");
+    ok($d->get_title() =~ m/Modify(.*)patron/, "Intra Navigate to Modify patron SMS number");
 
     $self->debugTakeSessionSnapshot();
 
@@ -167,7 +167,7 @@ sub navigateToLibraryUseEdit {
 
     my $elements = $self->_getEditLinks();
     $elements->{library_use}->click();
-    ok($d->get_title() =~ m/Modify patron/, "Intra Navigate to Modify patron Library use");
+    ok($d->get_title() =~ m/Modify(.*)patron/, "Intra Navigate to Modify patron Library use");
 
     $self->debugTakeSessionSnapshot();
 
@@ -181,7 +181,7 @@ sub navigateToAlternateAddressEdit {
 
     my $elements = $self->_getEditLinks();
     $elements->{alternate_address}->click();
-    ok($d->get_title() =~ m/Modify patron/, "Intra Navigate to Modify patron Alternate address");
+    ok($d->get_title() =~ m/Modify(.*)patron/, "Intra Navigate to Modify patron Alternate address");
 
     $self->debugTakeSessionSnapshot();
 
@@ -195,7 +195,7 @@ sub navigateToAlternativeContactEdit {
 
     my $elements = $self->_getEditLinks();
     $elements->{alternative_contact}->click();
-    ok($d->get_title() =~ m/Modify patron/, "Intra Navigate to Modify patron Alternative contact");
+    ok($d->get_title() =~ m/Modify(.*)patron/, "Intra Navigate to Modify patron Alternative contact");
 
     $self->debugTakeSessionSnapshot();
 
diff --git a/t/lib/Page/Members/Toolbar.pm b/t/lib/Page/Members/Toolbar.pm
index 132a588..d01fce2 100644
--- a/t/lib/Page/Members/Toolbar.pm
+++ b/t/lib/Page/Members/Toolbar.pm
@@ -132,9 +132,17 @@ sub navigateEditPatron {
     $self->debugTakeSessionSnapshot();
 
     my $elements = $self->_getToolbarActionElements();
-    $elements->{edit}->click();
-    ok($d->get_title() =~ m/Modify patron/, "Intra Navigate to Modify patron");
-
+    
+    my $func = sub {
+        $elements->{edit}->click();
+    };
+    my $success = sub {
+        return $self->getDriver()->get_title() =~ m/Modify(.*)patron/;
+    };
+    
+    $self->poll($func, $success, 20, 50);
+    
+    ok($d->get_title() =~ m/Modify(.*)patron/, "Intra Navigate to Modify patron");
     $self->debugTakeSessionSnapshot();
 
     return t::lib::Page::Members::Memberentry->rebrandFromPageObject($self);
-- 
1.9.1