Skip to content

Instantly share code, notes, and snippets.

@pbuyle
Last active January 25, 2024 23:25
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pbuyle/7698675 to your computer and use it in GitHub Desktop.
Save pbuyle/7698675 to your computer and use it in GitHub Desktop.
Behat step-definition to verify visibility (not just presence) of Drupal form elements.
@javascript
Scenario: Conditional display of textfield
Given I am on "/custon/form"
And I select the radio button "Show foo"
Then I should see a "Foo" textfield form element
And I should not see a "Bar" textfield form element
<?php
use Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Context\BehatContext,
Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
/**
* Features context.
*/
class FeatureContext extends \Drupal\DrupalExtension\Context\DrupalContext {
/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
*/
public function __construct(array $parameters) {
// Initialize your context here
}
/**
* Checks, that form element with specified label is visible on page.
*
* @Then /^(?:|I )should see an? "(?P<label>[^"]*)" form element$/
*/
public function assertFormElementOnPage($label) {
$element = $this->getSession()->getPage();
$nodes = $element->findAll('css', '.form-item label');
foreach ($nodes as $node) {
if ($node->getText() === $label) {
if ($node->isVisible()) {
return;
}
else {
throw new \Exception("Form item with label \"$label\" not visible.");
}
}
}
throw new \Behat\Mink\Exception\ElementNotFoundException($this->getSession(), 'form item', 'label', $label);
}
/**
* Checks, that form element with specified label and type is visible on page.
*
* @Then /^(?:|I )should see an? "(?P<label>[^"]*)" (?P<type>[^"]*) form element$/
*/
public function assertTypedFormElementOnPage($label, $type) {
$container = $this->getSession()->getPage();
$pattern = '/(^| )form-type-' . preg_quote($type). '($| )/';
$label_nodes = $container->findAll('css', '.form-item label');
foreach ($label_nodes as $label_node) {
// Note: getText() will return an empty string when using Selenium2D. This
// is ok since it will cause a failed step.
if ($label_node->getText() === $label
&& preg_match($pattern, $label_node->getParent()->getAttribute('class'))
&& $label_node->isVisible()) {
return;
}
}
throw new \Behat\Mink\Exception\ElementNotFoundException($this->getSession(), $type . ' form item', 'label', $label);
}
/**
* Checks, that element with specified CSS is not visible on page.
*
* @Then /^(?:|I )should not see an? "(?P<label>[^"]*)" form element$/
*/
public function assertFormElementNotOnPage($label) {
$element = $this->getSession()->getPage();
$nodes = $element->findAll('css', '.form-item label');
foreach ($nodes as $node) {
// Note: getText() will return an empty string when using Selenium2D. This
// is ok since it will cause a failed step.
if ($node->getText() === $label && $node->isVisible()) {
throw new \Exception();
}
}
}
/**
* Checks, that form element with specified label and type is not visible on page.
*
* @Then /^(?:|I )should not see an? "(?P<label>[^"]*)" (?P<type>[^"]*) form element$/
*/
public function assertTypedFormElementNotOnPage($label, $type) {
$container = $this->getSession()->getPage();
$pattern = '/(^| )form-type-' . preg_quote($type). '($| )/';
$label_nodes = $container->findAll('css', '.form-item label');
foreach ($label_nodes as $label_node) {
// Note: getText() will return an empty string when using Selenium2D. This
// is ok since it will cause a failed step.
if ($label_node->getText() === $label
&& preg_match($pattern, $label_node->getParent()->getAttribute('class'))
&& $label_node->isVisible()) {
throw new \Behat\Mink\Exception\ElementNotFoundException($this->getSession(), $type . ' form item', 'label', $label);
}
}
}
}
Copyright (c) 2014 Pierre Buyle
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@drupality
Copy link

Are you using Behat\Mink\Driver\Selenium2Driver with this? For me seems like every time page content is empty, therefore none fields are available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment