|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Ui\Test\Mftf\Helper; |
| 9 | + |
| 10 | +use Facebook\WebDriver\Remote\RemoteWebDriver; |
| 11 | +use Magento\FunctionalTestingFramework\Helper\Helper; |
| 12 | +use Magento\FunctionalTestingFramework\Module\MagentoWebDriver; |
| 13 | +use Facebook\WebDriver\Exception\NoSuchWindowException; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class for MFTF helpers for Ui module. |
| 17 | + */ |
| 18 | +class UiHelper extends Helper |
| 19 | +{ |
| 20 | + private const COMPARISON_PATH_EXACT_MATCH = 'COMPARISON_PATH_EXACT_MATCH'; |
| 21 | + private const COMPARISON_PATH_SUBSET_MATCH = 'COMPARISON_PATH_SUBSET_MATCH'; |
| 22 | + |
| 23 | + private const COMPARISON_MATCH_TYPES = [ |
| 24 | + self::COMPARISON_PATH_EXACT_MATCH, |
| 25 | + self::COMPARISON_PATH_SUBSET_MATCH |
| 26 | + ]; |
| 27 | + |
| 28 | + public function switchToWindowWithUrlAndClosePrintDialogIfEncountered( |
| 29 | + string $expectedUrl, |
| 30 | + string $expectedUrlComparisonType |
| 31 | + ) { |
| 32 | + if (!in_array($expectedUrlComparisonType, self::COMPARISON_MATCH_TYPES)) { |
| 33 | + $this->fail('Expected URL comparison match type is not valid'); |
| 34 | + } |
| 35 | + |
| 36 | + /** @var MagentoWebDriver $magentoWebDriver */ |
| 37 | + $magentoWebDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver'); |
| 38 | + |
| 39 | + /** @var RemoteWebDriver $webDriver */ |
| 40 | + $webDriver = $magentoWebDriver->webDriver; |
| 41 | + |
| 42 | + // Pressing escape blurs the window and "unfreezes" chromedriver when it switches context back to chrome::/print |
| 43 | + try { |
| 44 | + $magentoWebDriver->pressKey('body', [\Facebook\WebDriver\WebDriverKeys::ESCAPE]); |
| 45 | + } catch (NoSuchWindowException $e) { |
| 46 | + // This caught exception cannot be explained: no windows are closed as a result of this action; proceed |
| 47 | + } |
| 48 | + |
| 49 | + $evaluateIsWebDriverOnExpectedUrl = function () use ($webDriver, $expectedUrl, $expectedUrlComparisonType) { |
| 50 | + if ($expectedUrlComparisonType === self::COMPARISON_PATH_EXACT_MATCH) { |
| 51 | + $isWebDriverOnExpectedUrl = parse_url($webDriver->getCurrentURL(), PHP_URL_PATH) === $expectedUrl; |
| 52 | + } else { // COMPARISON_PATH_SUBSET_MATCH |
| 53 | + $isWebDriverOnExpectedUrl = strpos( |
| 54 | + parse_url($webDriver->getCurrentURL(), PHP_URL_PATH), |
| 55 | + $expectedUrl |
| 56 | + ) !== false; |
| 57 | + } |
| 58 | + |
| 59 | + return $isWebDriverOnExpectedUrl; |
| 60 | + }; |
| 61 | + |
| 62 | + $targetWindowHandle = null; |
| 63 | + $availableWindowHandles = $webDriver->getWindowHandles(); |
| 64 | + |
| 65 | + foreach ($availableWindowHandles as $availableWindowHandle) { |
| 66 | + $webDriver->switchTo()->window($availableWindowHandle); |
| 67 | + |
| 68 | + if ($webDriver->getCurrentURL() === 'chrome://print/') { |
| 69 | + try { |
| 70 | + // this escape press actually closes the print dialog |
| 71 | + // the previous escape press is necessary for this press to close the dialog |
| 72 | + $magentoWebDriver->pressKey('body', [\Facebook\WebDriver\WebDriverKeys::ESCAPE]); |
| 73 | + } catch (NoSuchWindowException $e) { |
| 74 | + // Print dialog closes yet exception is raised when it tries to get session context; proceed |
| 75 | + } |
| 76 | + |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + $isWebDriverOnExpectedUrl = $evaluateIsWebDriverOnExpectedUrl(); |
| 81 | + |
| 82 | + if ($isWebDriverOnExpectedUrl) { |
| 83 | + $targetWindowHandle = $webDriver->getWindowHandle(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (!$targetWindowHandle) { |
| 88 | + $this->fail('Could not find window handle with requested expected url'); |
| 89 | + } |
| 90 | + |
| 91 | + // switch to target window handle |
| 92 | + $webDriver->switchTo()->window($targetWindowHandle); |
| 93 | + } |
| 94 | +} |
0 commit comments