9 use Codeception\Module\WebDriver;
10 use Codeception\Test\Descriptor;
11 use Codeception\TestInterface;
12 use Facebook\WebDriver\Interactions\WebDriverActions;
13 use Codeception\Exception\ModuleConfigException;
14 use Codeception\Exception\ModuleException;
15 use Codeception\Util\Uri;
21 use Yandex\Allure\Adapter\Support\AttachmentSupport;
46 use AttachmentSupport;
53 '//div[contains(@class, "loading-mask")]',
54 '//div[contains(@class, "admin_data-grid-loading-mask")]',
55 '//div[contains(@class, "admin__data-grid-loading-mask")]',
56 '//div[contains(@class, "admin__form-loading-mask")]',
57 '//div[@data-role="spinner"]' 92 private $current_test;
113 private $jsErrors = [];
122 parent::_initialize();
133 parent::_resetConfig();
145 parent::_after($test);
154 public function _after(TestInterface $test)
168 if (!isset($this->config[
'url'])) {
169 throw new ModuleConfigException(
171 "Module connection failure. The URL for client can't bre retrieved" 174 return $this->config[
'url'];
186 $url = $this->webDriver->getCurrentURL();
187 if (
$url ==
'about:blank') {
188 throw new ModuleException($this,
'Current url is blank, no page was opened');
190 return Uri::retrieveUri(
$url);
201 $this->assertNotEquals(
$url, $this->webDriver->getCurrentURL());
212 $this->assertNotRegExp($regex, $this->webDriver->getCurrentURL());
223 $this->assertNotContains($needle, $this->webDriver->getCurrentURL());
234 $fullUrl = $this->webDriver->getCurrentURL();
239 $res = preg_match($regex, $fullUrl, $matches);
241 $this->fail(
"Couldn't match $regex in " . $fullUrl);
243 if (!isset($matches[1])) {
244 $this->fail(
"Nothing to grab. A regex parameter with a capture group is required. Ex: '/(foo)(bar)/'");
257 $this->assertEquals(
$url, $this->webDriver->getCurrentURL());
268 $this->assertRegExp($regex, $this->webDriver->getCurrentURL());
279 $this->assertContains($needle, $this->webDriver->getCurrentURL());
291 $this->executeJS(
"jQuery('.modal-popup').remove(); jQuery('.modals-overlay').remove();");
292 }
catch (\Exception $e) {
308 $selectDropdown =
$select .
' .action-select.admin__action-multiselect';
310 .
' .admin__action-multiselect-search-wrap>input[data-role="advanced-select-text"]';
311 $selectSearchResult =
$select .
' .admin__action-multiselect-label>span';
314 $this->waitForElementVisible($selectDropdown);
315 $this->click($selectDropdown);
319 if ($requireAction) {
320 $selectAction =
$select .
' button[class=action-default]';
322 $this->click($selectAction);
339 $this->fillField($selectSearchTextField,
'');
341 $this->fillField($selectSearchTextField,
$option);
343 $this->click($selectSearchResult);
355 $timeout = $timeout ?? $this->_getConfig()[
'pageload_timeout'];
358 $this->waitForJS(
'return !!window.jQuery && window.jQuery.active == 0;', $timeout);
359 }
catch (\Exception $exceptione) {
360 $this->debug(
"js never executed, performing {$timeout} second wait.");
361 $this->wait($timeout);
375 $timeout = $timeout ?? $this->_getConfig()[
'pageload_timeout'];
377 $this->waitForJS(
'return document.readyState == "complete"', $timeout);
391 foreach (self::$loadingMasksLocators as $maskLocator) {
394 $loadingMaskElements = $this->_findElements($maskLocator);
395 for (
$i = 1;
$i <= count($loadingMaskElements);
$i++) {
398 $this->waitForElementNotVisible(
"({$maskLocator})[{$i}]", $timeout);
408 public function formatMoney(
float $money, $locale =
'en_US.UTF-8')
411 $money = money_format(
'%.2n', $money);
413 $prefix = substr($money, 0, 1);
426 $floatString = str_replace(
',',
'', $floatString);
427 return floatval($floatString);
437 if (self::$localeAll[
$category] == $locale) {
440 foreach (self::$localeAll as $c =>
$l) {
441 self::$localeAll[$c] = setlocale($c, 0);
452 foreach (self::$localeAll as $c =>
$l) {
455 self::$localeAll[$c] =
null;
466 $this->executeJS(
'window.scrollTo(0,0);');
480 str_replace(
'index.php',
'', rtrim($this->config[
'url'],
'/')),
483 $apiURL = $baseUrl .
'/' . ltrim(getenv(
'MAGENTO_CLI_COMMAND_PATH'),
'/');
489 getenv(
'MAGENTO_CLI_COMMAND_PARAMETER') => $command,
526 $el = $this->_findElements($dependentSelector);
527 if (
sizeof($el) > 1) {
528 throw new \Exception(
"more than one element matches selector " . $dependentSelector);
531 $clickCondition =
null;
533 $clickCondition = !empty($el) && $el[0]->isDisplayed();
535 $clickCondition = empty($el) || !$el[0]->isDisplayed();
538 if ($clickCondition) {
539 $this->click($selector);
551 $this->fillField($selector,
"");
582 $this->current_test = $test;
583 $this->htmlReport =
null;
584 $this->pngReport =
null;
586 parent::_before($test);
599 if ($xOffset !==
null || $yOffset !==
null) {
600 $snodes = $this->matchFirstOrFail($this->baseElement,
$source);
601 $tnodes = $this->matchFirstOrFail($this->baseElement,
$target);
603 $targetX = intval($tnodes->getLocation()->getX() + $xOffset);
604 $targetY = intval($tnodes->getLocation()->getY() + $yOffset);
606 $travelX = intval($targetX - $snodes->getLocation()->getX());
607 $travelY = intval($targetY - $snodes->getLocation()->getY());
609 $action =
new WebDriverActions($this->webDriver);
610 $action->moveToElement($snodes)->perform();
611 $action->clickAndHold($snodes)->perform();
612 $action->moveByOffset($travelX, $travelY)->perform();
613 $action->release()->perform();
633 $this->fillField($field, $decryptedValue);
644 public function _failed(TestInterface $test, $fail)
646 $this->debugWebDriverLogs($test);
648 if ($this->pngReport ===
null && $this->htmlReport ===
null) {
652 if ($this->current_test ==
null) {
653 throw new \RuntimeException(
"Suite condition failure: \n" . $fail->getMessage());
656 $this->addAttachment($this->pngReport, $test->getMetadata()->getName() .
'.png',
'image/png');
657 $this->addAttachment($this->htmlReport, $test->getMetadata()->getName() .
'.html',
'text/html');
659 $this->debug(
"Failure due to : {$fail->getMessage()}");
660 $this->debug(
"Screenshot saved to {$this->pngReport}");
661 $this->debug(
"Html saved to {$this->htmlReport}");
670 $testDescription =
"unknown." . uniqid();
671 if ($this->current_test !=
null) {
672 $testDescription = Descriptor::getTestSignature($this->current_test);
675 $filename = preg_replace(
'~\W~',
'.', $testDescription);
676 $outputDir = codecept_output_dir();
677 $this->_saveScreenshot($this->pngReport = $outputDir . mb_strcut($filename, 0, 245,
'utf-8') .
'.fail.png');
678 $this->_savePageSource($this->htmlReport = $outputDir . mb_strcut($filename, 0, 244,
'utf-8') .
'.fail.html');
690 parent::amOnPage(
$page);
703 $this->config[
'skipReadiness'] = $check;
713 $this->jsErrors = [];
724 $this->jsErrors[] = $errMsg;
732 private function getJsErrors()
736 if (!empty($this->jsErrors)) {
737 $errors =
'Errors in JavaScript:';
738 foreach ($this->jsErrors as $jsError) {
752 $this->assertEmpty($this->jsErrors, $this->getJsErrors());
skipReadinessCheck($check)
static sanitizeWebDriverConfig($config, $params=['url', 'selenium'])
grabFromCurrentUrl($regex=null)
mSetLocale(int $category, $locale)
seeCurrentUrlMatches($regex)
conditionalClick($selector, $dependentSelector, $visible)
formatMoney(float $money, $locale='en_US.UTF-8')
waitForLoadingMaskToDisappear($timeout=null)
searchAndMultiSelectOption($select, array $options, $requireAction=false)
dontSeeCurrentUrlEquals($url)
selectMultipleOptions($selectSearchTextField, $selectSearchResult, array $options)
waitForAjaxLoad($timeout=null)
_before(TestInterface $test)
seeCurrentUrlEquals($url)
_runAfter(TestInterface $test)
dragAndDrop($source, $target, $xOffset=null, $yOffset=null)
dontSeeInCurrentUrl($needle)
_failed(TestInterface $test, $fail)
assertElementContainsAttribute($selector, $attribute, $value)
fillSecretField($field, $value)
magentoCLI($command, $arguments=null)
dontSeeCurrentUrlMatches($regex)
if(isset($opts->o)) if(! $usingStdout) $l
waitForPageLoad($timeout=null)
static $loadingMasksLocators
_after(TestInterface $test)