jhedstrom / drupalextension

An integration layer between Behat, Mink Extension, and Drupal.
GNU General Public License v2.0
209 stars 192 forks source link

When logged in as {role} failing due to race condition with Chromedriver #679

Closed danlinn closed 2 weeks ago

danlinn commented 2 weeks ago

I haven't been able to nail down the exact issue but when using Chromedriver the login seems to get past itself, showing a logged in page, but not being logged in on the subsequent step. I have reproduced the error on a Drupal 9.5 install using this composer only including the necessary behat stuff.

I have solved this by adding a sleep(1); right before the submit click. This solves it in all of our tests so far: Patch:

--- vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Manager/DrupalAuthenticationManager.php   2022-04-26 12:55:44
+++ vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Manager/DrupalAuthenticationManager.php       2024-08-26 12:05:37
@@ -68,7 +68,7 @@
         if (empty($submit)) {
             throw new \Exception(sprintf("No submit button at %s", $this->getSession()->getCurrentUrl()));
         }
-
+          sleep(1);
         // Log in.
         $submit->click();

Composer:

{
    "name": "drupal/recommended-project",
    "description": "Project template for Drupal projects with a relocated document root",
    "type": "project",
    "license": "GPL-2.0-or-later",
    "homepage": "https://www.drupal.org/project/drupal",
    "support": {
        "docs": "https://www.drupal.org/docs/user_guide/en/index.html",
        "chat": "https://www.drupal.org/node/314178"
    },
    "repositories": [
        {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        }
    ],
    "require": {
        "bex/behat-screenshot": "*",
        "composer/installers": "^2.0",
        "dmore/behat-chrome-extension": "^1.4",
        "dmore/chrome-mink-driver": "2.9.2",
        "drevops/behat-screenshot": "^1.2",
        "drupal/core-composer-scaffold": "^11.0",
        "drupal/core-project-message": "^11.0",
        "drupal/core-recommended": "^9.5",
        "drupal/drupal-extension": "4.2.1",
        "drush/drush": "*",
        "genesis/behat-fail-aid": "2.5.3"
    },
    "conflict": {
        "drupal/drupal": "*"
    },
    "minimum-stability": "stable",
    "prefer-stable": true,
    "config": {
        "allow-plugins": {
            "composer/installers": true,
            "drupal/core-composer-scaffold": true,
            "drupal/core-project-message": true,
            "phpstan/extension-installer": true,
            "dealerdirect/phpcodesniffer-composer-installer": true,
            "php-http/discovery": true
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "Drupal\\Tests\\Behat\\": "tests/behat/features/bootstrap"
        }
    },
    "extra": {
        "drupal-scaffold": {
            "locations": {
                "web-root": "web/"
            }
        },
        "installer-paths": {
            "web/core": [
                "type:drupal-core"
            ],
            "web/libraries/{$name}": [
                "type:drupal-library"
            ],
            "web/modules/contrib/{$name}": [
                "type:drupal-module"
            ],
            "web/profiles/contrib/{$name}": [
                "type:drupal-profile"
            ],
            "web/themes/contrib/{$name}": [
                "type:drupal-theme"
            ],
            "drush/Commands/contrib/{$name}": [
                "type:drupal-drush"
            ],
            "web/modules/custom/{$name}": [
                "type:drupal-custom-module"
            ],
            "web/profiles/custom/{$name}": [
                "type:drupal-custom-profile"
            ],
            "web/themes/custom/{$name}": [
                "type:drupal-custom-theme"
            ]
        },
        "drupal-core-project-message": {
            "include-keys": [
                "homepage",
                "support"
            ],
            "post-create-project-cmd-message": [
                "<bg=blue;fg=white>                                                         </>",
                "<bg=blue;fg=white>  Congratulations, you’ve installed the Drupal codebase  </>",
                "<bg=blue;fg=white>  from the drupal/recommended-project template!          </>",
                "<bg=blue;fg=white>                                                         </>",
                "",
                "<bg=yellow;fg=black>Next steps</>:",
                "  * Install the site: https://www.drupal.org/docs/installing-drupal",
                "  * Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html",
                "  * Get support: https://www.drupal.org/support",
                "  * Get involved with the Drupal community:",
                "      https://www.drupal.org/getting-involved",
                "  * Remove the plugin that prints this message:",
                "      composer remove drupal/core-project-message"
            ]
        }
    }
}

