Commit 5c93d7e3 authored by catch's avatar catch

Issue #2767655 by claudiu.cristea, dawehner, alexpott: Allow WebAssert to...

Issue #2767655 by claudiu.cristea, dawehner, alexpott: Allow WebAssert to inspect also hidden fields
parent 5d632f06
......@@ -34,6 +34,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => $this->t('Bananas'),
);
$form['strawberry'] = [
'#type' => 'hidden',
'#value' => 'red',
'#attributes' => ['id' => 'redstrawberryhiddenfield'],
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
......
......@@ -74,6 +74,17 @@ public function testForm() {
$this->assertSession()->elementExists('css', 'form#form-test-form-test-object');
$this->assertSession()->fieldExists('bananas');
// Check that the hidden field exists and has a specific value.
$this->assertSession()->hiddenFieldExists('strawberry');
$this->assertSession()->hiddenFieldExists('red');
$this->assertSession()->hiddenFieldExists('redstrawberryhiddenfield');
$this->assertSession()->hiddenFieldValueNotEquals('strawberry', 'brown');
$this->assertSession()->hiddenFieldValueEquals('strawberry', 'red');
// Check that a hidden field does not exist.
$this->assertSession()->hiddenFieldNotExists('bananas');
$this->assertSession()->hiddenFieldNotExists('pineapple');
$edit = ['bananas' => 'green'];
$this->submitForm($edit, 'Save', 'form-test-form-test-object');
......
......@@ -5,6 +5,7 @@
use Behat\Mink\Driver\GoutteDriver;
use Behat\Mink\Element\Element;
use Behat\Mink\Mink;
use Behat\Mink\Selector\SelectorsHandler;
use Behat\Mink\Session;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Serialization\Json;
......@@ -363,7 +364,10 @@ protected function initMink() {
$driver->getClient()->setClient($client);
}
$session = new Session($driver);
$selectors_handler = new SelectorsHandler([
'hidden_field_selector' => new HiddenFieldSelector()
]);
$session = new Session($driver, $selectors_handler);
$this->mink = new Mink();
$this->mink->registerSession('default', $session);
$this->mink->setDefaultSessionName('default');
......
<?php
namespace Drupal\Tests;
use Behat\Mink\Selector\PartialNamedSelector;
/**
* Extends PartialNamedSelector to allow retrieval of hidden fields.
*
* @see \Behat\Mink\Selector\PartialNamedSelector
*/
class HiddenFieldSelector extends PartialNamedSelector {
/**
* {@inheritdoc}
*/
public function __construct() {
$xpath = ".//input[%lowercaseType% = 'hidden' and (%idOrNameMatch% or %valueMatch%)]";
$this->registerNamedXpath('hidden_field', $xpath);
parent::__construct();
}
}
......@@ -422,4 +422,83 @@ public function fieldDisabled($field, TraversableElement $container = NULL) {
return $node;
}
/**
* Checks that specific hidden field exists.
*
* @param string $field
* One of id|name|value for the hidden field.
* @param \Behat\Mink\Element\TraversableElement $container
* (optional) The document to check against. Defaults to the current page.
*
* @return \Behat\Mink\Element\NodeElement
* The matching element.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
*/
public function hiddenFieldExists($field, TraversableElement $container = NULL) {
$container = $container ?: $this->session->getPage();
if ($node = $container->find('hidden_field_selector', ['hidden_field', $field])) {
return $node;
}
throw new ElementNotFoundException($this->session->getDriver(), 'form hidden field', 'id|name|value', $field);
}
/**
* Checks that specific hidden field does not exists.
*
* @param string $field
* One of id|name|value for the hidden field.
* @param \Behat\Mink\Element\TraversableElement $container
* (optional) The document to check against. Defaults to the current page.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function hiddenFieldNotExists($field, TraversableElement $container = NULL) {
$container = $container ?: $this->session->getPage();
$node = $container->find('hidden_field_selector', ['hidden_field', $field]);
$this->assert($node === NULL, "A hidden field '$field' exists on this page, but it should not.");
}
/**
* Checks that specific hidden field have provided value.
*
* @param string $field
* One of id|name|value for the hidden field.
* @param string $value
* The hidden field value that needs to be checked.
* @param \Behat\Mink\Element\TraversableElement $container
* (optional) The document to check against. Defaults to the current page.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function hiddenFieldValueEquals($field, $value, TraversableElement $container = NULL) {
$node = $this->hiddenFieldExists($field, $container);
$actual = $node->getValue();
$regex = '/^' . preg_quote($value, '/') . '$/ui';
$message = "The hidden field '$field' value is '$actual', but '$value' expected.";
$this->assert((bool) preg_match($regex, $actual), $message);
}
/**
* Checks that specific hidden field doesn't have the provided value.
*
* @param string $field
* One of id|name|value for the hidden field.
* @param string $value
* The hidden field value that needs to be checked.
* @param \Behat\Mink\Element\TraversableElement $container
* (optional) The document to check against. Defaults to the current page.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function hiddenFieldValueNotEquals($field, $value, TraversableElement $container = NULL) {
$node = $this->hiddenFieldExists($field, $container);
$actual = $node->getValue();
$regex = '/^' . preg_quote($value, '/') . '$/ui';
$message = "The hidden field '$field' value is '$actual', but it should not be.";
$this->assert(!preg_match($regex, $actual), $message);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment