Skip to content

Commit

Permalink
[java] Creating browser name for SAFARI_TECH_PREVIEW
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed May 27, 2022
1 parent 268161f commit 6e7cf5d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 34 deletions.
1 change: 1 addition & 0 deletions java/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ v4.2.0
* Fix screen rotate error (#10693)
* Make the action movement methods specify the button number
* Convert RemoteWebElement::getLocation and ::getSize from JWP Standard to W3C Standard (#10700)
* Creating browser name for SAFARI_TECH_PREVIEW

v4.1.4
======
Expand Down
32 changes: 21 additions & 11 deletions java/src/org/openqa/selenium/remote/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@
*/
public interface Browser {

String browserName();

default boolean is(String browserName) {
return browserName().equals(browserName);
}

default boolean is(Capabilities caps) {
Require.nonNull("Capabilities", caps);
return is(caps.getBrowserName());
}

Browser CHROME = () -> "chrome";
Browser EDGE = new Browser() {
@Override
Expand Down Expand Up @@ -72,6 +61,27 @@ public boolean is(String browserName) {
return browserName().equals(browserName) || "Safari".equals(browserName);
}
};
Browser SAFARI_TECH_PREVIEW = new Browser() {
@Override
public String browserName() {
return "Safari Technology Preview";
}

public boolean is(String browserName) {
return browserName().equals(browserName);
}
};

String browserName();

default boolean is(String browserName) {
return browserName().equals(browserName);
}

default boolean is(Capabilities caps) {
Require.nonNull("Capabilities", caps);
return is(caps.getBrowserName());
}

default String toJson() {
return browserName();
Expand Down
11 changes: 6 additions & 5 deletions java/src/org/openqa/selenium/safari/SafariDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

package org.openqa.selenium.safari;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.openqa.selenium.Platform.MAC;
import static org.openqa.selenium.remote.Browser.SAFARI;

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
Expand All @@ -36,6 +32,11 @@
import java.util.List;
import java.util.Map;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.openqa.selenium.Platform.MAC;
import static org.openqa.selenium.remote.Browser.SAFARI;
import static org.openqa.selenium.remote.Browser.SAFARI_TECH_PREVIEW;

public class SafariDriverService extends DriverService {

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ public int score(Capabilities capabilities) {

if (SAFARI.is(capabilities)) {
score++;
} else if (SafariOptions.SAFARI_TECH_PREVIEW.equals(capabilities.getBrowserName())) {
} else if (SAFARI_TECH_PREVIEW.browserName().equals(capabilities.getBrowserName())) {
score++;
}

Expand Down
15 changes: 8 additions & 7 deletions java/src/org/openqa/selenium/safari/SafariOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package org.openqa.selenium.safari;

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

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.internal.Require;
Expand All @@ -28,6 +25,10 @@
import java.util.Collections;
import java.util.Set;

import static org.openqa.selenium.remote.Browser.SAFARI;
import static org.openqa.selenium.remote.Browser.SAFARI_TECH_PREVIEW;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

/**
* Class to manage options specific to {@link SafariDriver}.
*
Expand All @@ -47,8 +48,6 @@
*/
public class SafariOptions extends AbstractDriverOptions<SafariOptions> {

static final String SAFARI_TECH_PREVIEW = "Safari Technology Preview";

public SafariOptions() {
setUseTechnologyPreview(false);
setCapability(BROWSER_NAME, SAFARI.browserName());
Expand Down Expand Up @@ -121,7 +120,7 @@ public SafariOptions setAutomaticProfiling(boolean automaticProfiling) {
}

public boolean getUseTechnologyPreview() {
return SAFARI_TECH_PREVIEW.equals(getBrowserName());
return SAFARI_TECH_PREVIEW.browserName().equals(getBrowserName());
}

/**
Expand All @@ -133,7 +132,9 @@ public boolean getUseTechnologyPreview() {
*/
public SafariOptions setUseTechnologyPreview(boolean useTechnologyPreview) {
// Use an object here, rather than a boolean to avoid a stack overflow
super.setCapability(BROWSER_NAME, useTechnologyPreview ? SAFARI_TECH_PREVIEW : "safari");
super.setCapability(BROWSER_NAME,
useTechnologyPreview ?
SAFARI_TECH_PREVIEW.browserName() : SAFARI.browserName());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.openqa.selenium.safari;

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

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
Expand All @@ -30,6 +28,10 @@

import java.util.Optional;

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

@SuppressWarnings("unused")
@AutoService(WebDriverInfo.class)
public class SafariTechPreviewDriverInfo implements WebDriverInfo {

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

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

@Override
public boolean isSupporting(Capabilities capabilities) {
if (SafariOptions.SAFARI_TECH_PREVIEW.equalsIgnoreCase(capabilities.getBrowserName())) {
if (SAFARI_TECH_PREVIEW.browserName().equalsIgnoreCase(capabilities.getBrowserName())) {
return true;
}

Expand Down Expand Up @@ -77,7 +79,7 @@ public int getMaximumSimultaneousSessions() {

@Override
public Optional<WebDriver> createDriver(Capabilities capabilities)
throws SessionNotCreatedException {
throws SessionNotCreatedException {
if (!isAvailable()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.openqa.selenium.safari;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
Expand All @@ -33,6 +31,9 @@
import java.util.List;
import java.util.Map;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.openqa.selenium.remote.Browser.SAFARI_TECH_PREVIEW;

public class SafariTechPreviewDriverService extends DriverService {

/**
Expand Down Expand Up @@ -82,7 +83,7 @@ public static class Builder extends DriverService.Builder<
public int score(Capabilities capabilities) {
int score = 0;

if (SafariOptions.SAFARI_TECH_PREVIEW.equals(capabilities.getBrowserName())) {
if (SAFARI_TECH_PREVIEW.browserName().equals(capabilities.getBrowserName())) {
score++;
}

Expand Down
7 changes: 4 additions & 3 deletions java/test/org/openqa/selenium/safari/SafariOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.testing.UnitTests;

Expand All @@ -32,6 +32,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.openqa.selenium.remote.Browser.SAFARI;
import static org.openqa.selenium.remote.Browser.SAFARI_TECH_PREVIEW;

@Category(UnitTests.class)
public class SafariOptionsTest {
Expand All @@ -53,7 +54,7 @@ public void canConstructFromCapabilities() {
assertThat(options.getUseTechnologyPreview()).isFalse();

options = new SafariOptions(
new ImmutableCapabilities(CapabilityType.BROWSER_NAME, SafariOptions.SAFARI_TECH_PREVIEW));
new ImmutableCapabilities(CapabilityType.BROWSER_NAME, SAFARI_TECH_PREVIEW.browserName()));
assertThat(options.getUseTechnologyPreview()).isTrue();

options = new SafariOptions(
Expand All @@ -79,7 +80,7 @@ public void settingTechnologyPreviewModeAlsoChangesBrowserName() {
assertThat(options.getBrowserName()).isEqualTo(SAFARI.browserName());

options.setUseTechnologyPreview(true);
assertThat(options.getBrowserName()).isEqualTo(SafariOptions.SAFARI_TECH_PREVIEW);
assertThat(options.getBrowserName()).isEqualTo(SAFARI_TECH_PREVIEW.browserName());

options.setUseTechnologyPreview(false);
assertThat(options.getBrowserName()).isEqualTo(SAFARI.browserName());
Expand Down

0 comments on commit 6e7cf5d

Please sign in to comment.