Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PageReadinessExtension.php
Go to the documentation of this file.
1 <?php
8 
9 use Codeception\Event\StepEvent;
10 use Codeception\Event\TestEvent;
11 use Codeception\Step;
12 use Facebook\WebDriver\Exception\UnexpectedAlertOpenException;
14 use Facebook\WebDriver\Exception\TimeOutException;
17 use Monolog\Logger;
18 
23 {
30  private $ignoredActions = [
31  'saveScreenshot',
32  'skipReadinessCheck',
33  'wait'
34  ];
35 
39  private $logger;
40 
46  private $verbose;
47 
53  private $readinessMetrics;
54 
60  private $testName;
61 
68  public function _initialize()
69  {
70  $this->logger = LoggingUtil::getInstance()->getLogger(get_class($this));
71  $this->verbose = MftfApplicationConfig::getConfig()->verboseEnabled();
72  parent::_initialize();
73  }
74 
82  public function beforeTest(TestEvent $e)
83  {
84  parent::beforeTest($e);
85  if (isset($this->config['resetFailureThreshold'])) {
86  $failThreshold = intval($this->config['resetFailureThreshold']);
87  } else {
88  $failThreshold = 3;
89  }
90 
91  $this->testName = $e->getTest()->getMetadata()->getName();
92 
93  $this->getDriver()->_setConfig(['skipReadiness' => false]);
94 
95  $metrics = [];
96  foreach ($this->config['readinessMetrics'] as $metricClass) {
97  $metrics[] = new $metricClass($this, $failThreshold);
98  }
99 
100  $this->readinessMetrics = $metrics;
101  }
102 
112  public function beforeStep(StepEvent $e)
113  {
114  $step = $e->getStep();
115  $manualSkip = $this->getDriver()->_getConfig()['skipReadiness'];
116  if ($this->shouldSkipCheck($step, $manualSkip)) {
117  return;
118  }
119 
120  // Check if page has changed and reset metric tracking if so
121  if ($this->pageChanged($step)) {
122  $this->logDebug(
123  'Page URI changed; resetting readiness metric failure tracking',
124  [
125  'step' => $step->__toString(),
126  'newUri' => $this->getUri()
127  ]
128  );
130  foreach ($this->readinessMetrics as $metric) {
131  $metric->resetTracker();
132  }
133  }
134 
135  // todo: Implement step parameter to override global timeout configuration
136  if (isset($this->config['timeout'])) {
137  $timeout = intval($this->config['timeout']);
138  } else {
139  $timeout = $this->getDriver()->_getConfig()['pageload_timeout'];
140  }
141 
142  $metrics = $this->readinessMetrics;
143 
144  try {
145  $this->getDriver()->webDriver->wait($timeout)->until(
146  function () use ($metrics) {
147  $passing = true;
148 
150  foreach ($metrics as $metric) {
151  try {
152  if (!$metric->runCheck()) {
153  $passing = false;
154  }
155  } catch (UnexpectedAlertOpenException $exception) {
156  }
157  }
158  return $passing;
159  }
160  );
161  } catch (TimeoutException $exception) {
162  }
163 
165  foreach ($metrics as $metric) {
166  $metric->finalizeForStep($step);
167  }
168  }
169 
175  public function getTestName()
176  {
177  return $this->testName;
178  }
179 
188  private function shouldSkipCheck($step, $manualSkip)
189  {
190  if ($step instanceof Step\Comment || in_array($step->getAction(), $this->ignoredActions) || $manualSkip) {
191  return true;
192  }
193  return false;
194  }
195 
204  private function logDebug($message, $context = [])
205  {
206  if ($this->verbose) {
207  $logContext = [
208  'test' => $this->testName,
209  'uri' => $this->getUri()
210  ];
211  foreach ($this->readinessMetrics as $metric) {
212  $logContext[$metric->getName()] = $metric->getStoredValue();
213  $logContext[$metric->getName() . '.failCount'] = $metric->getFailureCount();
214  }
215  $context = array_merge($logContext, $context);
216  //TODO REMOVE THIS LINE, UNCOMMENT LOGGER
217  //$this->logger->info($message, $context);
218  }
219  }
220 }
$message