99
1010use Facebook \WebDriver \Remote \RemoteWebDriver ;
1111use Magento \FunctionalTestingFramework \Helper \Helper ;
12- use Magento \FunctionalTestingFramework \Module \MagentoWebDriver ;
1312use Facebook \WebDriver \Exception \NoSuchWindowException ;
1413
1514/**
@@ -33,32 +32,17 @@ public function switchToWindowWithUrlAndClosePrintDialogIfEncountered(
3332 $ this ->fail ('Expected URL comparison match type is not valid ' );
3433 }
3534
36- /** @var MagentoWebDriver $magentoWebDriver */
3735 $ magentoWebDriver = $ this ->getModule ('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver ' );
3836
39- /** @var RemoteWebDriver $webDriver */
4037 $ webDriver = $ magentoWebDriver ->webDriver ;
4138
42- // Pressing escape blurs the window and "unfreezes" chromedriver when it switches context back to chrome:: /print
39+ // Pressing escape blurs the window and "unfreezes" chromedriver when it switches context back to chrome:/ /print
4340 try {
4441 $ magentoWebDriver ->pressKey ('body ' , [\Facebook \WebDriver \WebDriverKeys::ESCAPE ]);
4542 } catch (NoSuchWindowException $ e ) {
46- // This caught exception cannot be explained: no windows are closed as a result of this action; proceed
43+ // This caught exception cannot be explained; no windows are closed as a result of this action; proceed
4744 }
4845
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-
6246 $ targetWindowHandle = null ;
6347 $ availableWindowHandles = $ webDriver ->getWindowHandles ();
6448
@@ -71,13 +55,23 @@ public function switchToWindowWithUrlAndClosePrintDialogIfEncountered(
7155 // the previous escape press is necessary for this press to close the dialog
7256 $ magentoWebDriver ->pressKey ('body ' , [\Facebook \WebDriver \WebDriverKeys::ESCAPE ]);
7357 } catch (NoSuchWindowException $ e ) {
74- // Print dialog closes yet exception is raised when it tries to get session context; proceed
58+ // Print dialog successfully closes when requested in selenium,
59+ // yet missing window message is sent back in the response
60+ // when it evaluates the value on the element after the press; proceed
7561 }
7662
63+ // selenium is now effectively detached from any window; attach to an available window handle in case
64+ // "fail" method is called and MFTF "after"/teardown steps need to be executed
65+ $ webDriver ->switchTo ()->window ($ webDriver ->getWindowHandles ()[0 ]);
66+
7767 continue ;
7868 }
7969
80- $ isWebDriverOnExpectedUrl = $ evaluateIsWebDriverOnExpectedUrl ();
70+ $ isWebDriverOnExpectedUrl = $ this ->evaluateIsWebDriverOnExpectedUrl (
71+ $ webDriver ,
72+ $ expectedUrl ,
73+ $ expectedUrlComparisonType
74+ );
8175
8276 if ($ isWebDriverOnExpectedUrl ) {
8377 $ targetWindowHandle = $ webDriver ->getWindowHandle ();
@@ -91,4 +85,32 @@ public function switchToWindowWithUrlAndClosePrintDialogIfEncountered(
9185 // switch to target window handle
9286 $ webDriver ->switchTo ()->window ($ targetWindowHandle );
9387 }
88+
89+ /**
90+ * Is $webDriver currently attached to a window that matches $expectedUrl?
91+ *
92+ * @param RemoteWebDriver $webDriver
93+ * @param string $expectedUrl
94+ * @param string $expectedUrlComparisonType
95+ * @return bool
96+ */
97+ private function evaluateIsWebDriverOnExpectedUrl (
98+ RemoteWebDriver $ webDriver ,
99+ string $ expectedUrl ,
100+ string $ expectedUrlComparisonType
101+ ): bool {
102+ $ currentWebDriverUrlPath = parse_url ($ webDriver ->getCurrentURL (), PHP_URL_PATH );
103+
104+ switch ($ expectedUrlComparisonType ) {
105+ case self ::COMPARISON_PATH_EXACT_MATCH :
106+ $ isWebDriverOnExpectedUrl = $ currentWebDriverUrlPath === $ expectedUrl ;
107+ break ;
108+ case self ::COMPARISON_PATH_SUBSET_MATCH :
109+ default :
110+ $ isWebDriverOnExpectedUrl = strpos ($ currentWebDriverUrlPath , $ expectedUrl ) !== false ;
111+ break ;
112+ }
113+
114+ return $ isWebDriverOnExpectedUrl ;
115+ }
94116}
0 commit comments