9
9
10
10
use Facebook \WebDriver \Remote \RemoteWebDriver ;
11
11
use Magento \FunctionalTestingFramework \Helper \Helper ;
12
- use Magento \FunctionalTestingFramework \Module \MagentoWebDriver ;
13
12
use Facebook \WebDriver \Exception \NoSuchWindowException ;
14
13
15
14
/**
@@ -33,32 +32,17 @@ public function switchToWindowWithUrlAndClosePrintDialogIfEncountered(
33
32
$ this ->fail ('Expected URL comparison match type is not valid ' );
34
33
}
35
34
36
- /** @var MagentoWebDriver $magentoWebDriver */
37
35
$ magentoWebDriver = $ this ->getModule ('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver ' );
38
36
39
- /** @var RemoteWebDriver $webDriver */
40
37
$ webDriver = $ magentoWebDriver ->webDriver ;
41
38
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
43
40
try {
44
41
$ magentoWebDriver ->pressKey ('body ' , [\Facebook \WebDriver \WebDriverKeys::ESCAPE ]);
45
42
} 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
47
44
}
48
45
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
46
$ targetWindowHandle = null ;
63
47
$ availableWindowHandles = $ webDriver ->getWindowHandles ();
64
48
@@ -71,13 +55,23 @@ public function switchToWindowWithUrlAndClosePrintDialogIfEncountered(
71
55
// the previous escape press is necessary for this press to close the dialog
72
56
$ magentoWebDriver ->pressKey ('body ' , [\Facebook \WebDriver \WebDriverKeys::ESCAPE ]);
73
57
} 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
75
61
}
76
62
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
+
77
67
continue ;
78
68
}
79
69
80
- $ isWebDriverOnExpectedUrl = $ evaluateIsWebDriverOnExpectedUrl ();
70
+ $ isWebDriverOnExpectedUrl = $ this ->evaluateIsWebDriverOnExpectedUrl (
71
+ $ webDriver ,
72
+ $ expectedUrl ,
73
+ $ expectedUrlComparisonType
74
+ );
81
75
82
76
if ($ isWebDriverOnExpectedUrl ) {
83
77
$ targetWindowHandle = $ webDriver ->getWindowHandle ();
@@ -91,4 +85,32 @@ public function switchToWindowWithUrlAndClosePrintDialogIfEncountered(
91
85
// switch to target window handle
92
86
$ webDriver ->switchTo ()->window ($ targetWindowHandle );
93
87
}
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
+ }
94
116
}
0 commit comments