forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasBrowserCheck.java
More file actions
43 lines (39 loc) · 1.49 KB
/
HasBrowserCheck.java
File metadata and controls
43 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package io.appium.java_client;
import io.appium.java_client.internal.CapabilityHelpers;
import io.appium.java_client.remote.SupportsContextSwitching;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CapabilityType;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Locale.ROOT;
import static java.util.Objects.requireNonNull;
public interface HasBrowserCheck extends ExecutesMethod, HasCapabilities {
String NATIVE_CONTEXT = "NATIVE_APP";
/**
* Validates if the driver is currently in a web browser context.
*
* @return true or false.
*/
default boolean isBrowser() {
String browserName = CapabilityHelpers.getCapability(getCapabilities(),
CapabilityType.BROWSER_NAME, String.class);
if (!isNullOrEmpty(browserName)) {
try {
return requireNonNull(
CommandExecutionHelper.executeScript(this, "return !!window.navigator;")
);
} catch (WebDriverException ign) {
// ignore
}
}
if (!(this instanceof SupportsContextSwitching)) {
return false;
}
try {
var context = ((SupportsContextSwitching) this).getContext();
return context != null && !context.toUpperCase(ROOT).contains(NATIVE_CONTEXT);
} catch (WebDriverException e) {
return false;
}
}
}