Support multiple login forms on a page

Thanks to Jeffrey Guo for contributing.
This commit is contained in:
Ingo Schommer 2014-04-07 13:03:40 +12:00
parent 3928c6ecfe
commit 6f0cc7f68e

View File

@ -118,32 +118,45 @@ class LoginContext extends BehatContext
$this->getSession()->visit($c->joinUrlParts($c->getBaseUrl(), 'Security/logout')); $this->getSession()->visit($c->joinUrlParts($c->getBaseUrl(), 'Security/logout'));
} }
/** /**
* @When /^I log in with "(?<username>[^"]*)" and "(?<password>[^"]*)"$/ * @When /^I log in with "(?<username>[^"]*)" and "(?<password>[^"]*)"$/
*/ */
public function stepILogInWith($email, $password) public function stepILogInWith($email, $password)
{ {
$c = $this->getMainContext();
$loginUrl = $c->joinUrlParts($c->getBaseUrl(), $c->getLoginUrl());
$this->getSession()->visit($loginUrl);
$page = $this->getSession()->getPage(); $page = $this->getSession()->getPage();
$forms = $page->findAll('css', 'form[action="Security/LoginForm"]');
assertNotNull($forms, 'Login form not found');
$form = $page->find('css', 'form[action="Security/LoginForm"]'); // If no login form, go to /security/login page
assertNotNull($form, 'Login form not found'); if(count($forms) <= 0 ) {
$c = $this->getMainContext();
$loginUrl = $c->joinUrlParts($c->getBaseUrl(), $c->getLoginUrl());
$this->getSession()->visit($loginUrl);
$page = $this->getSession()->getPage();
$forms = $page->findAll('css', 'form[action="Security/LoginForm"]');
}
$emailField = $form->find('css', '[name=Email]'); $foundForm = false;
$passwordField = $form->find('css', '[name=Password]'); foreach($forms as $form) {
$submitButton = $form->find('css', '[type=submit]'); if(!$form->isVisible()) continue;
assertNotNull($emailField, 'Email field on login form not found'); $emailField = $form->find('css', '[name=Email]');
assertNotNull($passwordField, 'Password field on login form not found'); $passwordField = $form->find('css', '[name=Password]');
assertNotNull($submitButton, 'Submit button on login form not found'); $submitButton = $form->find('css', '[type=submit]');
$emailField->setValue($email); assertNotNull($emailField, 'Email field on login form not found');
$passwordField->setValue($password); assertNotNull($passwordField, 'Password field on login form not found');
$submitButton->press(); assertNotNull($submitButton, 'Submit button on login form not found');
$emailField->setValue($email);
$passwordField->setValue($password);
$submitButton->press();
$foundForm = true;
break;
}
assertTrue($foundForm, 'Found login form');
} }
/** /**