Skip to content

Commit 74a4027

Browse files
authored
Fix Java 8 compatibility issues (#313)
1 parent 0fbffec commit 74a4027

File tree

5 files changed

+113
-14
lines changed

5 files changed

+113
-14
lines changed

docs/JavaScriptEnhancements.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Within the script, use **`document`** to refer to the current document. Note tha
3333
import org.openqa.selenium.WebDriver;
3434
import org.openqa.selenium.WebElement;
3535
import com.nordstrom.automation.selenium.core.JsUtility;
36+
import com.nordstrom.automation.selenium.core.WebDriverUtils;
3637

3738
public class JavaScriptExample {
3839

@@ -50,7 +51,7 @@ public class JavaScriptExample {
5051
// Execute script as anonymous function, passing specified argument
5152
WebElement response = JsUtility.runAndReturn(driver, script, "viewport");
5253
// If element reference was returned, extract 'content' attribute
53-
return (response == null) ? null : response.getDomAttribute("content");
54+
return (response == null) ? null : WebDriverUtils.getDomAttributeOf(response, "content");
5455
}
5556
}
5657
```
@@ -88,6 +89,7 @@ import org.openqa.selenium.WebDriver;
8889
import org.openqa.selenium.WebDriverException;
8990
import org.openqa.selenium.WebElement;
9091
import com.nordstrom.automation.selenium.core.JsUtility;
92+
import com.nordstrom.automation.selenium.core.WebDriverUtils;
9193

9294
public class AnotherJavaScriptExample {
9395

@@ -109,7 +111,7 @@ public class AnotherJavaScriptExample {
109111
// Execute script as anonymous function, passing specified argument
110112
WebElement response = JsUtility.runAndReturn(driver, script, name);
111113
// Extract 'content' attribute
112-
return response.getDomAttribute("content");
114+
return WebDriverUtils.getDomAttributeOf(response, "content");
113115
} catch (WebDriverException e) {
114116
// Extract encoded exception
115117
throw JsUtility.propagate(e);

docs/example/TableComponent.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import org.openqa.selenium.By;
1313
import org.openqa.selenium.SearchContext;
1414
import org.openqa.selenium.WebElement;
1515

16+
import com.nordstrom.automation.selenium.core.WebDriverUtils;
17+
1618
public class TableComponent extends PageComponent {
1719

1820
public TableComponent(By locator, ComponentContainer parent) {
@@ -69,7 +71,7 @@ public class TableComponent extends PageComponent {
6971
}
7072

7173
public static Object getKey(SearchContext context) {
72-
return ((WebElement) context).getDomAttribute("id");
74+
return WebDriverUtils.getDomAttributeOf((WebElement) context, "id");
7375
}
7476
}
7577
```

src/main/java/com/nordstrom/automation/selenium/examples/TextEditApplication.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.nordstrom.automation.selenium.examples;
22

3-
import java.util.List;
3+
import static com.nordstrom.automation.selenium.utility.KeysPayloadBuilder.CMD;
4+
45
import java.util.Map;
56
import java.util.Set;
67
import java.util.stream.Collectors;
@@ -10,10 +11,12 @@
1011
import org.openqa.selenium.TimeoutException;
1112
import org.openqa.selenium.WebDriver;
1213
import com.nordstrom.automation.selenium.core.JsUtility;
14+
import com.nordstrom.automation.selenium.core.WebDriverUtils;
1315
import com.nordstrom.automation.selenium.exceptions.NoDocumentAppearedTimeoutException;
1416
import com.nordstrom.automation.selenium.model.Page;
1517
import com.nordstrom.automation.selenium.model.PageComponent;
1618
import com.nordstrom.automation.selenium.support.Coordinator;
19+
import com.nordstrom.automation.selenium.utility.KeysPayloadBuilder;
1720

1821
/**
1922
* This class is the model for the <b>TextEdit</b> application.
@@ -29,12 +32,15 @@ public TextEditApplication(WebDriver driver) {
2932
super(driver);
3033
}
3134

32-
private static final String DOC_WINDOW_TEMPLATE = ".//XCUIElementTypeWindow[@title='%s']";
35+
static {
36+
KeysPayloadBuilder.Builder builder = KeysPayloadBuilder.builder();
37+
CMD_N = builder.key("n", CMD).build();
38+
CMD_O = builder.key("o", CMD).build();
39+
}
3340

34-
private static final Map<String, Object> CMD_N =
35-
Map.of("keys", List.of(Map.of("key", "n", "modifierFlags", 1 << 4)));
36-
private static final Map<String, Object> CMD_O =
37-
Map.of("keys", List.of(Map.of("key", "o", "modifierFlags", 1 << 4)));
41+
private static final Map<String, Object> CMD_N;
42+
private static final Map<String, Object> CMD_O;
43+
private static final String DOC_WINDOW_TEMPLATE = ".//XCUIElementTypeWindow[@title='%s']";
3844

3945
private TextEditManagementPanel managementPanel;
4046

@@ -90,7 +96,7 @@ public TextEditDocumentWindow openNewDocument() {
9096
*/
9197
public Set<String> getOpenDocumentNames() {
9298
return driver.findElements(Using.DOCUMENT_WINDOW.locator).stream()
93-
.map(e -> e.getDomAttribute("title")).collect(Collectors.toSet());
99+
.map(e -> WebDriverUtils.getDomAttributeOf(e, "title")).collect(Collectors.toSet());
94100
}
95101

96102
/**
@@ -118,9 +124,8 @@ public PageComponent apply(final SearchContext context) {
118124
return null;
119125
} else {
120126
String name = currentNames.iterator().next();
121-
String xpath = DOC_WINDOW_TEMPLATE.formatted(name);
122-
By locator = By.xpath(xpath);
123-
return new TextEditDocumentWindow(locator, application);
127+
String xpath = String.format(DOC_WINDOW_TEMPLATE, name);
128+
return new TextEditDocumentWindow(By.xpath(xpath), application);
124129
}
125130
}
126131

src/main/java/com/nordstrom/automation/selenium/examples/TextEditDocumentWindow.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.nordstrom.automation.selenium.examples;
22

33
import org.openqa.selenium.By;
4+
5+
import com.nordstrom.automation.selenium.core.WebDriverUtils;
46
import com.nordstrom.automation.selenium.model.ComponentContainer;
57
import com.nordstrom.automation.selenium.model.PageComponent;
68

@@ -48,7 +50,7 @@ public By locator() {
4850
* @return document window title
4951
*/
5052
public String getDocumentTitle() {
51-
return getWrappedElement().getDomAttribute("title");
53+
return WebDriverUtils.getDomAttributeOf(getWrappedElement(), "title");
5254
}
5355

5456
/**
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.nordstrom.automation.selenium.utility;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.stream.Collectors;
10+
11+
/**
12+
* This utility class facilitates easy assembly lists of key chords for the {@code macos: keys} script
13+
* of Appium's <b>Mac2</b> engine.
14+
*/
15+
public final class KeysPayloadBuilder {
16+
17+
/**
18+
* Private constructor to prevent instantiation.
19+
*/
20+
private KeysPayloadBuilder() {
21+
throw new AssertionError("KeysPayloadBuilder is a static utility class that cannot be instantiated");
22+
}
23+
24+
/** SHIFT modifier */
25+
public static final int SHIFT = 1 << 1;
26+
/** CONTROL modifier */
27+
public static final int CTRL = 1 << 2;
28+
/** OPTION modifier */
29+
public static final int OPT = 1 << 3;
30+
/** COMMAND modifier */
31+
public static final int CMD = 1 << 4;
32+
33+
/**
34+
* Return an instance of the <b>Builder</b> class.
35+
*
36+
* @return new {@link Builder} object
37+
*/
38+
public static Builder builder() {
39+
return new Builder();
40+
}
41+
42+
/**
43+
* This class implements a builder for lists of key chords for the {@code macos: keys} script.
44+
*/
45+
public static final class Builder {
46+
private final List<Map<String, Object>> keys = new ArrayList<>();
47+
48+
/**
49+
* Add new key chord with modifiers.
50+
*
51+
* @param k keystroke character
52+
* @param flags modifier flags (may be omitted)
53+
* @return this {@link Builder} object
54+
* @see KeysPayloadBuilder#SHIFT
55+
* @see KeysPayloadBuilder#CTRL
56+
* @see KeysPayloadBuilder#OPT
57+
* @see KeysPayloadBuilder#CMD
58+
*/
59+
public Builder key(String k, int... flags) {
60+
Map<String, Object> m = new HashMap<>();
61+
m.put("key", k);
62+
63+
if (flags != null && flags.length > 0) {
64+
int combined = Arrays.stream(flags).reduce(0, (a, b) -> a | b);
65+
m.put("modifierFlags", combined);
66+
}
67+
68+
keys.add(m);
69+
return this;
70+
}
71+
72+
/**
73+
* Assemble payload for the {@code macos: keys} script.
74+
*
75+
* @return unmodifiable list of key chords for the {@code macos: keys} script
76+
*/
77+
public Map<String, Object> build() {
78+
Map<String, Object> map = Collections.unmodifiableMap(
79+
Collections.singletonMap(
80+
"keys",
81+
keys.stream()
82+
.map(Collections::unmodifiableMap)
83+
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList))));
84+
keys.clear();
85+
return map;
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)