From 2775895d03af93d207079637a8eb6afdfad5ab01 Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Mon, 8 Oct 2018 14:28:10 +1300 Subject: [PATCH] NEW Adding a helper to find a form field by label content --- tests/behat/src/CmsFormsContext.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/behat/src/CmsFormsContext.php b/tests/behat/src/CmsFormsContext.php index 624b0363f..bc85fee7d 100644 --- a/tests/behat/src/CmsFormsContext.php +++ b/tests/behat/src/CmsFormsContext.php @@ -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 "([^"]*)"$/ */