Skip to content

Commit d6863a3

Browse files
committed
Wait for page load after login to fix race condition
1 parent 394dd58 commit d6863a3

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/Drupal/DrupalExtension/Manager/DrupalAuthenticationManager.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,30 @@ public function logIn(\stdClass $user)
8686
throw new \Exception(sprintf("No submit button at %s", $this->getSession()->getCurrentUrl()));
8787
}
8888

89+
// Store the current URL before clicking login.
90+
$loginUrl = $this->getSession()->getCurrentUrl();
91+
8992
// Log in.
9093
$submit->click();
9194

95+
// Wait for URL change after login (max 3 seconds).
96+
$timeout = microtime(true) + 3;
97+
while (microtime(true) < $timeout && $this->getSession()->getCurrentUrl() === $loginUrl) {
98+
usleep(100000);
99+
}
100+
101+
// Wait for page to be fully loaded before check if loggedIn.
102+
$session = $this->getSession();
103+
$timeout = microtime(true) + 3;
104+
while (microtime(true) < $timeout) {
105+
if ($session->getPage() && $session->getPage()->find('css', 'body')) {
106+
// Additional wait to ensure DOM is stable.
107+
usleep(300000);
108+
break;
109+
}
110+
usleep(100000);
111+
}
112+
92113
if (!$this->loggedIn()) {
93114
if (isset($user->role)) {
94115
throw new \Exception(sprintf("Unable to determine if logged in because 'log_out' link cannot be found for user '%s' with role '%s'", $user->name, $user->role));

0 commit comments

Comments
 (0)