Skip to content

Commit

Permalink
Merge pull request #203 from creative-commoners/pulls/4/new-functions
Browse files Browse the repository at this point in the history
NEW Add function to elements and select from dropdowns
  • Loading branch information
Maxime Rainville authored Aug 4, 2021
2 parents 91be987 + 76431b1 commit ba2e931
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Context/BasicContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,6 @@ public function spin($lambda, $wait = 60)
));
}


/**
* We have to catch exceptions and log somehow else otherwise behat falls over
*
Expand All @@ -1290,4 +1289,38 @@ protected function logException(Exception $exception)
{
file_put_contents('php://stderr', 'Exception caught: ' . $exception->getMessage());
}

/**
* Detect element with javascript, rather than having the selector converted to xpath
* There's already an xpath based function 'I see the "" element' iSeeTheElement() in silverstripe/cms
* There's also an 'I should see "" element' in MinkContext which also converts the css selector to xpath
*
* @When /^I should see the "([^"]+)" element/
* @param $selector
*/
public function iShouldSeeTheElement($selector)
{
$sel = str_replace('"', '\\"', $selector);
$js = <<<JS
return document.querySelector("$sel");
JS;
$element = $this->getSession()->evaluateScript($js);
assertNotNull($element, sprintf('Element %s not found', $selector));
}

/**
* Selects the option in select field with specified id|name|label|value.
* Note: this is duplicate code from SilverStripeContext selectOption
* In practice, the primary context file using in modules have inherited from BasicContext
* and not SilverStripeContext so the selectOption method is not available.
*
* @When /^I select "([^"]+)" from the "([^"]+)" field$/
* @param string $value
* @param string $locator - select id, name or label - NOT a css selector
*/
public function iSelectFromTheField($value, $locator)
{
$val = str_replace('"', '\\"', $value);
$this->getSession()->getPage()->selectFieldOption($locator, $val);
}
}

0 comments on commit ba2e931

Please sign in to comment.