MDL-66378 behat: Add non-JS fallback for before/after

This commit is contained in:
Andrew Nicols
2019-08-17 16:37:40 +08:00
parent a4dcb6a26a
commit 7d84120a0e
+54 -19
View File
@@ -763,33 +763,53 @@ class behat_general extends behat_base {
/**
* Checks, that the first specified element appears before the second one.
*
* @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
* @Then :preelement :preselectortype should appear before :postelement :postselectortype
* @throws ExpectationException
* @param string $preelement The locator of the preceding element
* @param string $preselectortype The locator of the preceding element
* @param string $postelement The locator of the latest element
* @param string $postselectortype The selector type of the latest element
*/
public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
$msg = '"' . $preelement . '" "' . $preselectortype .
'" does not appear before "' . $postelement . '" "' . $postselectortype . '"';
$this->check_element_order($preelement, $preselectortype, $postelement, $postselectortype, $msg);
public function should_appear_before(
string $preelement,
string $preselectortype,
string $postelement,
string $postselectortype
) {
$msg = "'{$preelement}' '{$preselectortype}' does not appear after '{$postelement}' '{$postselectortype}'";
$this->check_element_order(
$preelement,
$preselectortype,
$postelement,
$postselectortype,
$msg
);
}
/**
* Checks, that the first specified element appears after the second one.
*
* @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
* @Then :postelement :postselectortype should appear after :preelement :preselectortype
* @throws ExpectationException
* @param string $postelement The locator of the latest element
* @param string $postselectortype The selector type of the latest element
* @param string $preelement The locator of the preceding element
* @param string $preselectortype The locator of the preceding element
*/
public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
$msg = '"' . $postelement . '" "' . $postselectortype .
'" does not appear after "' . $preelement . '" "' . $preselectortype . '"';
$this->check_element_order($preelement, $preselectortype, $postelement, $postselectortype, $msg);
public function should_appear_after(
string $postelement,
string $postselectortype,
string $preelement,
string $preselectortype
) {
$msg = "'{$postelement}' '{$postselectortype}' does not appear after '{$preelement}' '{$preselectortype}'";
$this->check_element_order(
$preelement,
$preselectortype,
$postelement,
$postselectortype,
$msg
);
}
/**
@@ -801,21 +821,36 @@ class behat_general extends behat_base {
* @param string $postselectortype The selector type of the following element
* @param string $msg Message to output if this fails
*/
protected function check_element_order(string $preelement, string $preselectortype,
string $postelement, string $postselectortype, string $msg) {
protected function check_element_order(
string $preelement,
string $preselectortype,
string $postelement,
string $postselectortype,
string $msg
) {
list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
$prexpath = $this->find($preselector, $prelocator)->getXpath();
$postxpath = $this->find($postselector, $postlocator)->getXpath();
// The xpath to do this was running really slowly on certain Chrome versions so we are using
// this DOM method instead.
$ok = $this->getSession()->getDriver()->evaluateScript('return (function() { ' .
'var a = document.evaluate("' . $prexpath . '", document).iterateNext();' .
'var b = document.evaluate("' . $postxpath . '", document).iterateNext();' .
'return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_PRECEDING;})()'
);
if ($this->running_javascript()) {
// The xpath to do this was running really slowly on certain Chrome versions so we are using
// this DOM method instead.
$js = <<<EOF
(function() {
var a = document.evaluate("{$prexpath}", document).iterateNext();
var b = document.evaluate("{$postxpath}", document).iterateNext();
return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING;
})()
EOF;
$ok = $this->getSession()->getDriver()->evaluateScript($js);
} else {
// Using following xpath axe to find it.
$xpath = "{$prexpath}/following::*[contains(., {$postxpath})]";
$ok = $this->getSession()->getDriver()->find($xpath);
}
if (!$ok) {
throw new ExpectationException($msg, $this->getSession());