From 1b2c35af340277b448a183af2c1b04e11244cbea Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 2 Dec 2014 08:57:38 +0800 Subject: [PATCH 1/2] MDL-48374 behat: improved page load detection Check that page load detection was correctly started before testing that a new page was loaded. Without this, it is possible to have mutliple subsequent cases of: And a new page should have loaded since I started watching Without first starting the page load detection. --- lib/tests/behat/behat_general.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index b4e446508ca..ae910a7bdbb 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -63,6 +63,12 @@ class behat_general extends behat_base { */ const PAGE_LOAD_DETECTION_STRING = 'new_page_not_loaded_since_behat_started_watching'; + /** + * @var $pageloaddetectionrunning boolean Used to ensure that page load detection was started before a page reload + * was checked for. + */ + private $pageloaddetectionrunning = null; + /** * Opens Moodle homepage. * @@ -1246,6 +1252,8 @@ class behat_general extends behat_base { throw new ExpectationException('Page load expectation error: page reloads are already been watched for.'); } + $this->pageloaddetectionrunning = true; + $this->getSession()->evaluateScript( 'var span = document.createElement("span"); span.setAttribute("data-rel", "' . self::PAGE_LOAD_DETECTION_STRING . '"); @@ -1269,6 +1277,15 @@ class behat_general extends behat_base { $this->getSession() ); } + + if (!$this->pageloaddetectionrunning) { + throw new ExpectationException( + 'Page load expectation error: page load tracking was not started.', + $this->getSession()); + } + + // Cancel the trakcing of pageloaddetectionrunning. + $this->pageloaddetectionrunning = false; } /** From 9f3a68fe612076b130c6216bc8549bb427f5f981 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Tue, 2 Dec 2014 11:40:33 +0800 Subject: [PATCH 2/2] MDL-48374 behat: Check flag before searching for span on page --- lib/tests/behat/behat_general.php | 41 +++++++++++++++++++------------ 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index ae910a7bdbb..6c52a44c4b2 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -67,7 +67,7 @@ class behat_general extends behat_base { * @var $pageloaddetectionrunning boolean Used to ensure that page load detection was started before a page reload * was checked for. */ - private $pageloaddetectionrunning = null; + private $pageloaddetectionrunning = false; /** * Opens Moodle homepage. @@ -1246,15 +1246,18 @@ class behat_general extends behat_base { throw new DriverException('Page load detection requires JavaScript.'); } - if ($this->getSession()->getPage()->find('xpath', $this->get_page_load_xpath())) { + $session = $this->getSession(); + + if ($this->pageloaddetectionrunning || $session->getPage()->find('xpath', $this->get_page_load_xpath())) { // If we find this node at this point we are already watching for a reload and the behat steps // are out of order. We will treat this as an error - really it needs to be fixed as it indicates a problem. - throw new ExpectationException('Page load expectation error: page reloads are already been watched for.'); + throw new ExpectationException( + 'Page load expectation error: page reloads are already been watched for.', $session); } $this->pageloaddetectionrunning = true; - $this->getSession()->evaluateScript( + $session->evaluateScript( 'var span = document.createElement("span"); span.setAttribute("data-rel", "' . self::PAGE_LOAD_DETECTION_STRING . '"); span.setAttribute("style", "display: none;"); @@ -1268,23 +1271,23 @@ class behat_general extends behat_base { * @Given /^a new page should have loaded since I started watching$/ */ public function a_new_page_should_have_loaded_since_i_started_watching() { - // As the node is inserted by code above it is either there or not, and we do not need spin and it is safe - // to use the native API here which is great as exception handling (the alternative is slow). - if ($this->getSession()->getPage()->find('xpath', $this->get_page_load_xpath())) { - // We don't want to find this node, if we do we have an error. - throw new ExpectationException( - 'Page load expectation error: a new page has not been loaded when it should have been.', - $this->getSession() - ); - } + $session = $this->getSession(); + // Make sure page load tracking was started. if (!$this->pageloaddetectionrunning) { throw new ExpectationException( - 'Page load expectation error: page load tracking was not started.', - $this->getSession()); + 'Page load expectation error: page load tracking was not started.', $session); } - // Cancel the trakcing of pageloaddetectionrunning. + // As the node is inserted by code above it is either there or not, and we do not need spin and it is safe + // to use the native API here which is great as exception handling (the alternative is slow). + if ($session->getPage()->find('xpath', $this->get_page_load_xpath())) { + // We don't want to find this node, if we do we have an error. + throw new ExpectationException( + 'Page load expectation error: a new page has not been loaded when it should have been.', $session); + } + + // Cancel the tracking of pageloaddetectionrunning. $this->pageloaddetectionrunning = false; } @@ -1295,6 +1298,12 @@ class behat_general extends behat_base { * @Given /^a new page should not have loaded since I started watching$/ */ public function a_new_page_should_not_have_loaded_since_i_started_watching() { + // Make sure page load tracking was started. + if (!$this->pageloaddetectionrunning) { + throw new ExpectationException( + 'Page load expectation error: page load tracking was not started.', $session); + } + // We use our API here as we can use the exception handling provided by it. $this->find( 'xpath',