26
26
27
27
import javax .annotation .Nullable ;
28
28
29
+ import java .util .Optional ;
30
+
29
31
import static io .appium .java_client .pagefactory .bys .ContentType .HTML_OR_DEFAULT ;
30
32
import static io .appium .java_client .pagefactory .bys .ContentType .NATIVE_MOBILE_SPECIFIC ;
31
- import static java .util .Optional .ofNullable ;
32
33
33
34
public final class WebDriverUnpackUtility {
34
35
private static final String NATIVE_APP_PATTERN = "NATIVE_APP" ;
@@ -37,35 +38,53 @@ private WebDriverUnpackUtility() {
37
38
}
38
39
39
40
/**
40
- * This method extract an instance of {@link WebDriver} from the given {@link SearchContext}.
41
+ * This method extracts an instance of the given interface from the given {@link SearchContext}.
42
+ * It is expected that the {@link SearchContext} itself or the object it wraps implements it.
43
+ *
41
44
* @param searchContext is an instance of {@link SearchContext}. It may be the instance of
42
45
* {@link WebDriver} or {@link org.openqa.selenium.WebElement} or some other
43
46
* user's extension/implementation.
44
47
* Note: if you want to use your own implementation then it should implement
45
48
* {@link WrapsDriver} or {@link WrapsElement}
46
- * @return the instance of {@link WebDriver}.
47
- * Note: if the given {@link SearchContext} is not
48
- * {@link WebDriver} and it doesn't implement
49
- * {@link WrapsDriver} or {@link WrapsElement} then this method returns null.
50
- *
49
+ * @param cls interface whose instance is going to be extracted.
50
+ * @return Either an instance of the given interface or Optional.empty().
51
51
*/
52
- @ Nullable
53
- public static WebDriver unpackWebDriverFromSearchContext (SearchContext searchContext ) {
54
- if (searchContext instanceof WebDriver ) {
55
- return (WebDriver ) searchContext ;
52
+ public static <T > Optional <T > unpackObjectFromSearchContext (@ Nullable SearchContext searchContext , Class <T > cls ) {
53
+ if (searchContext == null ) {
54
+ return Optional .empty ();
56
55
}
57
56
57
+ if (cls .isAssignableFrom (searchContext .getClass ())) {
58
+ return Optional .of (cls .cast (searchContext ));
59
+ }
58
60
if (searchContext instanceof WrapsDriver ) {
59
- return unpackWebDriverFromSearchContext (((WrapsDriver ) searchContext ).getWrappedDriver ());
61
+ return unpackObjectFromSearchContext (((WrapsDriver ) searchContext ).getWrappedDriver (), cls );
60
62
}
61
-
62
63
// Search context it is not only WebDriver. WebElement is search context too.
63
64
// RemoteWebElement implements WrapsDriver
64
65
if (searchContext instanceof WrapsElement ) {
65
- return unpackWebDriverFromSearchContext (((WrapsElement ) searchContext ).getWrappedElement ());
66
+ return unpackObjectFromSearchContext (((WrapsElement ) searchContext ).getWrappedElement (), cls );
66
67
}
67
68
68
- return null ;
69
+ return Optional .empty ();
70
+ }
71
+
72
+ /**
73
+ * This method extract an instance of {@link WebDriver} from the given {@link SearchContext}.
74
+ * @param searchContext is an instance of {@link SearchContext}. It may be the instance of
75
+ * {@link WebDriver} or {@link org.openqa.selenium.WebElement} or some other
76
+ * user's extension/implementation.
77
+ * Note: if you want to use your own implementation then it should implement
78
+ * {@link WrapsDriver} or {@link WrapsElement}
79
+ * @return the instance of {@link WebDriver}.
80
+ * Note: if the given {@link SearchContext} is not
81
+ * {@link WebDriver} and it doesn't implement
82
+ * {@link WrapsDriver} or {@link WrapsElement} then this method returns null.
83
+ *
84
+ */
85
+ @ Nullable
86
+ public static WebDriver unpackWebDriverFromSearchContext (@ Nullable SearchContext searchContext ) {
87
+ return unpackObjectFromSearchContext (searchContext , WebDriver .class ).orElse (null );
69
88
}
70
89
71
90
/**
@@ -83,20 +102,17 @@ public static WebDriver unpackWebDriverFromSearchContext(SearchContext searchCon
83
102
* {@link SearchContext} instance doesn't implement {@link ContextAware} and {@link WrapsDriver}
84
103
*/
85
104
public static ContentType getCurrentContentType (SearchContext context ) {
86
- return ofNullable ( unpackWebDriverFromSearchContext ( context )). map ( driver -> {
87
- if (driver instanceof HasBrowserCheck && !(( HasBrowserCheck ) driver ). isBrowser ()) {
88
- return NATIVE_MOBILE_SPECIFIC ;
89
- }
105
+ var browserCheckHolder = unpackObjectFromSearchContext ( context , HasBrowserCheck . class );
106
+ if (browserCheckHolder . filter ( hbc -> ! hbc . isBrowser ()). isPresent ()) {
107
+ return NATIVE_MOBILE_SPECIFIC ;
108
+ }
90
109
91
- if (ContextAware .class .isAssignableFrom (driver .getClass ())) { //it is desktop browser
92
- ContextAware contextAware = (ContextAware ) driver ;
93
- var currentContext = contextAware .getContext ();
94
- if (currentContext != null && currentContext .toUpperCase ().contains (NATIVE_APP_PATTERN )) {
95
- return NATIVE_MOBILE_SPECIFIC ;
96
- }
97
- }
110
+ var contextAware = unpackObjectFromSearchContext (context , ContextAware .class );
111
+ if (contextAware .map (ContextAware ::getContext )
112
+ .filter (c -> c .toUpperCase ().contains (NATIVE_APP_PATTERN )).isPresent ()) {
113
+ return NATIVE_MOBILE_SPECIFIC ;
114
+ }
98
115
99
- return HTML_OR_DEFAULT ;
100
- }).orElse (HTML_OR_DEFAULT );
116
+ return HTML_OR_DEFAULT ;
101
117
}
102
118
}
0 commit comments