Merge pull request #8461 from creative-commoners/pulls/4.3/unbelabelable-dom-skills

NEW Adding a helper to find a form field by label content (Behat)
This commit is contained in:
Robbie Averill 2018-10-08 23:22:03 +02:00 committed by GitHub
commit 1d1cf6882d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -310,11 +310,38 @@ JS;
{
$locator = $this->fixStepArgument($locator);
$page = $this->getSession()->getPage();
// Searching by name is usually good...
$element = $page->find('css', 'textarea.htmleditor[name=\'' . $locator . '\']');
if ($element === null) {
$element = $this->findInputByLabelContent($locator);
}
assertNotNull($element, sprintf('HTML field "%s" not found', $locator));
return $element;
}
protected function findInputByLabelContent($locator)
{
$page = $this->getSession()->getPage();
$label = $page->findAll('xpath', sprintf('//label[contains(text(), \'%s\')]', $locator));
if (empty($label)) {
return null;
}
assertCount(1, $label, sprintf(
'Found more than one element containing the phrase "%s".',
$locator
));
$label = array_shift($label);
$fieldId = $label->getAttribute('for');
return $page->find('css', '#' . $fieldId);
}
/**
* @Given /^the "([^"]*)" field ((?:does not have)|(?:has)) property "([^"]*)"$/
*/