|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DrevOps\BehatSteps\Drupal; |
| 6 | + |
| 7 | +use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
| 8 | +use Behat\Hook\BeforeScenario; |
| 9 | +use Behat\Hook\BeforeStep; |
| 10 | +use Behat\Mink\Exception\DriverException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Wait for Drupal BigPipe placeholders to be replaced on JavaScript scenarios. |
| 14 | + * |
| 15 | + * Drupal BigPipe streams parts of a page in after the initial response and |
| 16 | + * replaces its `<span data-big-pipe-placeholder-id="...">` markers with the |
| 17 | + * real markup using JavaScript. Assertions that run before those replacements |
| 18 | + * land intermittently fail with "element not found". When this trait is |
| 19 | + * included, every `@javascript` scenario waits - before each step - until no |
| 20 | + * BigPipe placeholder markers remain in the DOM, removing that race without an |
| 21 | + * explicit step. |
| 22 | + * |
| 23 | + * The wait is best-effort: on timeout the step still runs, so a genuinely stuck |
| 24 | + * placeholder surfaces as the real assertion failure rather than being masked |
| 25 | + * here. Non-JavaScript scenarios are left untouched, where BigPipe renders |
| 26 | + * server-side (see the drupal-extension `@bigpipe` cookie handling). |
| 27 | + * |
| 28 | + * Skip processing with tag: `@behat-steps-skip:BigPipeTrait`. |
| 29 | + * |
| 30 | + * Override `bigPipeGetWaitTimeout()` (or set `$bigPipeWaitTimeout`) in your |
| 31 | + * `FeatureContext` to change the maximum wait. |
| 32 | + */ |
| 33 | +trait BigPipeTrait { |
| 34 | + |
| 35 | + /** |
| 36 | + * Default maximum time to wait for BigPipe placeholders, in milliseconds. |
| 37 | + */ |
| 38 | + protected const DEFAULT_WAIT_TIMEOUT = 10000; |
| 39 | + |
| 40 | + /** |
| 41 | + * Whether the automatic BigPipe wait is active for the current scenario. |
| 42 | + */ |
| 43 | + protected bool $bigPipeAutoWaitEnabled = FALSE; |
| 44 | + |
| 45 | + /** |
| 46 | + * Maximum time to wait for BigPipe placeholders to be replaced, in milliseconds. |
| 47 | + */ |
| 48 | + protected int $bigPipeWaitTimeout = self::DEFAULT_WAIT_TIMEOUT; |
| 49 | + |
| 50 | + /** |
| 51 | + * Resolve whether the automatic BigPipe wait applies to this scenario. |
| 52 | + */ |
| 53 | + #[BeforeScenario] |
| 54 | + public function bigPipeBeforeScenario(BeforeScenarioScope $scope): void { |
| 55 | + $scenario = $scope->getScenario(); |
| 56 | + $feature = $scope->getFeature(); |
| 57 | + |
| 58 | + // Resolved here, not in the BeforeStep hook, because a BeforeStep scope |
| 59 | + // cannot read scenario-level tags. |
| 60 | + $is_javascript = $feature->hasTag('javascript') || $scenario->hasTag('javascript'); |
| 61 | + $is_skipped = $feature->hasTag('behat-steps-skip:BigPipeTrait') || $scenario->hasTag('behat-steps-skip:BigPipeTrait'); |
| 62 | + |
| 63 | + $this->bigPipeAutoWaitEnabled = $is_javascript && !$is_skipped; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Wait for BigPipe placeholders to settle before each step runs. |
| 68 | + * |
| 69 | + * Named to avoid overriding the drupal-extension BigPipeTrait's own |
| 70 | + * `bigPipeBeforeStep()` hook, inherited through `DrupalContext`. |
| 71 | + */ |
| 72 | + #[BeforeStep] |
| 73 | + public function bigPipeWaitBeforeStep(): void { |
| 74 | + if (!$this->bigPipeAutoWaitEnabled) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + $this->bigPipeWaitForPlaceholders($this->bigPipeGetWaitTimeout()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Wait until no BigPipe placeholder markers remain in the DOM. |
| 83 | + * |
| 84 | + * @param int $timeout_ms |
| 85 | + * Maximum time to wait, in milliseconds. |
| 86 | + */ |
| 87 | + protected function bigPipeWaitForPlaceholders(int $timeout_ms): void { |
| 88 | + try { |
| 89 | + $this->getSession()->wait($timeout_ms, "document.querySelectorAll('[data-big-pipe-placeholder-id]').length === 0"); |
| 90 | + } |
| 91 | + // @codeCoverageIgnoreStart |
| 92 | + catch (DriverException) { |
| 93 | + // The driver session is not ready (e.g. no page has been visited yet), |
| 94 | + // so there is nothing to synchronise. |
| 95 | + } |
| 96 | + // @codeCoverageIgnoreEnd |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Maximum time to wait for BigPipe placeholders, in milliseconds. |
| 101 | + * |
| 102 | + * @return int |
| 103 | + * The timeout in milliseconds. |
| 104 | + */ |
| 105 | + protected function bigPipeGetWaitTimeout(): int { |
| 106 | + return $this->bigPipeWaitTimeout; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments