I can't seem to login as an admin on behat tests. Could anyone help point me in the right direction?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
rgchi's picture

I'm trying to log create a scenario with a by logging in as a user in the administration role. I'm using the following as my first run to get my feet wet

$ cat first.feature
Feature: First
  Playing with features and exploring


@api
Scenario: Run cron
    Given I am logged in as a user with the "authenticated" role
    When I run cron
    And am on "admin/reports/dblog"
    Then I should see the link "Cron run completed"

However, on this I can't seem to get past the first step. When running this feature, I see the following error:

Failed to log in as user 'b8Yu3WCn' with role 'authenticated' (Exception)

And I'm at a loss as to how to debug this.

I'm running
- behat 3.0-dev via the Behat Extension,
- LDAP

Comments

I once had the problems with

rodrigoaguilera's picture

I once had the problems with the definition of authenticated begat has.

Have in mind any authenticated role must have a logout button to become anonymous again.

Or you can write your own step about what means to be authenticated.

I once had the problems with

rodrigoaguilera's picture

I once had the problems with the definition of authenticated begat has.

Have in mind any authenticated role must have a logout button to become anonymous again.

Or you can write your own step about what means to be authenticated.

Starting from the

franksj's picture

Starting from the easiest...

Double-check that your role is actually called "authenticated." If you haven't changed any roles, then it will be "authenticated user" rather than "authenticated."

Double-check that your behat.yml properly targets your URL and your Drupal root directory.

Where does your site redirect you after you successfully log in? Front? Make sure that page has a logout link, or the standard loggedIn() method will fail. When I have sites that don't have logout links, I use the 'logged-in' class on the body element. I have a custom Drupal context like this:

<?php

use Behat\Gherkin\Node\TableNode;
use Behat\Gherkin\Parser;
use Behat\MinkExtension\Context\RawMinkContext,
  Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

/
* Defines application features from the specific context.
*/
class CustomDrupalContext extends \Drupal\DrupalExtension\Context\DrupalContext {
  /

   * 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() {
  }

  public function loggedIn() {
    $session = $this->getSession();
    $session->visit($this->locatePath('/'));

    // Rather than checking If a logout link is found, let's check
    // for the .logged-in class on body.
    $element = $session->getPage();
    return $element->find('css', '.logged-in');
  }

}

I had this issue as well. It

fullerja's picture

I had this issue as well. It turned out to be a problem with LDAP. Switching to Mixed Mode allow the DrupalExtension to function properly. Since we don't want mixed by default, I added a pre-test setup function

public static function prepare($scope)
     {
        $data = array(
          'sids' => array (
            'directory' => 'directory',
          ),
          'authenticationMode' => 1,
          'loginConflictResolve' => 2,
          'acctCreation' => 4,
          'loginUIUsernameTxt' => NULL,
          'loginUIPasswordTxt' => NULL,
          'ldapUserHelpLinkUrl' => NULL,
          'ldapUserHelpLinkText' => 'Logon Help',
          'emailOption' => 3,
          'emailUpdate' => 1,
          'allowOnlyIfTextInDn' => array (),
          'excludeIfTextInDn' => array (),
          'allowTestPhp' => '',
          'excludeIfNoAuthorizations' => NULL,
          'ssoRemoteUserStripDomainName' => NULL,
          'seamlessLogin' => NULL,
          'ldapImplementation' => NULL,
          'cookieExpire' => NULL,

        );
        variable_set('ldap_authentication_conf', $data);
     }

tried this example feature.

chriscalip's picture

Digging in more to the situation

An authenticated user usually does not have access to "admin/reports/dblog"
or for all of "admin/*"