From 9ba504387135d99901878ca6da66f10be0d629c4 Mon Sep 17 00:00:00 2001 From: Jeffrey Guo Date: Wed, 30 Jul 2014 12:42:58 +1200 Subject: [PATCH] check a text is before or after another text added comments for each step check a text is before or after another text in an element --- .../BehatExtension/Context/BasicContext.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/SilverStripe/BehatExtension/Context/BasicContext.php b/src/SilverStripe/BehatExtension/Context/BasicContext.php index 4ae2866..c201c65 100644 --- a/src/SilverStripe/BehatExtension/Context/BasicContext.php +++ b/src/SilverStripe/BehatExtension/Context/BasicContext.php @@ -750,4 +750,26 @@ JS; return $table; } + /** + * Checks the order of two texts. + * Assumptions: the two texts appear in their conjunct parent element once + * @Then /^I should see the text "(?P(?:[^"]|\\")*)" (before|after) the text "(?P(?:[^"]|\\")*)" in the "(?P[^"]*)" element$/ + */ + public function theTextBeforeAfter($textBefore, $order, $textAfter, $element) { + $ele = $this->getSession()->getPage()->find('css', $element); + assertNotNull($ele, sprintf('%s not found', $element)); + + // Check both of the texts exist in the element + $text = $ele->getText(); + assertTrue(strpos($text, $textBefore) !== 'FALSE', sprintf('%s not found in the element %s', $textBefore, $element)); + assertTrue(strpos($text, $textAfter) !== 'FALSE', sprintf('%s not found in the element %s', $textAfter, $element)); + + /// Use strpos to get the position of the first occurrence of the two texts (case-sensitive) + // and compare them with the given order (before or after) + if($order === 'before') { + assertTrue(strpos($text, $textBefore) < strpos($text, $textAfter)); + } else { + assertTrue(strpos($text, $textBefore) > strpos($text, $textAfter)); + } + } }