logged in as user with role does not seem to work

Created on 30 January 2018, over 6 years ago
Updated 27 July 2023, about 1 year ago

When using the "drupal" api_driver, Scenarios with the given 'I am logged in as a user with the "[whatever]" role' do not execute the remaining steps of the scenario as a privileged user.

For example, this simple feature:

Feature: Does this work
  In order to demonstrate that behat functions
  as a user
  I need a feature

  @api
  Scenario: Access Content screen
    Given I am logged in as a user with the "administrator" role
    When I am at "admin/content"
    Then I should not see the heading "Access denied"

Results in:

Feature: Does this work
  In order to demonstrate that behat functions
  as a user
  I need a feature

  @api
  Scenario: Access Content screen                                # features/simple.feature:7
    Given I am logged in as a user with the "administrator" role # Drupal\DrupalExtension\Context\DrupalContext::assertAuthenticatedByRole()
    When I am at "admin/content"                                 # Drupal\DrupalExtension\Context\MinkContext::assertAtPath()
      Current response status code is 403, but 200 expected. (Behat\Mink\Exception\ExpectationException)
    Then I should not see the heading "Access denied"            # Drupal\DrupalExtension\Context\MinkContext::assertNotHeading()

--- Failed scenarios:

    features/simple.feature:7

The 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: http://sandbox.local
    Drupal\DrupalExtension:
      blackbox: ~
      api_driver: 'drupal'
      drupal:
        drupal_root: '/var/www/sandbox/drupal'
      text:
        log_out: "Log out"
        log_in: "Log in"
        password_field: "Password"
        username_field: "Username"
      selectors:
        logged_in_selector: "body.logged-in"
        login_form_selector: "form#user-login"

I have tried it with and without the text and selectors areas. I can easily reproduce a previously-closed bug about not matching the logout string, but this is not the issue. The issue seems to be that the steps are executed as an unprivileged user despite the Given statement being an administrator; going to admin pages results in a 403.

CAVEAT: Hopefully this is something I'm doing wrong, since I'm new to Behat, but I've tried it on multiple machines against multiple sites, including a brand new installation of behat following the instructions here against a brand-new installation of Drupal 7 with no content and only the default user 1. Since I have never gotten this to work previously, I can't tell if it is a documentation problem or a code problem, or just a support request (though I have done my best to eliminate that last possibility).

πŸ› Bug report
Status

Closed: cannot reproduce

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ldpm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024