Skip to content

Commit b7ec561

Browse files
fix: Get Context Handles Only in WebView Context to Avoid UnsupportedCommandException
1 parent f30abd8 commit b7ec561

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ final AppiumDriver switchToWebView(String title) {
100100
// For the Appium chromedriver limitation to handle multiple WebViews,
101101
// If switch to context fail to find the target WebView, then switch to
102102
// use window
103-
if (mobilePlatform == MobilePlatformType.ANDROID
103+
if ((mobilePlatform == MobilePlatformType.ANDROID
104104
|| mobilePlatform == MobilePlatformType.ANDROID_PHONE
105-
|| mobilePlatform == MobilePlatformType.ANDROID_TABLET) {
105+
|| mobilePlatform == MobilePlatformType.ANDROID_TABLET)
106+
&& !isNativeContext()) {
106107
Set<String> windowHandles = appiumDriver.getWindowHandles();
107108
for (String windowHandle : windowHandles) {
108109
if (!windowHandle.equals(NATIVE_CONTEXT_HANDLE)) {

utam-core/src/test/java/utam/core/selenium/appium/MobileDriverAdapterTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
import java.util.Arrays;
3131
import java.util.Collections;
3232
import java.util.HashSet;
33+
import java.util.List;
3334
import java.util.Set;
3435
import java.util.stream.Collectors;
3536
import java.util.stream.Stream;
3637
import org.openqa.selenium.Platform;
3738
import org.openqa.selenium.TimeoutException;
39+
import org.openqa.selenium.UnsupportedCommandException;
3840
import org.openqa.selenium.WebDriver.Navigation;
3941
import org.openqa.selenium.WebDriver.TargetLocator;
4042
import org.testng.annotations.Test;
@@ -253,7 +255,6 @@ public void testIsAnyWebViewContextAvailableWithNoWebView() {
253255
@Test
254256
public void testIsAnyWebViewContextAvailable() {
255257
MockUtilities mock = new MockUtilities(AppiumDriver.class);
256-
AppiumDriver driver = (AppiumDriver) mock.getWebDriverMock();
257258
SupportsContextSwitching contextSwitcher = mock.getContextSwitcherMock();
258259
MobileDriverAdapter provider = (MobileDriverAdapter) mock.getDriverAdapter();
259260

@@ -413,6 +414,35 @@ public void testSwitchToWebViewWithMultipleWebViewsAndroid() {
413414
assertThat(windowHandleTracker.currentHandle, is(equalTo(testWindowHandle)));
414415
}
415416

417+
/**
418+
* Tests that the expectation to switch to no windows and not attempt to get handles when only
419+
* native context exists on Android platform, negative case
420+
*/
421+
@Test
422+
public void testSwitchToWebViewWithNoWebViewsAndroid() {
423+
ContextTracker contextTracker = new ContextTracker();
424+
425+
MockUtilities mock = new MockUtilities(AppiumDriver.class);
426+
AppiumDriver driver = mock.getAppiumDriverMock();
427+
SupportsContextSwitching contextSwitcher = mock.getContextSwitcherMock();
428+
429+
Set<String> contextHandles = new HashSet<>(List.of(NATIVE_CONTEXT_HANDLE));
430+
431+
String testWebViewTitle = "Test Application";
432+
when(driver.getCapabilities().getPlatformName()).thenReturn(Platform.ANDROID);
433+
when(contextSwitcher.getContextHandles()).thenReturn(contextHandles);
434+
when(driver.getWindowHandles())
435+
.then(
436+
(arg) -> {
437+
throw new UnsupportedCommandException("getWindowHandles {}");
438+
});
439+
when(contextSwitcher.getContext()).thenReturn(contextTracker.currentContext);
440+
mock.setMobilePlatform(Platform.LINUX);
441+
MobileDriverAdapter adapter = mock.getMobileDriverAdapter();
442+
assertThat(adapter.switchToWebView(testWebViewTitle), nullValue());
443+
assertThat(contextTracker.currentContext, is(equalTo(NATIVE_CONTEXT_HANDLE)));
444+
}
445+
416446
@Test
417447
public void testSetPageContext() {
418448
MockUtilities mock = new MockUtilities(AppiumDriver.class);

0 commit comments

Comments
 (0)