drupalextension icon indicating copy to clipboard operation
drupalextension copied to clipboard

Logging in after already having been logged in doesn't work (at least with Chrome Driver)

Open bkosborne opened this issue 5 years ago • 2 comments

I'm working on an upgrade from version 3.4.1 to 4.1.0. I had a feature like this:

  Scenario: SVG images can be uploaded to the media library
    Given I am logged in as a "site_admin"
    And I am on "/media/add/svg"
    Then I should see "Access denied"
    Given I am logged in as a "admin"
    And I am on "/media/add/svg"
    And I fill in "Sample SVG" for "Name"

This doesn't work right anymore. After Given I am logged in as a "admin", the user gets logged out, but doesn't get logged in again. I have a plugin that captures screenshots and HTML output and indeed, the admin toolbar isn't there for the user account.

I can fix it by doing an explicit logout step:

  Scenario: SVG images can be uploaded to the media library
    Given I am logged in as a "site_admin"
    And I am on "/media/add/svg"
    Then I should see "Access denied"
    Then I log out
    Given I am logged in as a "admin"
    And I am on "/media/add/svg"
    And I fill in "Sample SVG" for "Name"

I am using ChromeDriver

bkosborne avatar Oct 26 '20 19:10 bkosborne

You might be interested in the steps introduced in https://github.com/jhedstrom/drupalextension/commit/f0ecbb4645a0a0abb9128aa66d27823e7516c7ec (which I think aren't available unless you replicate them to your own FeatureContext; they are in features/bootstrap/FeatureContext.php).

AFAIK Given I am logged in as :role does not perform an explicit logout step, though it may have in the past. You could make this implicit with a @BeforeStep annotation (Drupal\DrupalExtension\Context\MinkContext::afterJavascriptStep() shows a similar approach to what you might do), but in your situation I'd prefer the explicit Then I log out for clarity.

xurizaemon avatar Jun 28 '21 00:06 xurizaemon

Logging in and out has been completely overhauled in 4.x. I am not sure what might be causing the problem, since we are still attempting a logout before logging in a new user. But the way we log out has been changed. Beforehand we were manually logging out, but now we are simply deleting all session cookies. Without any cookies the user is considered to be anonymous.

This approach was chosen since it is much faster than manually going through the logout process. The change was introduced in #427 and was proposed / discussed in #62.

If you have customized the logout functionality in some way and need to revert to manually logging out, the good news is that in 4.x the user management and authentication logic has been moved into swappable services, so you can write your own custom code to do whatever is required for your site. For an example alternative login system see https://github.com/drupaltest/behat-one-time-login

pfrenssen avatar Jun 29 '21 08:06 pfrenssen