Permalink
Browse files

MDL-39795 behat: Improving find-texts-in-page-contents performance

  • Loading branch information...
1 parent f65261d commit e489df87bf6ab0aecdfd4f334f8c211192a99de3 @dmonllao dmonllao committed May 23, 2013
Showing with 33 additions and 11 deletions.
  1. +22 −4 lib/tests/behat/behat_general.php
  2. +11 −7 lib/tests/behat/behat_hooks.php
@@ -214,23 +214,41 @@ public function i_drag_and_i_drop_it_in($element, $selectortype, $containereleme
/**
* Checks, that page contains specified text.
*
- * @see Behat\MinkExtension\Context\MinkContext
* @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
+ * @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<text_string>(?:[^"]|\\")*)"$/
+ * @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());
+ }
}
/**
@@ -257,14 +257,18 @@ public function i_look_for_exceptions() {
}
// 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) {

0 comments on commit e489df8

Please sign in to comment.