Skip to content

Commit

Permalink
Replace BrowserType with a Browser interface
Browse files Browse the repository at this point in the history
This allows us to do nice things like group together multiple browser
names that a driver may use (such as both Safari and Edge do) in a
single place without needing people to depend on the browser-specific
maven deps.
  • Loading branch information
shs96c committed Sep 28, 2021
1 parent 05a3a5e commit e6366da
Show file tree
Hide file tree
Showing 49 changed files with 269 additions and 216 deletions.
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/chrome/AddHasCasting.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.BrowserType.CHROME;
import static org.openqa.selenium.remote.Browser.CHROME;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasCasting extends org.openqa.selenium.chromium.AddHasCasting {
Expand All @@ -45,6 +45,6 @@ GET_CAST_ISSUE_MESSAGE, new CommandInfo("session/:sessionId/goog/cast/get_issue_

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> CHROME.equals(caps.getBrowserName());
return CHROME::is;
}
}
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/chrome/AddHasCdp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.BrowserType.CHROME;
import static org.openqa.selenium.remote.Browser.CHROME;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasCdp extends org.openqa.selenium.chromium.AddHasCdp {
Expand All @@ -41,6 +41,6 @@ public Map<String, CommandInfo> getAdditionalCommands() {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> CHROME.equals(caps.getBrowserName());
return CHROME::is;
}
}
8 changes: 4 additions & 4 deletions java/src/org/openqa/selenium/chrome/ChromeDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
package org.openqa.selenium.chrome;

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.chromium.ChromiumDriverInfo;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;

import java.util.Optional;

import static org.openqa.selenium.remote.Browser.CHROME;

