mirror of
https://github.com/silverstripe/silverstripe-behat-extension
synced 2024-10-22 15:05:32 +00:00
More resistant login form detection
Had some issues with multiple login forms on the same page, e.g. for social login, or with duplicated hidden login forms in the header.
This commit is contained in:
parent
6f0cc7f68e
commit
6a7df2f702
@ -126,37 +126,46 @@ class LoginContext extends BehatContext
|
||||
$page = $this->getSession()->getPage();
|
||||
$forms = $page->findAll('css', 'form[action="Security/LoginForm"]');
|
||||
assertNotNull($forms, 'Login form not found');
|
||||
|
||||
|
||||
// Try to find visible forms on current page
|
||||
// Allow multiple login forms (e.g. social login) by filering for "Email" field
|
||||
$visibleForm = null;
|
||||
foreach($forms as $form) {
|
||||
if($form->isVisible() && $form->find('css', '[name=Email]')) {
|
||||
$visibleForm = $form;
|
||||
}
|
||||
}
|
||||
|
||||
// If no login form, go to /security/login page
|
||||
if(count($forms) <= 0 ) {
|
||||
if(!$visibleForm) {
|
||||
$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"]');
|
||||
}
|
||||
|
||||
$foundForm = false;
|
||||
|
||||
// Try to find visible forms again on login page.
|
||||
$visibleForm = null;
|
||||
foreach($forms as $form) {
|
||||
if(!$form->isVisible()) continue;
|
||||
|
||||
$emailField = $form->find('css', '[name=Email]');
|
||||
$passwordField = $form->find('css', '[name=Password]');
|
||||
$submitButton = $form->find('css', '[type=submit]');
|
||||
|
||||
assertNotNull($emailField, 'Email field on login form not found');
|
||||
assertNotNull($passwordField, 'Password field on login form not found');
|
||||
assertNotNull($submitButton, 'Submit button on login form not found');
|
||||
|
||||
$emailField->setValue($email);
|
||||
$passwordField->setValue($password);
|
||||
$submitButton->press();
|
||||
|
||||
$foundForm = true;
|
||||
break;
|
||||
if($form->isVisible() && $form->find('css', '[name=Email]')) {
|
||||
$visibleForm = $form;
|
||||
}
|
||||
}
|
||||
|
||||
assertNotNull($visibleForm, 'Could not find login form');
|
||||
|
||||
assertTrue($foundForm, 'Found login form');
|
||||
$emailField = $visibleForm->find('css', '[name=Email]');
|
||||
$passwordField = $visibleForm->find('css', '[name=Password]');
|
||||
$submitButton = $visibleForm->find('css', '[type=submit]');
|
||||
|
||||
assertNotNull($emailField, 'Email field on login form not found');
|
||||
assertNotNull($passwordField, 'Password field on login form not found');
|
||||
assertNotNull($submitButton, 'Submit button on login form not found');
|
||||
|
||||
$emailField->setValue($email);
|
||||
$passwordField->setValue($password);
|
||||
$submitButton->press();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user