From e489df87bf6ab0aecdfd4f334f8c211192a99de3 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Thu, 23 May 2013 13:18:51 +0800 Subject: [PATCH 1/2] MDL-39795 behat: Improving find-texts-in-page-contents performance --- lib/tests/behat/behat_general.php | 26 ++++++++++++++++++++++---- lib/tests/behat/behat_hooks.php | 18 +++++++++++------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 5a2ba31e04a..c30c4c31e52 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -214,23 +214,41 @@ class behat_general extends behat_base { /** * Checks, that page contains specified text. * - * @see Behat\MinkExtension\Context\MinkContext * @Then /^I should see "(?P(?:[^"]|\\")*)"$/ + * @throws ExpectationException * @param string $text */ public function assert_page_contains_text($text) { - $this->assertSession()->pageTextContains($text); + + $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text); + $xpath = "/descendant::*[contains(., " . $xpathliteral. ")]"; + + // Wait until it finds the text, otherwise custom exception. + try { + $this->find('xpath', $xpath); + } catch (ElementNotFoundException $e) { + throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession()); + } } /** * Checks, that page doesn't contain specified text. * - * @see Behat\MinkExtension\Context\MinkContext * @Then /^I should not see "(?P(?:[^"]|\\")*)"$/ + * @throws ExpectationException * @param string $text */ public function assert_page_not_contains_text($text) { - $this->assertSession()->pageTextNotContains($text); + + $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text); + $xpath = "/descendant::*[not(contains(., " . $xpathliteral. "))]"; + + // Wait until it finds the text, otherwise custom exception. + try { + $this->find('xpath', $xpath); + } catch (ElementNotFoundException $e) { + throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession()); + } } /** diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 5cc8112cb65..3c297c2cb27 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -257,14 +257,18 @@ class behat_hooks extends behat_base { } // Any other backtrace. - $backtracespattern = '/(line [0-9]* of [^:]*: call to [\->&;:a-zA-Z_\x7f-\xff][\->&;:a-zA-Z0-9_\x7f-\xff]*)/'; - if (preg_match_all($backtracespattern, $this->getSession()->getPage()->getContent(), $backtraces)) { - $msgs = array(); - foreach ($backtraces[0] as $backtrace) { - $msgs[] = $backtrace . '()'; + // First looking through xpath as it is faster than get and parse the whole page contents, + // we get the contents and look for matches once we found something to suspect that there is a backtrace. + if ($this->getSession()->getDriver()->find("(//html/descendant::*[contains(., ': call to ')])[1]")) { + $backtracespattern = '/(line [0-9]* of [^:]*: call to [\->&;:a-zA-Z_\x7f-\xff][\->&;:a-zA-Z0-9_\x7f-\xff]*)/'; + if (preg_match_all($backtracespattern, $this->getSession()->getPage()->getContent(), $backtraces)) { + $msgs = array(); + foreach ($backtraces[0] as $backtrace) { + $msgs[] = $backtrace . '()'; + } + $msg = "Other backtraces found:\n" . implode("\n", $msgs); + throw new \Exception(htmlentities($msg)); } - $msg = "Other backtraces found:\n" . implode("\n", $msgs); - throw new \Exception(htmlentities($msg)); } } catch (NoSuchWindow $e) { From 0653761da2ff0b1b14e72fe198c22a5eb1f45810 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 27 May 2013 14:23:45 +0800 Subject: [PATCH 2/2] MDL-39795 behat: Adapting features to consider spaces --- backup/util/ui/tests/behat/restore_moodle2_courses.feature | 2 +- mod/lesson/tests/behat/time_limit.feature | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/backup/util/ui/tests/behat/restore_moodle2_courses.feature b/backup/util/ui/tests/behat/restore_moodle2_courses.feature index f415f07f92f..4c603718832 100644 --- a/backup/util/ui/tests/behat/restore_moodle2_courses.feature +++ b/backup/util/ui/tests/behat/restore_moodle2_courses.feature @@ -88,7 +88,7 @@ Feature: Restore Moodle 2 course backups | id_startdate_year | 2020 | | id_format | Weekly format | And I press "Save changes" - And I should see "1 January - 7 January" + And I should see "1 January - 7 January" And I should see "Test forum name" And I follow "Edit settings" And I expand all fieldsets diff --git a/mod/lesson/tests/behat/time_limit.feature b/mod/lesson/tests/behat/time_limit.feature index 97666e3f3a9..853fc352684 100644 --- a/mod/lesson/tests/behat/time_limit.feature +++ b/mod/lesson/tests/behat/time_limit.feature @@ -39,10 +39,11 @@ Feature: A teacher can set a time limit for a lesson And I wait "3" seconds And I should see "Time remaining" And I press "Single button" - And I should see " 0:00:" + And I should see "0:00:" And I should see "Warning: You have 1 minute or less to finish the lesson." And I wait "60" seconds And I press "Single button" - And I should see "Attention: You ran out of time for this lesson. Your last answer may not have counted if it was answered after the time was up." + And I should see "You ran out of time for this lesson." + And I should see "Your last answer may not have counted if it was answered after the time was up." And I should see "Congratulations - end of lesson reached" And I should not see "Single lesson page contents"