@AutoService(WebDriverInfo.class)
public class ChromeDriverInfo extends ChromiumDriverInfo {

Expand All @@ -41,12 +41,12 @@ public String getDisplayName() {

@Override
public Capabilities getCanonicalCapabilities() {
return new ImmutableCapabilities(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
return new ImmutableCapabilities(CapabilityType.BROWSER_NAME, CHROME.browserName());
}

@Override
public boolean isSupporting(Capabilities capabilities) {
return BrowserType.CHROME.equalsIgnoreCase(capabilities.getBrowserName()) ||
return CHROME.is(capabilities) ||
capabilities.getCapability("chromeOptions") != null ||
capabilities.getCapability("goog:chromeOptions") != null;
}
Expand Down
9 changes: 4 additions & 5 deletions java/src/org/openqa/selenium/chrome/ChromeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@

package org.openqa.selenium.chrome;

import static java.util.Collections.unmodifiableList;

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
Expand All @@ -33,6 +29,9 @@
import java.util.List;
import java.util.Map;

import static java.util.Collections.unmodifiableList;
import static org.openqa.selenium.remote.Browser.CHROME;

/**
* Manages the life and death of a ChromeDriver server.
*/
Expand Down Expand Up @@ -160,7 +159,7 @@ public static class Builder extends DriverService.Builder<
public int score(Capabilities capabilities) {
int score = 0;

if (BrowserType.CHROME.equals(capabilities.getBrowserName())) {
if (CHROME.is(capabilities.getBrowserName())) {
score++;
}

Expand Down
6 changes: 3 additions & 3 deletions java/src/org/openqa/selenium/chrome/ChromeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.chromium.ChromiumOptions;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;

import static org.openqa.selenium.remote.Browser.CHROME;

/**
* Class to manage options specific to {@link ChromeDriver}.
*
Expand Down Expand Up @@ -53,8 +54,7 @@ public class ChromeOptions extends ChromiumOptions<ChromeOptions> {
private ChromeDriverLogLevel logLevel;

public ChromeOptions() {

super(CapabilityType.BROWSER_NAME, BrowserType.CHROME, CAPABILITY);
super(CapabilityType.BROWSER_NAME, CHROME.browserName(), CAPABILITY);
}

public ChromeOptions setLogLevel(ChromeDriverLogLevel logLevel){
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/chromium/AddHasCdp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.chromium.ChromiumDriver.KNOWN_CHROMIUM_BROWSERS;
import static org.openqa.selenium.chromium.ChromiumDriver.IS_CHROMIUM_BROWSER;

public abstract class AddHasCdp implements AugmenterProvider<HasCdp>, AdditionalHttpCommands {

Expand All @@ -39,7 +39,7 @@ public abstract class AddHasCdp implements AugmenterProvider<HasCdp>, Additional

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> KNOWN_CHROMIUM_BROWSERS.contains(caps.getBrowserName());
return caps -> IS_CHROMIUM_BROWSER.test(caps.getBrowserName());
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/chromium/AddHasLaunchApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.chromium.ChromiumDriver.KNOWN_CHROMIUM_BROWSERS;
import static org.openqa.selenium.chromium.ChromiumDriver.IS_CHROMIUM_BROWSER;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasLaunchApp implements AugmenterProvider<HasLaunchApp>, AdditionalHttpCommands {
Expand All @@ -47,7 +47,7 @@ public Map<String, CommandInfo> getAdditionalCommands() {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> KNOWN_CHROMIUM_BROWSERS.contains(caps.getBrowserName());
return caps -> IS_CHROMIUM_BROWSER.test(caps.getBrowserName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.chromium.ChromiumDriver.KNOWN_CHROMIUM_BROWSERS;
import static org.openqa.selenium.chromium.ChromiumDriver.IS_CHROMIUM_BROWSER;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasNetworkConditions implements AugmenterProvider<HasNetworkConditions>, AdditionalHttpCommands {
Expand All @@ -53,7 +53,7 @@ public Map<String, CommandInfo> getAdditionalCommands() {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> KNOWN_CHROMIUM_BROWSERS.contains(caps.getBrowserName());
return caps -> IS_CHROMIUM_BROWSER.test(caps.getBrowserName());
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/chromium/AddHasPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.chromium.ChromiumDriver.KNOWN_CHROMIUM_BROWSERS;
import static org.openqa.selenium.chromium.ChromiumDriver.IS_CHROMIUM_BROWSER;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasPermissions implements AugmenterProvider<HasPermissions>, AdditionalHttpCommands {
Expand All @@ -47,7 +47,7 @@ public Map<String, CommandInfo> getAdditionalCommands() {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> KNOWN_CHROMIUM_BROWSERS.contains(caps.getBrowserName());
return caps -> IS_CHROMIUM_BROWSER.test(caps.getBrowserName());
}

@Override
Expand Down
12 changes: 8 additions & 4 deletions java/src/org/openqa/selenium/chromium/ChromiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.openqa.selenium.logging.EventType;
import org.openqa.selenium.logging.HasLogEvents;
import org.openqa.selenium.mobile.NetworkConnection;
import org.openqa.selenium.remote.Browser;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteTouchScreen;
Expand All @@ -62,9 +63,9 @@
import java.util.function.Supplier;
import java.util.logging.Logger;

import static org.openqa.selenium.remote.BrowserType.CHROME;
import static org.openqa.selenium.remote.BrowserType.EDGE;
import static org.openqa.selenium.remote.BrowserType.OPERA;
import static org.openqa.selenium.remote.Browser.CHROME;
import static org.openqa.selenium.remote.Browser.EDGE;
import static org.openqa.selenium.remote.Browser.OPERA;

/**
* A {@link WebDriver} implementation that controls a Chromium browser running on the local machine.
Expand All @@ -84,7 +85,10 @@ public class ChromiumDriver extends RemoteWebDriver implements
NetworkConnection,
WebStorage {

public static final List<String> KNOWN_CHROMIUM_BROWSERS = ImmutableList.of(CHROME, EDGE, OPERA, "msedge");
public static final Predicate<String> IS_CHROMIUM_BROWSER = name ->
CHROME.is(name) ||
EDGE.is(name) ||
OPERA.is(name);
private static final Logger LOG = Logger.getLogger(ChromiumDriver.class.getName());

private final Capabilities capabilities;
Expand Down
4 changes: 3 additions & 1 deletion java/src/org/openqa/selenium/edge/AddHasCasting.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.Browser.EDGE;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasCasting extends org.openqa.selenium.chromium.AddHasCasting {

Expand All @@ -45,6 +47,6 @@ public Map<String, CommandInfo> getAdditionalCommands() {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> "msedge".equals(caps.getBrowserName());
return EDGE::is;
}
}
4 changes: 3 additions & 1 deletion java/src/org/openqa/selenium/edge/AddHasCdp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.Browser.EDGE;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasCdp extends org.openqa.selenium.chromium.AddHasCdp {

Expand All @@ -41,6 +43,6 @@ public Map<String, CommandInfo> getAdditionalCommands() {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> "msedge".equals(caps.getBrowserName());
return EDGE::is;
}
}
7 changes: 3 additions & 4 deletions java/src/org/openqa/selenium/edge/EdgeDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
package org.openqa.selenium.edge;

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.chromium.ChromiumDriverInfo;
import org.openqa.selenium.remote.BrowserType;

import java.util.Objects;
import java.util.Optional;

import static org.openqa.selenium.remote.Browser.EDGE;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

@AutoService(WebDriverInfo.class)
Expand All @@ -42,12 +41,12 @@ public String getDisplayName() {

@Override
public Capabilities getCanonicalCapabilities() {
return new ImmutableCapabilities(BROWSER_NAME, BrowserType.EDGE);
return new ImmutableCapabilities(BROWSER_NAME, EDGE.browserName());
}

@Override
public boolean isSupporting(Capabilities capabilities) {
return (BrowserType.EDGE.equalsIgnoreCase(capabilities.getBrowserName())
return (EDGE.is(capabilities.getBrowserName())
|| capabilities.getCapability("ms:edgeOptions") != null
|| capabilities.getCapability("edgeOptions") != null)
&&
Expand Down
11 changes: 5 additions & 6 deletions java/src/org/openqa/selenium/edge/EdgeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@
// under the License.
package org.openqa.selenium.edge;

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
Expand All @@ -35,6 +30,10 @@
import java.util.Map;
import java.util.Objects;

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
import static org.openqa.selenium.remote.Browser.EDGE;

/**
* Manages the life and death of the EdgeDriver (MicrosoftWebDriver or MSEdgeDriver).
*/
Expand Down Expand Up @@ -122,7 +121,7 @@ public static class Builder extends DriverService.Builder<
public int score(Capabilities capabilities) {
int score = 0;

if (BrowserType.EDGE.equals(capabilities.getBrowserName())) {
if (EDGE.is(capabilities)) {
score++;
}

Expand Down
5 changes: 3 additions & 2 deletions java/src/org/openqa/selenium/edge/EdgeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.chromium.ChromiumOptions;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;

import static org.openqa.selenium.remote.Browser.EDGE;

/**
* Class to manage options specific to {@link EdgeDriver}.
*
Expand Down Expand Up @@ -54,7 +55,7 @@ public class EdgeOptions extends ChromiumOptions<EdgeOptions> {
public static final String CAPABILITY = "ms:edgeOptions";

public EdgeOptions() {
super(CapabilityType.BROWSER_NAME, BrowserType.EDGE, CAPABILITY);
super(CapabilityType.BROWSER_NAME, EDGE.browserName(), CAPABILITY);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/firefox/AddHasContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.BrowserType.FIREFOX;
import static org.openqa.selenium.remote.Browser.FIREFOX;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasContext implements AugmenterProvider<HasContext>, AdditionalHttpCommands {
Expand All @@ -47,7 +47,7 @@ public Map<String, CommandInfo> getAdditionalCommands() {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> FIREFOX.equals(caps.getBrowserName());
return FIREFOX::is;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/firefox/AddHasExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.BrowserType.FIREFOX;
import static org.openqa.selenium.remote.Browser.FIREFOX;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasExtensions implements AugmenterProvider<HasExtensions>, AdditionalHttpCommands {
Expand All @@ -50,7 +50,7 @@ public Map<String, CommandInfo> getAdditionalCommands() {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> FIREFOX.equals(caps.getBrowserName());
return FIREFOX::is;
}

@Override
Expand Down
Loading

0 comments on commit e6366da

Please sign in to comment.