Skip to content

Commit

Permalink
[grid] Adding SafariTechPreviewDriverService
Browse files Browse the repository at this point in the history
This enables users to run Safari Tech Preview in
Grid. It was not possible before because the builder
assigned to the stereotype did not have the right
browser driver binary path configured, and setting
that afterwards was not simple.

Also, a couple of things got deprecated in
SafariDriverService, so we can remove them in the
next release.

Fixes #6791
  • Loading branch information
diemol committed Sep 27, 2021
1 parent 0dcffa2 commit 184ac78
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 18 deletions.
26 changes: 16 additions & 10 deletions java/src/org/openqa/selenium/safari/SafariDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ public class SafariDriverService extends DriverService {
public static final String SAFARI_DRIVER_EXE_PROPERTY = "webdriver.safari.driver";

private static final File SAFARI_DRIVER_EXECUTABLE = new File("/usr/bin/safaridriver");

/**
* @deprecated Directly use {@link SafariTechPreviewDriverService}
* Remove after deprecation policy is fulfilled
*/
@Deprecated
private static final File TP_SAFARI_DRIVER_EXECUTABLE =
new File("/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver");
new File("/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver");

public SafariDriverService(
File executable,
int port,
List<String> args,
Map<String, String> environment) throws IOException {
File executable,
int port,
List<String> args,
Map<String, String> environment) throws IOException {
super(executable, port, DEFAULT_TIMEOUT, args, environment);
}

Expand All @@ -73,10 +79,6 @@ static SafariDriverService createDefaultService(SafariOptions options) {
return new Builder().usingTechnologyPreview(options.getUseTechnologyPreview()).build();
}

static SafariDriverService createDefaultService(Capabilities caps) {
return createDefaultService(new SafariOptions(caps));
}

@Override
protected void waitUntilAvailable() {
try {
Expand All @@ -98,13 +100,17 @@ public int score(Capabilities capabilities) {

if (BrowserType.SAFARI.equals(capabilities.getBrowserName())) {
score++;
} else if ("Safari Technology Preview".equals(capabilities.getBrowserName())) {
} else if (SafariOptions.SAFARI_TECH_PREVIEW.equals(capabilities.getBrowserName())) {
score++;
}

return score;
}

/**
* @deprecated Directly use {@link SafariTechPreviewDriverService}
*/
@Deprecated
public SafariDriverService.Builder usingTechnologyPreview(boolean useTechnologyPreview) {
this.useTechnologyPreview = useTechnologyPreview;
return this;
Expand Down
4 changes: 0 additions & 4 deletions java/src/org/openqa/selenium/safari/SafariOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ public boolean getAutomaticInspection() {
return Boolean.TRUE.equals(getCapability(Option.AUTOMATIC_INSPECTION));
}

// Setters

/**
* Instruct the SafariDriver to enable the Automatic Inspection if true, otherwise disable
* the automatic inspection. Defaults to disabling the automatic inspection.
Expand Down Expand Up @@ -122,8 +120,6 @@ public SafariOptions setAutomaticProfiling(boolean automaticProfiling) {
return this;
}

// Getters

public boolean getUseTechnologyPreview() {
return SAFARI_TECH_PREVIEW.equals(getBrowserName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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 @@ -28,8 +30,6 @@

import java.util.Optional;

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

@AutoService(WebDriverInfo.class)
public class SafariTechPreviewDriverInfo implements WebDriverInfo {

Expand Down Expand Up @@ -63,7 +63,7 @@ public boolean isSupportingCdp() {
@Override
public boolean isAvailable() {
try {
SafariDriverService.createDefaultService(getCanonicalCapabilities());
SafariTechPreviewDriverService.createDefaultService();
return true;
} catch (IllegalStateException | WebDriverException e) {
return false;
Expand All @@ -82,6 +82,6 @@ public Optional<WebDriver> createDriver(Capabilities capabilities)
return Optional.empty();
}

return Optional.of(new SafariDriver(capabilities));
return Optional.of(new SafariDriver(new SafariOptions(capabilities)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// 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.safari;

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

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SafariTechPreviewDriverService extends DriverService {

/**
* System property that defines the location of the tech preview safaridriver executable that
* will be used by the {@link #createDefaultService() default service}.
*/
public static final String TP_SAFARI_DRIVER_EXE_PROPERTY = "webdriver.tp.safari.driver";

private static final File TP_SAFARI_DRIVER_EXECUTABLE =
new File("/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver");

public SafariTechPreviewDriverService(
File executable,
int port,
List<String> args,
Map<String, String> environment) throws IOException {
super(executable, port, DEFAULT_TIMEOUT, args, environment);
}

public SafariTechPreviewDriverService(
File executable,
int port,
Duration timeout,
List<String> args,
Map<String, String> environment) throws IOException {
super(executable, port, timeout, args, environment);
}

public static SafariTechPreviewDriverService createDefaultService() {
return new Builder().build();
}

@Override
protected void waitUntilAvailable() {
try {
PortProber.waitForPortUp(getUrl().getPort(), (int) getTimeout().toMillis(), MILLISECONDS);
} catch (RuntimeException e) {
throw new WebDriverException(e);
}
}

@AutoService(DriverService.Builder.class)
public static class Builder extends DriverService.Builder<
SafariTechPreviewDriverService, SafariTechPreviewDriverService.Builder> {

@Override
public int score(Capabilities capabilities) {
int score = 0;

if (SafariOptions.SAFARI_TECH_PREVIEW.equals(capabilities.getBrowserName())) {
// Returning this value so this service is preferred over SafariDriverService
// Needs to be reestablished to "score++" when the deprecated methods at SafariDriverService
// get removed.
score = 10;
}

return score;
}

@Override
protected File findDefaultExecutable() {
File exe;
if (System.getProperty(TP_SAFARI_DRIVER_EXE_PROPERTY) != null) {
exe = new File(System.getProperty(TP_SAFARI_DRIVER_EXE_PROPERTY));
} else {
exe = TP_SAFARI_DRIVER_EXECUTABLE;
}

if (!exe.isFile()) {
throw new WebDriverException("Unable to find driver executable: " + exe);
}

return exe;
}

@Override
protected List<String> createArgs() {
return Arrays.asList("--port", String.valueOf(getPort()));
}

@Override
protected SafariTechPreviewDriverService createDriverService(
File exe,
int port,
Duration timeout,
List<String> args,
Map<String, String> environment) {
try {
return new SafariTechPreviewDriverService(exe, port, timeout, args, environment);
} catch (IOException e) {
throw new WebDriverException(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.safari;

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.junit.Test;
import org.junit.experimental.categories.Category;
import org.openqa.selenium.testing.UnitTests;

import java.io.File;
import java.time.Duration;

@Category(UnitTests.class)
public class SafariTechPreviewDriverServiceTest {

@Test
public void builderPassesTimeoutToDriverService() {
File exe = new File("someFile");
Duration defaultTimeout = Duration.ofSeconds(20);
Duration customTimeout = Duration.ofSeconds(60);

SafariTechPreviewDriverService.Builder builderMock =
spy(MockSafariTechPreviewDriverServiceBuilder.class);
doReturn(exe).when(builderMock).findDefaultExecutable();
builderMock.build();

verify(builderMock).createDriverService(any(), anyInt(), eq(defaultTimeout), any(), any());

builderMock.withTimeout(customTimeout);
builderMock.build();
verify(builderMock).createDriverService(any(), anyInt(), eq(customTimeout), any(), any());
}

public static class MockSafariTechPreviewDriverServiceBuilder
extends SafariTechPreviewDriverService.Builder {

@Override
public SafariTechPreviewDriverService.Builder usingDriverExecutable(File file) {
return this;
}
}
}

0 comments on commit 184ac78

Please sign in to comment.