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
@ -127,8 +127,17 @@ class LoginContext extends BehatContext
|
||||
$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);
|
||||
@ -136,13 +145,19 @@ class LoginContext extends BehatContext
|
||||
$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;
|
||||
if($form->isVisible() && $form->find('css', '[name=Email]')) {
|
||||
$visibleForm = $form;
|
||||
}
|
||||
}
|
||||
|
||||
$emailField = $form->find('css', '[name=Email]');
|
||||
$passwordField = $form->find('css', '[name=Password]');
|
||||
$submitButton = $form->find('css', '[type=submit]');
|
||||
assertNotNull($visibleForm, 'Could not find 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');
|
||||
@ -151,12 +166,6 @@ class LoginContext extends BehatContext
|
||||
$emailField->setValue($email);
|
||||
$passwordField->setValue($password);
|
||||
$submitButton->press();
|
||||
|
||||
$foundForm = true;
|
||||
break;
|
||||
}
|
||||
|
||||
assertTrue($foundForm, 'Found login form');
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user