Test:

Feature: Check that content type basic page and the respective fields exist and are as expected.

  Scenario: All basic page fields exist and are present.
    Given I am logged in as a user with the 'administrator' role
    When I am at "/admin/structure/types/manage/page/fields"
    Then I should see "Body" in the "body" row

Behat.yml:

 default:
  calls:
    error_reporting: 7 # value of E_ERROR | E_WARNING | E_PARSE
  suites:
    default:
      filters:
        tags: "~@wip&&~@notest&&~@manual"
      paths:
        features: "%paths.base%/features"
        bootstrap: "%paths.base%/features/bootstrap"
      contexts:
        - Drupal\Tests\Behat\FeatureContext:
          arguments:
            - "@mink"
            - "@environment"
        - Drupal\Tests\Behat\BrowserResponseContext
        - Drupal\Tests\Behat\OpignoContext
        - Drupal\Tests\Behat\ElementContext
        - Drupal\Tests\Behat\EntityContext
        - Drupal\Tests\Behat\StripeWebhookContext
        - Drupal\Tests\Behat\GroupContentContext
        - Drupal\Tests\Behat\GroupContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext
        - DrevOps\BehatScreenshotExtension\Context\ScreenshotContext
        - FailAid\Context\FailureContext
    notest:
      filters:
        tags: "@notest"
      paths:
        notest: "%paths.base%/notest"
      contexts:
        - Drupal\Tests\Behat\FeatureContext
        - Drupal\Tests\Behat\BrowserResponseContext
        - Drupal\Tests\Behat\OpignoContext
        - Drupal\Tests\Behat\ElementContext
        - Drupal\Tests\Behat\EntityContext
        - Drupal\Tests\Behat\StripeWebhookContext
        - Drupal\Tests\Behat\GroupContentContext
        - Drupal\Tests\Behat\GroupContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext

  extensions:
    DMore\ChromeExtension\Behat\ServiceContainer\ChromeExtension: ~

    Drupal\MinkExtension:
      ajax_timeout: 50
      browser_name: chrome
      base_url: http://my-lando-app.lndo.site/
      sessions:
        default:
          chrome:
            socket_timeout: 60
            api_url: "http://host.docker.internal:9515"
            dom_wait_timeout: 5000

    Drupal\DrupalExtension:
      blackbox: ~
      api_driver: "drupal"
      drupal:
        drupal_root: "%paths.base%/../../web"
      region_map:
      selectors:
      subcontexts:
        paths:
          - "%paths.base%/../../web/modules"
      text:
      drush:
        alias: "@self"
        binary: "%paths.base%/../../vendor/bin/drush"
        root: "%paths.base%/../../web"
    FailAid\Extension:
      screenshot:
        directory: "/app/tests/behat/results"
        mode: default
        autoClean: false
        size: 1444x1280
    DrevOps\BehatScreenshotExtension:
      dir: "%paths.base%/results"
      fail: true
      fail_prefix: "failed_"
      purge: false
    Bex\Behat\ScreenshotExtension:
      image_drivers:
        local:
          screenshot_directory: "/app/tests/behat/results"
          clear_screenshot_directory: true # Enable removing all images before each test run. It is false by default.
  formatters:
    pretty:
      output_decorate: true
    junit:
      output_path: "%paths.base%/../reports/junit"

.lando:

name: my-lando-app
recipe: drupal9
config:
  webroot: web
  database: mariadb
services:
  database:
    type: mariadb:10.4
    portforward: true
    creds:
      user: drupal
      password: drupal
      database: drupal
tooling:
  drush:
    service: appserver
    cmd: drush
  behat:
    service: appserver
    description: Runs Behat tests locally
    cmd:
      - /app/vendor/bin/behat --config /app/tests/behat/behat.yml --suite default
danlinn commented 2 weeks ago

Hold off on this. I was able to get it working on the vanilla install. I'll try to see which of our modules is causing the conflict.