Skip to content

Commit

Permalink
Implement Safari attach debugger endpoint and make accessible to Remo…
Browse files Browse the repository at this point in the history
…teWebDriver via Augmenter
  • Loading branch information
titusfortner committed Sep 22, 2021
1 parent 73cbd4b commit 89d4403
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
65 changes: 65 additions & 0 deletions java/src/org/openqa/selenium/safari/AddHasDebugger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.AdditionalHttpCommands;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.ExecuteMethod;
import org.openqa.selenium.remote.http.HttpMethod;

import java.util.Map;
import java.util.function.Predicate;

import static java.util.Collections.singletonMap;

@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasDebugger implements AugmenterProvider<HasDebugger>, AdditionalHttpCommands {

public static final String ATTACH_DEBUGGER = "attachDebugger";

private static final Map<String, CommandInfo> COMMANDS = ImmutableMap.of(
ATTACH_DEBUGGER, new CommandInfo("/session/:sessionId/apple/attach_debugger", HttpMethod.POST));

@Override
public Map<String, CommandInfo> getAdditionalCommands() {
return COMMANDS;
}

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> "Safari".equals(caps.getBrowserName());
}

@Override
public Class<HasDebugger> getDescribedInterface() {
return HasDebugger.class;
}

@Override
public HasDebugger getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
return new HasDebugger() {
@Override public void attachDebugger() {
executeMethod.execute(ATTACH_DEBUGGER, singletonMap(ATTACH_DEBUGGER, null));
}
};
}
}
31 changes: 31 additions & 0 deletions java/src/org/openqa/selenium/safari/HasDebugger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 org.openqa.selenium.Beta;

@Beta
public interface HasDebugger {

/**
* This opens Safari's Web Inspector
* If driver subsequently executes script of "debugger;" the execution will pause, no additional commands will be processed,
* and the code will time out.
*/
void attachDebugger();
}
8 changes: 7 additions & 1 deletion java/src/org/openqa/selenium/safari/SafariDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
*
* This driver can be configured using the {@link SafariOptions} class.
*/
public class SafariDriver extends RemoteWebDriver implements HasPermissions{
public class SafariDriver extends RemoteWebDriver implements HasPermissions, HasDebugger {

private final HasPermissions permissions;
private final HasDebugger debugger;

/**
* Initializes a new SafariDriver} class with default {@link SafariOptions}.
Expand Down Expand Up @@ -84,6 +85,7 @@ public SafariDriver(SafariDriverService safariService) {
public SafariDriver(SafariDriverService safariServer, SafariOptions safariOptions) {
super(new SafariDriverCommandExecutor(safariServer), safariOptions);
permissions = new AddHasPermissions().getImplementation(getCapabilities(), getExecuteMethod());
debugger = new AddHasDebugger().getImplementation(getCapabilities(), getExecuteMethod());
}

@Override
Expand All @@ -96,6 +98,10 @@ public Map<String, Boolean> getPermissions() {
return permissions.getPermissions();
}

@Override public void attachDebugger() {
debugger.attachDebugger();
}

private static class SafariDriverCommandExecutor extends DriverCommandExecutor {
public SafariDriverCommandExecutor(DriverService service) {
super(service, getExtraCommands());
Expand Down
31 changes: 31 additions & 0 deletions java/test/org/openqa/selenium/safari/SafariDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.Assume.assumeTrue;

public class SafariDriverTest extends JUnit4TestBase {
Expand Down Expand Up @@ -107,4 +109,33 @@ public void shouldAllowRemoteWebDriverToAugmentHasPermissions() throws Malformed
driver.quit();
}
}

@Test
public void canAttachDebugger() {
removeDriver();

SafariOptions options = new SafariOptions();
options.setScriptTimeout(Duration.ofSeconds(4));
SafariDriver driver = new SafariDriver(options);

driver.attachDebugger();

driver.quit();
}

@Test
public void shouldAllowRemoteWebDriverToAugmentHasDebugger() throws MalformedURLException {
removeDriver();

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/"), new SafariOptions());
WebDriver augmentedDriver = new Augmenter().augment(driver);

try {
((HasDebugger) augmentedDriver).attachDebugger();
} catch (ClassCastException e) {
fail("Expected augmented driver to be able to cast to HasDebugger");
} finally {
driver.quit();
}
}
}

0 comments on commit 89d4403

Please sign in to comment.