From 084980ea27fa6176028b2b660c6105497e217beb Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Thu, 28 Nov 2013 23:03:38 +1300 Subject: [PATCH] Adding definition for selecting an input by the group's label. Allows for a step like: `I select "Admins" from "Groups" input group` Adding it to BasicContext as it seems useful to be used in other places, not just the CMS admin interface. It could be used frontend forms that use CheckboxSetField, or OptionsetField, for example. Thanks to @srizzling for writing the definition for this step! --- README.md | 5 ++- .../BehatExtension/Context/BasicContext.php | 31 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 03a2cc2..174f6f1 100644 --- a/README.md +++ b/README.md @@ -503,7 +503,10 @@ It's based on the `vendor/bin/behat -di @cms` output. When /^(?:|I )fill in "(?P(?:[^"]|\\")*)" for "(?P(?:[^"]|\\")*)" dropdown$/ - Workaround for chosen.js dropdowns or tree dropdowns which hide the original dropdown field. - + Given /^I select "([^"]*)" from "([^"]*)" input group$/ + - Check an individual input button from a group of inputs + - Example: I select "Admins" from "Groups" input group + (where "Groups" is the title of the CheckboxSetField or OptionsetField form field) ### Interactions diff --git a/src/SilverStripe/BehatExtension/Context/BasicContext.php b/src/SilverStripe/BehatExtension/Context/BasicContext.php index 226d4ee..9ce09bd 100644 --- a/src/SilverStripe/BehatExtension/Context/BasicContext.php +++ b/src/SilverStripe/BehatExtension/Context/BasicContext.php @@ -392,7 +392,36 @@ JS; return new Step\Given(sprintf('I attach the file "%s" to "%s"', $path, $field)); } - /** + /** + * Select an individual input from within a group, matched by the top-most label. + * + * @Given /^I select "([^"]*)" from "([^"]*)" input group$/ + */ + public function iSelectFromInputGroup($value, $labelText) { + $page = $this->getSession()->getPage(); + $parent = null; + + foreach($page->findAll('css', 'label') as $label) { + if($label->getText() == $labelText) { + $parent = $label->getParent(); + } + } + + if(!$parent) throw new \InvalidArgumentException(sprintf('Input group with label "%s" cannot be found', $labelText)); + + foreach($parent->findAll('css', 'label') as $option) { + if($option->getText() == $value) { + $for = $option->getAttribute('for'); + $input = $parent->findById($for); + + if(!$input) throw new \InvalidArgumentException(sprintf('Input "%s" cannot be found', $value)); + + $this->getSession()->getDriver()->click($input->getXPath()); + } + } + } + + /** * Transforms relative time statements compatible with strtotime(). * Example: "time of 1 hour ago" might return "22:00:00" if its currently "23:00:00". * Customize through {@link setTimeFormat()}.