So, I ran across a problem of identifying radio buttons in my Gherkin. Wanted to be able to look at a page, find the 'heading' of a set of buttons, and see if the one I want is 'checked'. Then the "Error messages to display" radio button should be set to "None" (taken from admin/config/development/logging). Turned out to not be quite as easy as I had hoped for, there is a disconnect between the label and the radio buttons. The label is for the div that contains the buttons, rather than the "name" of the buttons. So, I built this definition to look for a div with the specified label, check if it contains radio buttons, then get the label of the "checked" radio button.
/**
* @Then /^the "([^"]*)" radio button should be set to "([^"]*)"$/
*/
public function theRadioButtonShouldBeSetTo($identifier, $option) {
$page = $this->getSession()->getPage();
$div = $page->find('xpath', "//div[contains(., '$identifier') and @class[contains(.,'form-type-radio')]]");
if ($div) {
$radios = $div->find('xpath', "//input[@type='radio']");
if ($radios) {
$checkedRadio = $div->find('xpath', "//input[@type='radio' and @checked='checked']/following-sibling::label[contains(text(), '$option')] ");
if (!$checkedRadio) {
throw new \Exception(sprintf('We found the radio buttons for "%s", but "%s" was not selected.', $identifier, $option));
}
} elseif (!$radios) {
throw new \Exception(sprintf('We found "%s", but it did not contain any radio buttons".', $identifier));
}
} elseif (!$div) {
throw new \Exception(sprintf('We couldn\'nt find "%s" on the page', $identifier));
} else {
throw new \Exception('General exception from '.__FUNCTION__);
}
}Our use-case is to run a series of tests against a live site (but not change anything on live), and make sure that a deployment checklist was followed by testing the values of various settings.
Comments
Comment #1
eliza411 commentedThis seems really useful.
If the disconnected label is the result of Drupalish markup, I'd +1 getting this into the extension. If it's a more general web pattern, it might be more appropriate to post and issue for the MinkExtension.
@jhedstrom, your thoughts?
Comment #2
jhedstromI'd be up for adding this to the Drupal Extension, or helping review a pull request directly to the Mink Extension.
Comment #3
alex.skrypnykPlease see https://github.com/drevops/behat-steps/blob/main/src/FieldTrait.php#L630