Skip to content

Commit

Permalink
[java] Restoring ability to use the same browserName for both Edgium …
Browse files Browse the repository at this point in the history
…and EdgeHTML
  • Loading branch information
barancev committed May 23, 2020
1 parent 979f02a commit 3b36787
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 17 deletions.
27 changes: 21 additions & 6 deletions java/client/src/org/openqa/selenium/edge/EdgeDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@

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

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;

public abstract class EdgeDriverInfo extends ChromiumDriverInfo {
@AutoService(WebDriverInfo.class)
public class EdgeDriverInfo extends ChromiumDriverInfo {

@Override
public String getDisplayName() {
Expand All @@ -41,14 +47,23 @@ public Capabilities getCanonicalCapabilities() {

@Override
public boolean isSupporting(Capabilities capabilities) {
return BrowserType.EDGE.equals(capabilities.getBrowserName()) ||
capabilities.getCapability("ms:edgeChromium") != null ||
capabilities.getCapability("ms:edgeOptions") != null ||
capabilities.getCapability("edgeOptions") != null;
return (BrowserType.EDGE.equals(capabilities.getBrowserName())
|| capabilities.getCapability("ms:edgeOptions") != null
|| capabilities.getCapability("edgeOptions") != null)
&&
(capabilities.getCapability(EdgeOptions.USE_CHROMIUM) == null
|| Objects.equals(capabilities.getCapability(EdgeOptions.USE_CHROMIUM), true));
}

@Override
public abstract boolean isAvailable();
public boolean isAvailable() {
try {
EdgeDriverService.createDefaultService();
return true;
} catch (IllegalStateException | WebDriverException e) {
return false;
}
}

@Override
public int getMaximumSimultaneousSessions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Manages the life and death of the EdgeDriver (MicrosoftWebDriver or MSEdgeDriver).
Expand Down Expand Up @@ -118,6 +119,11 @@ public int score(Capabilities capabilities) {
score++;
}

Object useChromium = capabilities.getCapability(EdgeOptions.USE_CHROMIUM);
if (Objects.equals(useChromium, false)) {
score--;
}

if (capabilities.getCapability(EdgeOptions.CAPABILITY) != null) {
score++;
}
Expand Down
8 changes: 7 additions & 1 deletion java/client/src/org/openqa/selenium/edge/EdgeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

/**
Expand All @@ -41,13 +42,18 @@
*/
public class EdgeOptions extends ChromiumOptions<EdgeOptions> {

/**
* Key used to indicate whether to use an Edge Chromium or Edge Legacy driver.
*/
public static final String USE_CHROMIUM = "ms:edgeChromium";

/**
* Key used to store a set of ChromeOptions in a {@link Capabilities}
* object.
*/
public static final String CAPABILITY = "ms:edgeOptions";

public EdgeOptions() {
super(CapabilityType.BROWSER_NAME, "MicrosoftEdge", CAPABILITY);
super(CapabilityType.BROWSER_NAME, BrowserType.EDGE, CAPABILITY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.remote.BrowserType;

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

@AutoService(WebDriverInfo.class)
Expand All @@ -40,14 +41,16 @@ public String getDisplayName() {

@Override
public Capabilities getCanonicalCapabilities() {
return new ImmutableCapabilities(BROWSER_NAME, BrowserType.EDGEHTML);
return new ImmutableCapabilities(BROWSER_NAME, BrowserType.EDGE, EdgeHtmlOptions.USE_CHROMIUM, false);
}

@Override
public boolean isSupporting(Capabilities capabilities) {
return BrowserType.EDGEHTML.equals(capabilities.getBrowserName()) ||
capabilities.getCapability("ms:edgeOptions") != null ||
capabilities.getCapability("edgeOptions") != null;
return (BrowserType.EDGE.equals(capabilities.getBrowserName())
|| capabilities.getCapability("ms:edgeOptions") != null
|| capabilities.getCapability("edgeOptions") != null)
&&
Objects.equals(capabilities.getCapability(EdgeHtmlOptions.USE_CHROMIUM), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class EdgeHtmlDriverService extends DriverService {

Expand Down Expand Up @@ -88,10 +89,15 @@ public static class Builder extends DriverService.Builder<
public int score(Capabilities capabilities) {
int score = 0;

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

Object useChromium = capabilities.getCapability(EdgeHtmlOptions.USE_CHROMIUM);
if (useChromium == null || Objects.equals(useChromium, true)) {
score--;
}

return score;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.remote.AbstractDriverOptions;
import org.openqa.selenium.remote.BrowserType;

Expand All @@ -37,19 +36,25 @@
* // For use with RemoteWebDriver:
* RemoteWebDriver driver = new RemoteWebDriver(
* new URL("http://localhost:4444/"),
* new EdgeHtmlDriver());
* new EdgeHtmlOptions());
* </code></pre>
*
*/
public class EdgeHtmlOptions extends AbstractDriverOptions<EdgeHtmlOptions> {

/**
* Key used to indicate whether to use an Edge Chromium or Edge Legacy driver.
*/
public static final String USE_CHROMIUM = "ms:edgeChromium";

/**
* Key used to store a set of EdgeHtmlOptions in a {@link Capabilities} object.
*/
public static final String CAPABILITY = "ms:edgeOptions";

public EdgeHtmlOptions() {
setCapability(BROWSER_NAME, BrowserType.EDGEHTML);
setCapability(BROWSER_NAME, BrowserType.EDGE);
setCapability(USE_CHROMIUM, false);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions java/client/src/org/openqa/selenium/remote/BrowserType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public interface BrowserType {
@Deprecated(since = "4.0", forRemoval = true)
String OPERA_BLINK = "operablink";
String OPERA = "opera";
String EDGE = "edgium";
String EDGEHTML = "MicrosoftEdge";
String EDGE = "MicrosoftEdge";
String EDGEHTML = "EdgeHTML";
String IEXPLORE= "iexplore";
String IEXPLORE_PROXY= "iexploreproxy";
String SAFARI_PROXY = "safariproxy";
Expand Down
86 changes: 86 additions & 0 deletions java/client/test/org/openqa/selenium/edge/EdgeDriverInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.edge;

import static org.assertj.core.api.Assertions.assertThat;

import org.assertj.core.api.Condition;
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;

import java.util.Collections;

public class EdgeDriverInfoTest {

@Test
public void canonicalCapabilitiesContainProperBrowserName() {
Capabilities caps = new EdgeDriverInfo().getCanonicalCapabilities();
assertThat(caps.getBrowserName()).isEqualTo(BrowserType.EDGE);
}

@Test
public void isSupportingCapabilitiesWithProperBrowserNameOnly() {
assertThat(new EdgeDriverInfo()).is(supporting(
new ImmutableCapabilities(CapabilityType.BROWSER_NAME, BrowserType.EDGE)));
}

@Test
public void isNotSupportingEdgeHtml() {
assertThat(new EdgeDriverInfo()).isNot(supporting(
new ImmutableCapabilities(CapabilityType.BROWSER_NAME, BrowserType.EDGE,
EdgeOptions.USE_CHROMIUM, false)));
}

@Test
public void isSupportingEdgeWithExplicitlySetChromiumFlag() {
assertThat(new EdgeDriverInfo()).is(supporting(
new ImmutableCapabilities(CapabilityType.BROWSER_NAME, BrowserType.EDGE,
EdgeOptions.USE_CHROMIUM, true)));
}

@Test
public void isNotSupportingFirefox() {
assertThat(new EdgeDriverInfo()).isNot(supporting(
new ImmutableCapabilities(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX)));
}

@Test
public void canDetectBrowserByVendorSpecificCapability() {
assertThat(new EdgeDriverInfo()).is(supporting(
new ImmutableCapabilities(EdgeOptions.CAPABILITY, Collections.emptyMap())));
assertThat(new EdgeDriverInfo()).is(supporting(
new ImmutableCapabilities("edgeOptions", Collections.emptyMap())));
}

@Test
public void canRejectEdgeHtmlByVendorSpecificCapability() {
assertThat(new EdgeDriverInfo()).isNot(supporting(
new ImmutableCapabilities(EdgeOptions.CAPABILITY, Collections.emptyMap(),
EdgeOptions.USE_CHROMIUM, false)));
assertThat(new EdgeDriverInfo()).isNot(supporting(
new ImmutableCapabilities("edgeOptions", Collections.emptyMap(),
EdgeOptions.USE_CHROMIUM, false)));
}

private Condition<EdgeDriverInfo> supporting(Capabilities capabilities) {
return new Condition<>(info -> info.isSupporting(capabilities), "supporting " + capabilities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

package org.openqa.selenium.edge;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.openqa.selenium.edgehtml.EdgeHtmlOptions;

import java.io.File;
import java.time.Duration;
Expand All @@ -47,4 +50,11 @@ public void builderPassesTimeoutToDriverService() {
builderMock.build();
verify(builderMock).createDriverService(any(), anyInt(), eq(customTimeout), any(), any());
}

@Test
public void testScoring() {
EdgeDriverService.Builder builder = new EdgeDriverService.Builder();
assertThat(builder.score(new EdgeOptions())).isGreaterThan(0);
assertThat(builder.score(new EdgeHtmlOptions())).isEqualTo(0);
}
}
Loading

0 comments on commit 3b36787

Please sign in to comment.