-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[grid] Adding SafariTechPreviewDriverService
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
Showing
5 changed files
with
213 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
java/test/org/openqa/selenium/safari/SafariTechPreviewDriverServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |