Skip to content

Commit

Permalink
A better implementation of checking JS support in capabilities. It sh…
Browse files Browse the repository at this point in the history
…ould be true if not set explicitly to false. Also getting rid of duplicated code.
  • Loading branch information
barancev committed Mar 30, 2017
1 parent 70539cd commit b31dc3f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 95 deletions.
38 changes: 33 additions & 5 deletions java/client/src/org/openqa/selenium/Capabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,47 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;


/**
* Describes a series of key/value pairs that encapsulate aspects of a browser.
*/
public interface Capabilities {

String getBrowserName();
default String getBrowserName() {
return String.valueOf(Optional.ofNullable(getCapability("browserName")).orElse(""));
}

default Platform getPlatform() {
Object rawPlatform = getCapability("platform");

if (rawPlatform == null) {
return null;
}

if (rawPlatform instanceof String) {
return Platform.valueOf((String) rawPlatform);
} else if (rawPlatform instanceof Platform) {
return (Platform) rawPlatform;
}

Platform getPlatform();
throw new IllegalStateException("Platform was neither a string or a Platform: " + rawPlatform);
}

String getVersion();
default String getVersion() {
return String.valueOf(Optional.ofNullable(getCapability("version")).orElse(""));
}

/**
* @deprecated Use is(SUPPORTS_JAVASCRIPT) instead
* @see #is(String)
* @see org.openqa.selenium.remote.CapabilityType#SUPPORTS_JAVASCRIPT
*/
@Deprecated
boolean isJavascriptEnabled();
default boolean isJavascriptEnabled() {
return is("javascriptEnabled");
}

/**
* @return The capabilities as a Map
Expand All @@ -57,7 +78,14 @@ public interface Capabilities {
* @param capabilityName The capability to check.
* @return Whether or not the value is not null and not false.
*/
boolean is(String capabilityName);
default boolean is(String capabilityName) {
Object cap = getCapability(capabilityName);
if (cap == null) {
// javascriptEnabled is true if not set explicitly
return "javascriptEnabled".equals(capabilityName);
}
return cap instanceof Boolean ? (Boolean) cap : Boolean.parseBoolean(String.valueOf(cap));
}

/**
* Merge two {@link Capabilities} together and return the union of the two as a new
Expand Down
48 changes: 0 additions & 48 deletions java/client/src/org/openqa/selenium/ImmutableCapabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,59 +41,11 @@ public ImmutableCapabilities(Map<String, ?> capabilities) {
});
}

@Override
public String getBrowserName() {
return String.valueOf(caps.getOrDefault("browserName", ""));
}

@Override
public Platform getPlatform() {
Object rawPlatform = caps.get("platform");

if (rawPlatform == null) {
return null;
}

if (rawPlatform instanceof String) {
return Platform.valueOf((String) rawPlatform);
} else if (rawPlatform instanceof Platform) {
return (Platform) rawPlatform;
}

throw new IllegalStateException("Platform was neither a string or a Platform: " + rawPlatform);
}

@Override
public String getVersion() {
return String.valueOf(caps.getOrDefault("version", ""));
}

@Override
public boolean isJavascriptEnabled() {
Object raw = caps.getOrDefault("javascriptEnabled", true);
if (raw instanceof String) {
return Boolean.parseBoolean((String) raw);
} else if (raw instanceof Boolean) {
return (Boolean) raw;
}

throw new IllegalStateException("Javascript-enabled capability was of invalid type: " + raw);
}

@Override
public Object getCapability(String capabilityName) {
return caps.get(capabilityName);
}

@Override
public boolean is(String capabilityName) {
Object cap = getCapability(capabilityName);
if (cap == null) {
return false;
}
return cap instanceof Boolean ? (Boolean) cap : Boolean.parseBoolean(String.valueOf(cap));
}

@Override
public Map<String, ?> asMap() {
return Collections.unmodifiableMap(caps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,52 +86,18 @@ public DesiredCapabilities(Capabilities... others) {
}
}

public String getBrowserName() {
Object browserName = capabilities.get(BROWSER_NAME);
return browserName == null ? "" : browserName.toString();
}

public void setBrowserName(String browserName) {
setCapability(BROWSER_NAME, browserName);
}

public String getVersion() {
Object version = capabilities.get(VERSION);
return version == null ? "" : version.toString();
}

public void setVersion(String version) {
setCapability(VERSION, version);
}

public Platform getPlatform() {
if (capabilities.containsKey(PLATFORM)) {
Object raw = capabilities.get(PLATFORM);
if (raw instanceof String) {
return Platform.valueOf((String) raw);
} else if (raw instanceof Platform) {
return (Platform) raw;
}
}
return null;
}

public void setPlatform(Platform platform) {
setCapability(PLATFORM, platform);
}

public boolean isJavascriptEnabled() {
if (capabilities.containsKey(SUPPORTS_JAVASCRIPT)) {
Object raw = capabilities.get(SUPPORTS_JAVASCRIPT);
if (raw instanceof String) {
return Boolean.parseBoolean((String) raw);
} else if (raw instanceof Boolean) {
return ((Boolean) raw).booleanValue();
}
}
return true;
}

public void setJavascriptEnabled(boolean javascriptEnabled) {
setCapability(SUPPORTS_JAVASCRIPT, javascriptEnabled);
}
Expand All @@ -156,14 +122,6 @@ public Object getCapability(String capabilityName) {
return capabilities.get(capabilityName);
}

public boolean is(String capabilityName) {
Object cap = getCapability(capabilityName);
if (cap == null) {
return false;
}
return cap instanceof Boolean ? (Boolean) cap : Boolean.parseBoolean(String.valueOf(cap));
}

/**
* Merges the extra capabilities provided into this DesiredCapabilities instance. If capabilities
* with the same name exist in this instance, they will be overridden by the values from the
Expand Down

0 comments on commit b31dc3f

Please sign in to comment.