Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LoginUserOnBackendStep.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Backend\Test\Page\Adminhtml\Dashboard;
11 use Magento\Mtf\Client\BrowserInterface;
12 use Magento\Mtf\TestStep\TestStepInterface;
14 
18 class LoginUserOnBackendStep implements TestStepInterface
19 {
26 
32  protected $adminAuth;
33 
39  protected $user;
40 
46  protected $dashboard;
47 
53  private $browser;
54 
60  private $errorMessages = [
61  'Invalid Form Key. Please refresh the page.',
62  'Your current session has been expired.',
63  ];
64 
73  public function __construct(
76  User $user,
77  Dashboard $dashboard,
78  BrowserInterface $browser
79  ) {
80  $this->logoutUserOnBackendStep = $logoutUserOnBackendStep;
81  $this->adminAuth = $adminAuth;
82  $this->user = $user;
83  $this->dashboard = $dashboard;
84  $this->browser = $browser;
85  }
86 
92  public function run()
93  {
94  $this->adminAuth->open();
95 
96  if (!$this->adminAuth->getLoginBlock()->isVisible()) {
97  $this->logoutUserOnBackendStep->run();
98  }
99 
100  try {
101  $this->login();
102  } catch (\PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
103  if (strpos($e->getMessage(), 'Timed out after') !== false) {
104  $messages = $this->adminAuth->getMessagesBlock();
105  if (in_array($messages->getErrorMessage(), $this->errorMessages, true)) {
106  $this->browser->refresh();
107  $this->login();
108  }
109  }
110  }
111 
112  $this->dashboard->getSystemMessageDialog()->closePopup();
113  }
114 
120  private function login()
121  {
122  $this->adminAuth->getLoginBlock()->fill($this->user);
123  $this->adminAuth->getLoginBlock()->submit();
124  $this->adminAuth->getLoginBlock()->waitFormNotVisible();
125  }
126 }
__construct(LogoutUserOnBackendStep $logoutUserOnBackendStep, AdminAuthLogin $adminAuth, User $user, Dashboard $dashboard, BrowserInterface $browser)