Skip to content

Commit bd85c3c

Browse files
Address try-catch clauses
1 parent 485f9ff commit bd85c3c

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

utam-core/src/main/java/utam/core/selenium/appium/MobileDriverAdapter.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openqa.selenium.By;
2525
import org.openqa.selenium.NoSuchWindowException;
2626
import org.openqa.selenium.Platform;
27+
import org.openqa.selenium.WebDriverException;
2728
import org.openqa.selenium.WebElement;
2829
import utam.core.driver.Driver;
2930
import utam.core.driver.DriverConfig;
@@ -192,30 +193,34 @@ private static AppiumDriver checkIOSWebViewContext(
192193
private static AppiumDriver switchToContext(
193194
AppiumDriver appiumDriver, String context, String window) {
194195
SupportsContextSwitching contextSwitcher = (SupportsContextSwitching) appiumDriver;
196+
AppiumDriver newDriver = null;
195197
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) {
215200
error(e);
216201
error("Failed to switch to context: " + context);
217202
}
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;
219224
}
220225

221226
@Override

0 commit comments

Comments
 (0)