‪TYPO3CMS  11.5
PasswordLogin.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Codeception\Module;
21 use Codeception\Module\WebDriver;
22 use Codeception\Util\Locator;
23 use Facebook\WebDriver\Exception\TimeoutException;
24 use Facebook\WebDriver\WebDriverKeys;
25 
29 final class ‪PasswordLogin extends Module
30 {
34  protected ‪$config = [
35  'passwords' => [],
36  ];
37 
48  public function ‪useExistingSession(string $role, float $waitTime = 0.5): void
49  {
50  $webDriver = $this->‪getWebDriver();
51 
52  $hasSession = $this->‪loadSession($role);
53 
54  $webDriver->amOnPage('/typo3');
55  $webDriver->wait($waitTime);
56 
57  if (!$hasSession) {
58  $this->‪login($role);
59  }
60 
61  // Ensure main content frame is fully loaded, otherwise there are load-race-conditions ..
62  $webDriver->debugSection('IFRAME', 'Switch to list_frame');
63  try {
64  $webDriver->waitForElement('iframe[name="list_frame"]');
65  } catch (TimeoutException $e) {
66  // We don't know why, but it seems loading an existing session fails sometimes.
67  // In this case, the BE is not loaded and the system "hangs" in login.
68  // Catch this here and login again.
69  $webDriver->debugSection('Session lost', 'Log in again');
70  $this->‪login($role);
71  $webDriver->debugSection('IFRAME', 'Switch to list_frame');
72  $webDriver->waitForElement('iframe[name="list_frame"]');
73  }
74  $webDriver->switchToIFrame('list_frame');
75  $webDriver->waitForElement(Locator::firstElement('div.module'));
76  $webDriver->wait($waitTime);
77  // .. and switch back to main frame.
78  $webDriver->debugSection('IFRAME', 'Switch to main frame');
79  $webDriver->switchToIFrame();
80 
81  $webDriver->debug(sprintf('useExistingSession("%s", %s) finished.', $role, $waitTime));
82  }
83 
84  private function ‪loadSession(string $role): bool
85  {
86  $webDriver = $this->‪getWebDriver();
87  $webDriver->webDriver->manage()->deleteCookieNamed('be_typo_user');
88  $webDriver->webDriver->manage()->deleteCookieNamed('be_lastLoginProvider');
89  return $webDriver->loadSessionSnapshot('login.' . $role);
90  }
91 
92  private function ‪login(string $role): void
93  {
94  $webDriver = $this->‪getWebDriver();
95  $webDriver->waitForElement('body[data-typo3-login-ready]');
96  $password = $this->_getConfig('passwords')[$role];
97  $webDriver->fillField('#t3-username', $role);
98  $webDriver->fillField('#t3-password', $password);
99  $webDriver->pressKey('#t3-password', WebDriverKeys::ENTER);
100  $webDriver->waitForElement('.t3js-scaffold-toolbar');
101  $webDriver->saveSessionSnapshot('login.' . $role);
102  }
103 
104  private function ‪getWebDriver(): WebDriver
105  {
106  return $this->getModule('WebDriver');
107  }
108 }
‪TYPO3\CMS\Core\Tests\Acceptance\Helper\PasswordLogin\login
‪login(string $role)
Definition: PasswordLogin.php:91
‪TYPO3\CMS\Core\Tests\Acceptance\Helper\PasswordLogin\useExistingSession
‪useExistingSession(string $role, float $waitTime=0.5)
Definition: PasswordLogin.php:47
‪TYPO3\CMS\Core\Tests\Acceptance\Helper\PasswordLogin\getWebDriver
‪getWebDriver()
Definition: PasswordLogin.php:103
‪TYPO3\CMS\Core\Tests\Acceptance\Helper\PasswordLogin
Definition: PasswordLogin.php:30
‪TYPO3\CMS\Core\Tests\Acceptance\Helper\PasswordLogin\$config
‪array $config
Definition: PasswordLogin.php:33
‪TYPO3\CMS\Core\Tests\Acceptance\Helper
Definition: PasswordLogin.php:18
‪TYPO3\CMS\Core\Tests\Acceptance\Helper\PasswordLogin\loadSession
‪loadSession(string $role)
Definition: PasswordLogin.php:83