Skip to content

Commit

Permalink
MDL-66378 behat: Before/after detection should be constrainable
Browse files Browse the repository at this point in the history
The default before/after detection checks the entire body. Any match of
the text will therefore be found.

Add support for specification of a container for the nodes.
  • Loading branch information
andrewnicols committed Aug 17, 2019
1 parent 17a7dc9 commit 7d6d232
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions lib/tests/behat/behat_general.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,20 +764,27 @@ function($context, $args) {
* Checks, that the first specified element appears before the second one.
*
* @Then :preelement :preselectortype should appear before :postelement :postselectortype
* @Then :preelement :preselectortype should appear before :postelement :postselectortype in the :containerelement :containerselectortype
* @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
* @param string $containerelement
* @param string $containerselectortype
*/
public function should_appear_before(
string $preelement,
string $preselectortype,
string $postelement,
string $postselectortype
string $postselectortype,
?string $containerelement = null,
?string $containerselectortype = null
) {
$msg = "'{$preelement}' '{$preselectortype}' does not appear after '{$postelement}' '{$postselectortype}'";
$this->check_element_order(
$containerelement,
$containerselectortype,
$preelement,
$preselectortype,
$postelement,
Expand All @@ -790,20 +797,27 @@ public function should_appear_before(
* Checks, that the first specified element appears after the second one.
*
* @Then :postelement :postselectortype should appear after :preelement :preselectortype
* @Then :postelement :postselectortype should appear after :preelement :preselectortype in the :containerelement :containerselectortype
* @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
* @param string $containerelement
* @param string $containerselectortype
*/
public function should_appear_after(
string $postelement,
string $postselectortype,
string $preelement,
string $preselectortype
string $preselectortype,
?string $containerelement = null,
?string $containerselectortype = null
) {
$msg = "'{$postelement}' '{$postselectortype}' does not appear after '{$preelement}' '{$preselectortype}'";
$this->check_element_order(
$containerelement,
$containerselectortype,
$preelement,
$preselectortype,
$postelement,
Expand All @@ -815,24 +829,35 @@ public function should_appear_after(
/**
* Shared code to check whether an element is before or after another one.
*
* @param string $containerelement
* @param string $containerselectortype
* @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 following element
* @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 $containerelement,
?string $containerselectortype,
string $preelement,
string $preselectortype,
string $postelement,
string $postselectortype,
string $msg
) {
$containernode = false;
if ($containerselectortype && $containerelement) {
// Get the container node.
$containernode = $this->get_selected_node($containerselectortype, $containerelement);
$msg .= " in the '{$containerelement}' '{$containerselectortype}'";
}

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();
$prexpath = $this->find($preselector, $prelocator, false, $containernode)->getXpath();
$postxpath = $this->find($postselector, $postlocator, false, $containernode)->getXpath();

if ($this->running_javascript()) {
// The xpath to do this was running really slowly on certain Chrome versions so we are using
Expand Down

0 comments on commit 7d6d232

Please sign in to comment.