From 76431b1190543105b30dae94943db7d28db9289b Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 29 Jul 2021 22:05:50 +1200 Subject: [PATCH] NEW Add function to elements and select from dropdowns --- src/Context/BasicContext.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Context/BasicContext.php b/src/Context/BasicContext.php index 061a5f9..640f99e 100644 --- a/src/Context/BasicContext.php +++ b/src/Context/BasicContext.php @@ -1280,7 +1280,6 @@ JS; )); } - /** * We have to catch exceptions and log somehow else otherwise behat falls over * @@ -1290,4 +1289,38 @@ JS; { 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 = <<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); + } }