|
24 | 24 | import org.openqa.selenium.By; |
25 | 25 | import org.openqa.selenium.NoSuchWindowException; |
26 | 26 | import org.openqa.selenium.Platform; |
| 27 | +import org.openqa.selenium.WebDriverException; |
27 | 28 | import org.openqa.selenium.WebElement; |
28 | 29 | import utam.core.driver.Driver; |
29 | 30 | import utam.core.driver.DriverConfig; |
@@ -192,30 +193,34 @@ private static AppiumDriver checkIOSWebViewContext( |
192 | 193 | private static AppiumDriver switchToContext( |
193 | 194 | AppiumDriver appiumDriver, String context, String window) { |
194 | 195 | SupportsContextSwitching contextSwitcher = (SupportsContextSwitching) appiumDriver; |
| 196 | + AppiumDriver newDriver = null; |
195 | 197 | try { |
196 | | - AppiumDriver newDriver = (AppiumDriver) contextSwitcher.context(context); |
197 | | - if (window != null) { |
198 | | - if (appiumDriver.getCapabilities().getPlatformName().equals(Platform.ANDROID)) { |
199 | | - // On emulators if it's the first window and Android < 13, it will fail if tried without |
200 | | - // the |
201 | | - // 'CDwindow-' prefix. |
202 | | - try { |
203 | | - newDriver.switchTo().window(window); |
204 | | - } catch (NoSuchWindowException e) { |
205 | | - warning("Failed to switch to window: " + window); |
206 | | - newDriver.switchTo().window("CDwindow-" + window); |
207 | | - info("Successfully switched to window: CDwindow-" + window + ", retrying with prefix"); |
208 | | - } |
209 | | - } else { |
210 | | - newDriver.switchTo().window(window); |
211 | | - } |
212 | | - } |
213 | | - return newDriver; |
214 | | - } catch (Exception e) { |
| 198 | + newDriver = (AppiumDriver) contextSwitcher.context(context); |
| 199 | + } catch (WebDriverException e) { |
215 | 200 | error(e); |
216 | 201 | error("Failed to switch to context: " + context); |
217 | 202 | } |
218 | | - return null; |
| 203 | + if (newDriver != null |
| 204 | + && window != null |
| 205 | + && appiumDriver.getCapabilities().getPlatformName().equals(Platform.ANDROID)) { |
| 206 | + // On emulators if it's the first window and Android < 13, it will fail if tried without |
| 207 | + // the |
| 208 | + // 'CDwindow-' prefix. |
| 209 | + try { |
| 210 | + newDriver.switchTo().window(window); |
| 211 | + } catch (NoSuchWindowException e1) { |
| 212 | + warning("Failed to switch to window: " + window); |
| 213 | + String cdWindow = "CDwindow-" + window; |
| 214 | + try { |
| 215 | + newDriver.switchTo().window(cdWindow); |
| 216 | + info("Successfully switched to window: " + cdWindow + ", retrying with prefix"); |
| 217 | + } catch (NoSuchWindowException e2) { |
| 218 | + error("Failed to switch to window: " + cdWindow); |
| 219 | + newDriver = null; |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + return newDriver; |
219 | 224 | } |
220 | 225 |
|
221 | 226 | @Override |
|
0 commit comments