Skip to content

Commit

Permalink
Merge pull request #193 from lucatume/fix-wpwebdriver-login-issue
Browse files Browse the repository at this point in the history
Introduce login re-attempts in WpWebDriver to avoid missing cookies issues, resolves #121
  • Loading branch information
lucatume authored Nov 7, 2018
2 parents 05138ac + 89fe75f commit fbe3638
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- compatibility with Codeception `2.5.0` updating the `WPDb` class
- added a clear disclaimer about db wiping in the `init wpbrowser` command
- an issue where the WpWebDriver module would not login correctly [#121]

### Changed
- lowered the PHP required version from 7.0 to 5.6
Expand Down
83 changes: 56 additions & 27 deletions src/Codeception/Module/WPWebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Codeception\Module;

use Codeception\Exception\ModuleException;

/**
* A Codeception module offering specific WordPress browsing methods.
*/
Expand Down Expand Up @@ -36,6 +38,8 @@ class WPWebDriver extends WebDriver
*/
protected $pluginsPath;

protected $loginAttempt = 0;

/**
* Initializes the module setting the properties values.
* @return void
Expand Down Expand Up @@ -71,33 +75,58 @@ public function loginAsAdmin($time = 10)
return $this->loginAs($this->config['adminUsername'], $this->config['adminPassword'], $time);
}

/**
* Goes to the login page, wait for the login form and logs in using the given credentials.
*
* @param string $username
* @param string $password
* @param int $time
*
* @return array An array of login credentials and auth cookies.
*/
public function loginAs($username, $password, $time = 10)
{
$this->amOnPage($this->loginUrl);

$this->waitForElement('#user_login', $time);
$this->waitForElement('#user_pass', $time);
$this->waitForElement('#wp-submit', $time);

$this->fillField('#user_login', $username);
$this->fillField('#user_pass', $password);
$this->click('#wp-submit');

return [
'username' => $username,
'password' => $password,
'authCookie' => $this->grabWordPressAuthCookie(),
'loginCookie' => $this->grabWordPressLoginCookie()
];
/**
* Goes to the login page, wait for the login form and logs in using the given credentials.
*
* Depending on the driven browser the login might be "too fast" and the server might have not
* replied with valid cookies yet; in that case the method will re-attempt the login to obtain
* the cookies.
*
* @param string $username
* @param string $password
* @param int $timeout
* @param int $maxAttempts
*
* @return array An array of login credentials and auth cookies.
*
* @throws \Codeception\Exception\ModuleException If all the attempts of obtaining the cookie fail.
*/
public function loginAs($username, $password, $timeout = 10, $maxAttempts = 5) {
if ($this->loginAttempt === $maxAttempts) {
throw new ModuleException(
__CLASS__, "Could not login as [{$username}, {$password}] after {$maxAttempts} attempts."
);
}

$this->debug("Trying to login, attempt {$this->loginAttempt}/{$maxAttempts}...");

$this->amOnPage($this->loginUrl);

$this->waitForElement('#user_login', $timeout);
$this->waitForElement('#user_pass', $timeout);
$this->waitForElement('#wp-submit', $timeout);

$this->fillField('#user_login', $username);
$this->fillField('#user_pass', $password);
$this->click('#wp-submit');

$authCookie = $this->grabWordPressAuthCookie();
$loginCookie = $this->grabWordPressLoginCookie();
$empty_cookies = empty($authCookie) && empty($loginCookie);

if ($empty_cookies) {
$this->loginAttempt++;
$this->wait(1);
return $this->loginAs($username, $password, $timeout, $maxAttempts);
}

$this->loginAttempt = 0;
return [
'username' => $username,
'password' => $password,
'authCookie' => $authCookie,
'loginCookie' => $loginCookie,
];
}

/**
Expand Down

0 comments on commit fbe3638

Please sign in to comment.