staminna
staminna

Reputation: 468

Login authentication with Behat and Mink

When I am on scenario step of authenticating, Mink is unable find Fields to fill.

I am using behat 3.5 and the PHP 5.6.38. The whole composer stack is up to date including Mink.

behat.yml file:

default:
  suites:
    default:
      contexts:
        - FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext
  extensions:
    Behat\MinkExtension:
      goutte: ~
      selenium2: ~
      base_url: 'localhost'
    Drupal\DrupalExtension:
      blackbox: ~
      api_driver: 'drush'
      drush:
        alias: 'local'
        root: 'C:\xampp2\htdocs\project'
      drupal:
        alias: 'local'
        drupal_root: 'C:\xampp2\htdocs\project'
local:
  extensions:
    Behat\MinkExtension:
    base_url: 'localhost'
    Drupal\DrupalExtension:
      api_driver: 'drush'
      drush:
        alias: '@self'
        root: 'C:\xampp2\htdocs\project'
      drupal:
        drupal_root: 'c:\xampp2\htdocs\project'

my.feature file:

Feature: Login 
  In order to login
  Authenticate as administrator
  Submit form sucessfully.

  @api
    Scenario: Login and join community.
    Given I am on "/user"
    # Given I am logged in as a user with the "administrator" role
    When I fill in the following:
      | edit-name | bddtest |
      | edit-pass | Behattest101 |

featureContext.php:

<?php

use Drupal\DrupalExtension\Context\RawDrupalContext;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Behat\Tester\Exception\PendingException;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends RawDrupalContext implements SnippetAcceptingContext {

  /**
   * Initializes context.
   *
   * Every scenario gets its own context instance.
   * You can also pass arbitrary arguments to the
   * context constructor through behat.yml.
   */
  public function __construct() {
  }

}

when I run > behat ... When I fill in the following: # Drupal\DrupalExtension\Context\MinkContext::fillFields()

  | edit-name | myusername |
  | edit-pass | mypassword |
  Form field with id|name|label|value|placeholder "edit-name" not found. (Behat\Mink\Exception\ElementNotFoundException)
1 scenario (1 failed)
5 steps (1 passed, 1 failed, 3 skipped)
I also tried "Username" and "Password" but with the same result.

Upvotes: 0

Views: 1200

Answers (1)

Malte K&#246;lle
Malte K&#246;lle

Reputation: 192

I also had the Problem that Behat didn't find the Field. I just used Javascript as a workaround. (Behat/Mink doesn't find field by id)

my.feature file:

  @javascript
  Scenario Outline: Login with JS
    When I fill in "<field>" with "<value>" with javascript
    Examples:
      |field    |value        |
      |edit-name| myusername  |
      |edit-pass| mypassword  |

featureContext.php

  /**
   * @When I fill in :field with :value with javascript
   */
  public function loginWithJavascript($field, $value){
      $javascript = "window.onload = function () {var e = document.getElementById('$field').value='$value';}";
      $this->getSession()->executeScript($javascript);
  }

Although it is not the cleanest solution, it works for me. Hopefully it works for you as well. I'm still trying to find a better solution.

Upvotes: 0

Related Questions