Skip to content

Commit 93b761f

Browse files
Make some of the requested updates
1 parent f5f3871 commit 93b761f

File tree

4 files changed

+95
-60
lines changed

4 files changed

+95
-60
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
<hamcrest.version>2.2</hamcrest.version>
138138
<sarif.version>2.0</sarif.version>
139139
<spotless.version>2.31.0</spotless.version>
140-
<gson.version>2.11.0</gson.version>
141140
</properties>
142141

143142
<scm>

utam-core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@
5454
<groupId>com.fasterxml.jackson.core</groupId>
5555
<artifactId>jackson-annotations</artifactId>
5656
</dependency>
57-
<dependency>
58-
<groupId>com.google.code.gson</groupId>
59-
<artifactId>gson</artifactId>
60-
<version>${gson.version}</version>
61-
</dependency>
6257
<dependency>
6358
<groupId>org.testng</groupId>
6459
<artifactId>testng</artifactId>

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

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
import static utam.core.framework.UtamLogger.info;
1212
import static utam.core.framework.UtamLogger.warning;
1313

14-
import com.google.common.reflect.TypeToken;
15-
import com.google.gson.Gson;
14+
import com.fasterxml.jackson.core.JsonProcessingException;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
1616
import io.appium.java_client.AppiumDriver;
1717
import io.appium.java_client.remote.SupportsContextSwitching;
18-
import java.lang.reflect.Type;
1918
import java.util.List;
2019
import java.util.ListIterator;
2120
import java.util.Map;
@@ -43,8 +42,14 @@
4342
public class MobileDriverAdapter extends DriverAdapter implements Driver {
4443

4544
static final String WEBVIEW_CONTEXT_HANDLE_PREFIX = "WEBVIEW";
46-
static final String WEBVIEW_KEY_ANDROID = "webviewName";
47-
static final String WEBVIEW_KEY_IOS = "id";
45+
static final String WEBVIEW_CONTEXT_KEY_ANDROID = "webviewName";
46+
static final String WEBVIEW_CONTEXT_KEY_IOS = "id";
47+
static final String WEBVIEW_PAGES_KEY = "pages";
48+
static final String WEBVIEW_PAGE_KEY = "id";
49+
static final String WEBVIEW_PAGE_DESCRIPTION_KEY = "description";
50+
static final String WEBVIEW_PAGE_DESCRIPTION_VISIBILITY_KEY = "visible";
51+
static final String WEBVIEW_TITLE_KEY_ANDROID = "title";
52+
static final String WEBVIEW_TITLE_KEY_IOS = "title";
4853
static final String NATIVE_CONTEXT_HANDLE = "NATIVE_APP";
4954
static final String ERR_BRIDGE_TITLE_NULL = "Bridge application title is null, please configure";
5055

@@ -92,15 +97,15 @@ final AppiumDriver switchToWebView(String title) {
9297
List<Map<String, Object>> contexts = (List<Map<String, Object>>) result;
9398
int contextsSize = contexts.size();
9499
ListIterator<Map<String, Object>> li = contexts.listIterator(contextsSize);
100+
AppiumDriver newDriver = null;
95101
// Iterate in reverse since the WebView on screen is often the last context in the list.
96102
while (li.hasPrevious()) {
97103
Map<String, Object> context = li.previous();
98-
AppiumDriver newDriver = null;
99104
if (appiumDriver.getCapabilities().getPlatformName().equals(Platform.ANDROID)
100-
&& context.containsKey(WEBVIEW_KEY_ANDROID)) {
105+
&& context.containsKey(WEBVIEW_CONTEXT_KEY_ANDROID)) {
101106
newDriver = checkAndroidWebViewContext(appiumDriver, title, context);
102107
} else if (appiumDriver.getCapabilities().getPlatformName().equals(Platform.IOS)
103-
&& context.containsKey(WEBVIEW_KEY_IOS)) {
108+
&& context.containsKey(WEBVIEW_CONTEXT_KEY_IOS)) {
104109
newDriver = checkIOSWebViewContext(appiumDriver, title, context);
105110
}
106111
if (newDriver != null) {
@@ -127,20 +132,25 @@ final AppiumDriver switchToWebView(String title) {
127132
*/
128133
private static AppiumDriver checkAndroidWebViewContext(
129134
AppiumDriver appiumDriver, String title, Map<String, Object> context) {
130-
String webviewName = (String) context.get(WEBVIEW_KEY_ANDROID);
131-
if (!context.containsKey("pages")) {
135+
String webviewName = (String) context.get(WEBVIEW_CONTEXT_KEY_ANDROID);
136+
if (!context.containsKey(WEBVIEW_PAGES_KEY)) {
132137
return null;
133138
}
134-
List<Map<String, Object>> pages = (List<Map<String, Object>>) context.get("pages");
139+
List<Map<String, Object>> pages = (List<Map<String, Object>>) context.get(WEBVIEW_PAGES_KEY);
135140
for (Map<String, Object> page : pages) {
136-
String newTitle = (String) page.get("title");
141+
String newTitle = (String) page.get(WEBVIEW_TITLE_KEY_ANDROID);
137142
// 'description' doesn't cast to Map, only String
138-
Type descriptionType = new TypeToken<Map<String, Object>>() {}.getType();
139-
Map<String, Object> description =
140-
new Gson().fromJson((String) page.get("description"), descriptionType);
141-
boolean visible = (boolean) description.getOrDefault("visible", false);
143+
String jsonString = (String) page.get(WEBVIEW_PAGE_DESCRIPTION_KEY);
144+
Map description = null;
145+
try {
146+
description = new ObjectMapper().readValue(jsonString, Map.class);
147+
} catch (JsonProcessingException e) {
148+
error(e);
149+
}
150+
boolean visible =
151+
description != null && (boolean) description.get(WEBVIEW_PAGE_DESCRIPTION_VISIBILITY_KEY);
142152
if (newTitle.equalsIgnoreCase(title) && visible) {
143-
String id = (String) page.get("id");
153+
String id = (String) page.get(WEBVIEW_PAGE_KEY);
144154
AppiumDriver newDriver = switchToContext(appiumDriver, webviewName, id);
145155
if (newDriver != null) {
146156
return newDriver;
@@ -166,11 +176,11 @@ private static AppiumDriver checkAndroidWebViewContext(
166176
*/
167177
private static AppiumDriver checkIOSWebViewContext(
168178
AppiumDriver appiumDriver, String title, Map<String, Object> context) {
169-
String id = (String) context.get(WEBVIEW_KEY_IOS);
179+
String id = (String) context.get(WEBVIEW_CONTEXT_KEY_IOS);
170180
if (id.equals(NATIVE_CONTEXT_HANDLE)) {
171181
return null;
172182
}
173-
String newTitle = (String) context.get("title");
183+
String newTitle = (String) context.get(WEBVIEW_TITLE_KEY_IOS);
174184
if (newTitle.equalsIgnoreCase(title)) {
175185
return switchToContext(appiumDriver, id, null);
176186
}
@@ -183,14 +193,19 @@ private static AppiumDriver switchToContext(
183193
try {
184194
AppiumDriver newDriver = (AppiumDriver) contextSwitcher.context(context);
185195
if (window != null) {
186-
// On emulators if it's the first window and Android < 13, it will fail if tried without the
187-
// 'CDwindow-' prefix.
188-
try {
196+
if (appiumDriver.getCapabilities().getPlatformName().equals(Platform.ANDROID)) {
197+
// On emulators if it's the first window and Android < 13, it will fail if tried without
198+
// the
199+
// 'CDwindow-' prefix.
200+
try {
201+
newDriver.switchTo().window(window);
202+
} catch (NoSuchWindowException e) {
203+
warning("Failed to switch to window: " + window);
204+
newDriver.switchTo().window("CDwindow-" + window);
205+
info("Successfully switched to window: CDwindow-" + window + ", retrying with prefix");
206+
}
207+
} else {
189208
newDriver.switchTo().window(window);
190-
} catch (NoSuchWindowException e) {
191-
warning("Failed to switch to window: " + window);
192-
newDriver.switchTo().window("CDwindow-" + window);
193-
info("Successfully switched to window: CDwindow-" + window + ", retrying with prefix");
194209
}
195210
}
196211
return newDriver;

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

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import static org.mockito.Mockito.when;
2323
import static org.testng.Assert.assertThrows;
2424
import static org.testng.Assert.expectThrows;
25-
import static utam.core.selenium.appium.MobileDriverAdapter.NATIVE_CONTEXT_HANDLE;
26-
import static utam.core.selenium.appium.MobileDriverAdapter.WEBVIEW_CONTEXT_HANDLE_PREFIX;
25+
import static utam.core.selenium.appium.MobileDriverAdapter.*;
2726

2827
import io.appium.java_client.AppiumDriver;
2928
import io.appium.java_client.remote.SupportsContextSwitching;
@@ -131,9 +130,13 @@ public void testSwitchToWebViewTimeout() {
131130

132131
String testWebViewHandle = WEBVIEW_CONTEXT_HANDLE_PREFIX + "_1";
133132

134-
Map<String, Object> nativeContext = Map.of("id", NATIVE_CONTEXT_HANDLE);
133+
Map<String, Object> nativeContext = Map.of(WEBVIEW_CONTEXT_KEY_IOS, NATIVE_CONTEXT_HANDLE);
135134
Map<String, Object> webviewContext =
136-
Map.of("id", testWebViewHandle, "title", DEFAULT_APP_CONTEXT_TITLE);
135+
Map.of(
136+
WEBVIEW_CONTEXT_KEY_IOS,
137+
testWebViewHandle,
138+
WEBVIEW_TITLE_KEY_IOS,
139+
DEFAULT_APP_CONTEXT_TITLE);
137140

138141
Set<String> contextHandles =
139142
Stream.of(NATIVE_CONTEXT_HANDLE, testWebViewHandle).collect(Collectors.toSet());
@@ -160,9 +163,13 @@ public void testSwitchToWebView() {
160163

161164
String testWebViewHandle = WEBVIEW_CONTEXT_HANDLE_PREFIX + "_1";
162165

163-
Map<String, Object> nativeContext = Map.of("id", NATIVE_CONTEXT_HANDLE);
166+
Map<String, Object> nativeContext = Map.of(WEBVIEW_CONTEXT_KEY_IOS, NATIVE_CONTEXT_HANDLE);
164167
Map<String, Object> webviewContext =
165-
Map.of("id", testWebViewHandle, "title", DEFAULT_WEBVIEW_TITLE);
168+
Map.of(
169+
WEBVIEW_CONTEXT_KEY_IOS,
170+
testWebViewHandle,
171+
WEBVIEW_TITLE_KEY_IOS,
172+
DEFAULT_WEBVIEW_TITLE);
166173

167174
Set<String> contextHandles =
168175
Stream.of(NATIVE_CONTEXT_HANDLE, testWebViewHandle).collect(Collectors.toSet());
@@ -196,9 +203,13 @@ public void testSwitchToWebViewAlreadyOnTargetPage() {
196203
SupportsContextSwitching contextSwitcher = mock.getContextSwitcherMock();
197204
MobileDriverAdapter provider = mock.getMobileDriverAdapter();
198205

199-
Map<String, Object> nativeContext = Map.of("id", NATIVE_CONTEXT_HANDLE);
206+
Map<String, Object> nativeContext = Map.of(WEBVIEW_CONTEXT_KEY_IOS, NATIVE_CONTEXT_HANDLE);
200207
Map<String, Object> webviewContext =
201-
Map.of("id", testWebViewHandle, "title", DEFAULT_WEBVIEW_TITLE);
208+
Map.of(
209+
WEBVIEW_CONTEXT_KEY_IOS,
210+
testWebViewHandle,
211+
WEBVIEW_TITLE_KEY_IOS,
212+
DEFAULT_WEBVIEW_TITLE);
202213

203214
Set<String> contextHandles =
204215
new HashSet<>(Arrays.asList(NATIVE_CONTEXT_HANDLE, testWebViewHandle));
@@ -233,9 +244,13 @@ public void testSwitchToBridge() {
233244

234245
String testWebViewHandle = WEBVIEW_CONTEXT_HANDLE_PREFIX + "_1";
235246

236-
Map<String, Object> nativeContext = Map.of("id", NATIVE_CONTEXT_HANDLE);
247+
Map<String, Object> nativeContext = Map.of(WEBVIEW_CONTEXT_KEY_IOS, NATIVE_CONTEXT_HANDLE);
237248
Map<String, Object> webviewContext =
238-
Map.of("id", testWebViewHandle, "title", DEFAULT_APP_CONTEXT_TITLE);
249+
Map.of(
250+
WEBVIEW_CONTEXT_KEY_IOS,
251+
testWebViewHandle,
252+
WEBVIEW_TITLE_KEY_IOS,
253+
DEFAULT_APP_CONTEXT_TITLE);
239254

240255
Set<String> contextHandles =
241256
Stream.of(NATIVE_CONTEXT_HANDLE, testWebViewHandle).collect(Collectors.toSet());
@@ -309,13 +324,13 @@ public void testSwitchToWebViewWait() {
309324
String testWebViewHandle = WEBVIEW_CONTEXT_HANDLE_PREFIX + "_1";
310325
String testWebViewTitle = "Test Application";
311326

312-
Map<String, Object> nativeContext = Map.of("id", NATIVE_CONTEXT_HANDLE);
313-
Map<String, Object> webviewContext = Map.of("id", testWebViewHandle, "title", testWebViewTitle);
327+
Map<String, Object> nativeContext = Map.of(WEBVIEW_CONTEXT_KEY_IOS, NATIVE_CONTEXT_HANDLE);
328+
Map<String, Object> webviewContext =
329+
Map.of(WEBVIEW_CONTEXT_KEY_IOS, testWebViewHandle, WEBVIEW_TITLE_KEY_IOS, testWebViewTitle);
314330

315331
List<Map<String, Object>> contexts = List.of(nativeContext, webviewContext);
316332

317333
when(driver.executeScript(CONTEXT_SCRIPT, CONTEXT_ARGS)).thenReturn(contexts);
318-
when(driver.getTitle()).thenReturn(testWebViewTitle);
319334
when(contextSwitcher.context(anyString()))
320335
.then(
321336
(arg) -> {
@@ -338,20 +353,20 @@ public void testSwitchToWebViewWait() {
338353
public void testSwitchToWebViewAndPageTimeout() {
339354
MockUtilities mock = new MockUtilities(AppiumDriver.class);
340355
AppiumDriver driver = mock.getAppiumDriverMock();
341-
SupportsContextSwitching contextSwitcher = mock.getContextSwitcherMock();
342356

343-
String testWebViewHandle = WEBVIEW_CONTEXT_HANDLE_PREFIX + "_1";
344357
String testWebViewTitle = "Test Application";
345358
String testWebViewName = "WEBVIEW_com.io.appium.setting";
346359

347360
Map<String, Object> page1 =
348-
Map.of("description", "{\"visible\":true}", "title", testWebViewTitle);
361+
Map.of(
362+
WEBVIEW_PAGE_DESCRIPTION_KEY,
363+
"{\"" + WEBVIEW_PAGE_DESCRIPTION_VISIBILITY_KEY + "\":true}",
364+
WEBVIEW_TITLE_KEY_ANDROID,
365+
testWebViewTitle);
349366
List<Map<String, Object>> pages = List.of(page1);
350-
Map context =
351-
Map.of("webview", testWebViewHandle, "pages", pages, "webviewName", testWebViewName);
367+
Map context = Map.of(WEBVIEW_PAGES_KEY, pages, WEBVIEW_CONTEXT_KEY_ANDROID, testWebViewName);
352368

353369
when(driver.executeScript(CONTEXT_SCRIPT, CONTEXT_ARGS)).thenReturn(List.of(context));
354-
when(contextSwitcher.context(testWebViewHandle)).thenReturn(driver);
355370
MobileDriverAdapter adapter = mock.getMobileDriverAdapter();
356371
mock.setMobilePlatform(Platform.ANDROID);
357372
TimeoutException e =
@@ -376,10 +391,11 @@ public void testSwitchToWebViewWithMultipleWebViewsiOS() {
376391
String testWebViewHandle = WEBVIEW_CONTEXT_HANDLE_PREFIX + "_1";
377392
String testWebViewTitle = "Test Application";
378393

379-
Map<String, Object> nativeContext = Map.of("id", NATIVE_CONTEXT_HANDLE);
380-
Map<String, Object> webviewContext1 = Map.of("id", "", "title", "");
394+
Map<String, Object> nativeContext = Map.of(WEBVIEW_CONTEXT_KEY_IOS, NATIVE_CONTEXT_HANDLE);
395+
Map<String, Object> webviewContext1 =
396+
Map.of(WEBVIEW_CONTEXT_KEY_IOS, "", WEBVIEW_TITLE_KEY_IOS, "");
381397
Map<String, Object> webviewContext2 =
382-
Map.of("id", testWebViewHandle, "title", testWebViewTitle);
398+
Map.of(WEBVIEW_CONTEXT_KEY_IOS, testWebViewHandle, WEBVIEW_TITLE_KEY_IOS, testWebViewTitle);
383399

384400
List<Map<String, Object>> contexts = List.of(nativeContext, webviewContext1, webviewContext2);
385401

@@ -412,18 +428,28 @@ public void testSwitchToWebViewWithMultipleWebViewsAndroid() {
412428
SupportsContextSwitching contextSwitcher = mock.getContextSwitcherMock();
413429
TargetLocator mockLocator = mock(TargetLocator.class);
414430

415-
String testWebViewHandle = WEBVIEW_CONTEXT_HANDLE_PREFIX + "_1";
416431
String testWebViewName = "WEBVIEW_com.io.appium.setting";
417432
String testWebViewTitle = "Test Application";
418433
String testWindowHandle = "123";
419434

420-
Map<String, Object> page1 = Map.of("description", "{\"visible\":true}", "id", "", "title", "");
435+
Map<String, Object> page1 =
436+
Map.of(
437+
WEBVIEW_PAGE_DESCRIPTION_KEY,
438+
"{\"" + WEBVIEW_PAGE_DESCRIPTION_VISIBILITY_KEY + "\":true}",
439+
WEBVIEW_PAGE_KEY,
440+
"",
441+
WEBVIEW_TITLE_KEY_ANDROID,
442+
"");
421443
Map<String, Object> page2 =
422444
Map.of(
423-
"description", "{\"visible\":true}", "id", testWindowHandle, "title", testWebViewTitle);
445+
WEBVIEW_PAGE_DESCRIPTION_KEY,
446+
"{\"" + WEBVIEW_PAGE_DESCRIPTION_VISIBILITY_KEY + "\":true}",
447+
WEBVIEW_PAGE_KEY,
448+
testWindowHandle,
449+
WEBVIEW_TITLE_KEY_ANDROID,
450+
testWebViewTitle);
424451
List<Map<String, Object>> pages = List.of(page1, page2);
425-
Map context =
426-
Map.of("webview", testWebViewHandle, "pages", pages, "webviewName", testWebViewName);
452+
Map context = Map.of(WEBVIEW_PAGES_KEY, pages, WEBVIEW_CONTEXT_KEY_ANDROID, testWebViewName);
427453

428454
when(driver.executeScript(CONTEXT_SCRIPT, CONTEXT_ARGS)).thenReturn(List.of(context));
429455
when(contextSwitcher.context(anyString()))
@@ -439,7 +465,7 @@ public void testSwitchToWebViewWithMultipleWebViewsAndroid() {
439465
windowHandleTracker.currentHandle = arg.getArgument(0);
440466
return driver;
441467
});
442-
when(contextSwitcher.getContext()).thenReturn(testWebViewHandle);
468+
when(contextSwitcher.getContext()).thenReturn(testWebViewName);
443469
MobileDriverAdapter adapter = mock.getMobileDriverAdapter();
444470
mock.setMobilePlatform(Platform.ANDROID);
445471
assertThat(

0 commit comments

Comments
 (0)