Skip to content

Commit 2847390

Browse files
fix: Get Context Handles Only in WebView Context to Avoid UnsupportedCommandException (#235)
* fix: Get Context Handles Only in WebView Context to Avoid UnsupportedCommandException * Splotless apply * Fix failing test and improve existing exception
1 parent f30abd8 commit 2847390

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

Lines changed: 4 additions & 3 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
104-
|| mobilePlatform == MobilePlatformType.ANDROID_PHONE
105-
|| mobilePlatform == MobilePlatformType.ANDROID_TABLET) {
103+
if ((mobilePlatform == MobilePlatformType.ANDROID
104+
|| mobilePlatform == MobilePlatformType.ANDROID_PHONE
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 & 2 deletions
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

@@ -405,14 +406,42 @@ public void testSwitchToWebViewWithMultipleWebViewsAndroid() {
405406
});
406407
when(driver.getWindowHandles()).thenReturn(windowHandles);
407408
when(driver.getTitle()).thenReturn("").thenReturn(testWebViewTitle);
408-
when(contextSwitcher.getContext()).thenReturn(contextTracker.currentContext);
409+
when(contextSwitcher.getContext())
410+
.thenReturn(contextTracker.currentContext)
411+
.thenReturn(testWebViewHandle);
409412
mock.setMobilePlatform(Platform.LINUX);
410413
MobileDriverAdapter adapter = mock.getMobileDriverAdapter();
411414
assertThat(
412415
adapter.waitFor(() -> adapter.switchToWebView(testWebViewTitle)), is(sameInstance(driver)));
413416
assertThat(windowHandleTracker.currentHandle, is(equalTo(testWindowHandle)));
414417
}
415418

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

0 commit comments

Comments
 (0)