Patrick Kenny
Patrick Kenny

Reputation: 6497

Behat: how to check if an email address input field contains a domain?

I'm trying to use Behat to test if an email address input field contains a certain domain.

Here's the HTML:

<input autocomplete="off" data-drupal-selector="edit-mail" aria-describedby="edit-mail--description" type="email" id="edit-mail" name="mail" value="[email protected]" size="60" maxlength="254" class="form-email required form-element form-element--type-email form-element--api-email" required="required" aria-required="true">

First, I tried this:

And the "input#edit-mail" element should contain "example.com"

However, this fails with:

  The string "example.com" was not found in the HTML of the element matching css "input#edit-mail". (Behat\Mink\Exception\ElementHtmlException)

So then I tried to write my own checker in FeatureContext.php based on this issue:

  /**
   * @Then the :element element should have the value :value
   */
  public function iShouldSeeValueElement($element, $value) {
    $page = $this->getSession()->getPage();
    // Alternately, substitute with getText() for the label.
    $element_value = $page->find('css', "$element")->getValue();
    if ($element_value != $value) {
      throw new exception('Value "'.$value.'" not found in element '.$element.'.');
    }
  }

However, this code only finds the value if it is an exact match, so it will not match just the domain.

How can I check for just the domain (partial match) in an input field?

Upvotes: 1

Views: 902

Answers (1)

Patrick Kenny
Patrick Kenny

Reputation: 6497

Whoops, this problem was really just a fail to read the PHP. Here's working code:

  /**
   * @Then the :element element should have the value :value
   *
   * https://github.com/minkphp/Mink/issues/215
   */
  public function iShouldSeeValueElement($element, $value) {
    $page = $this->getSession()->getPage();
    // Alternately, substitute with getText() for the label.
    $element_value = $page->find('css', "$element")->getValue();
    if (strpos("$element_value", "$value") === false) {
      throw new exception('Value '.$value.' not found in element '.$element.', which had a value of '.$element_value.'.');
    }
  }

Upvotes: 1

Related Questions