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
This commit is contained in:
Jeffrey Guo 2014-07-30 12:42:58 +12:00
parent 8b020593b7
commit 9ba5043871
1 changed files with 22 additions and 0 deletions

View File

@ -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<textBefore>(?:[^"]|\\")*)" (before|after) the text "(?P<textAfter>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" 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));
}
}
}