Skip to content

Commit cbd263f

Browse files
committed
[java][cdp] Simplify Augmentation for Dom Mutation
1 parent e3e77e7 commit cbd263f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote;
19+
20+
import static org.openqa.selenium.remote.Browser.CHROME;
21+
import static org.openqa.selenium.remote.Browser.EDGE;
22+
import static org.openqa.selenium.remote.Browser.OPERA;
23+
24+
import org.openqa.selenium.Capabilities;
25+
import org.openqa.selenium.WebDriver;
26+
import org.openqa.selenium.devtools.HasDevTools;
27+
import org.openqa.selenium.logging.EventType;
28+
import org.openqa.selenium.logging.HasLogEvents;
29+
30+
import java.util.function.Predicate;
31+
32+
public class AddHasLogEvents
33+
implements AugmenterProvider<HasLogEvents> {
34+
35+
private static final Predicate<String> IS_CHROMIUM_BROWSER = name ->
36+
CHROME.is(name) ||
37+
EDGE.is(name) ||
38+
OPERA.is(name);
39+
40+
@Override
41+
public Predicate<Capabilities> isApplicable() {
42+
return caps -> IS_CHROMIUM_BROWSER.test(caps.getBrowserName());
43+
}
44+
45+
@Override
46+
public Class<HasLogEvents> getDescribedInterface() {
47+
return HasLogEvents.class;
48+
}
49+
50+
@Override
51+
public HasLogEvents getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
52+
return new HasLogEvents() {
53+
@Override
54+
public <X> void onLogEvent(EventType<X> kind) {
55+
if (((RemoteExecuteMethod) executeMethod).getWrappedDriver() instanceof HasDevTools) {
56+
WebDriver driver = ((RemoteExecuteMethod) executeMethod).getWrappedDriver();
57+
kind.initializeListener(driver);
58+
}
59+
}
60+
};
61+
}
62+
}

java/src/org/openqa/selenium/remote/Augmenter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openqa.selenium.WebDriverException;
3434
import org.openqa.selenium.WrapsDriver;
3535
import org.openqa.selenium.internal.Require;
36+
import org.openqa.selenium.logging.HasLogEvents;
3637
import org.openqa.selenium.remote.html5.AddWebStorage;
3738

3839
import java.lang.reflect.Field;
@@ -219,6 +220,10 @@ private WebDriver addDependentAugmentations(WebDriver driver) {
219220
augmentationList.add(createAugmentation(new AddHasAuthentication()));
220221
}
221222

223+
if (!(driver instanceof HasLogEvents)) {
224+
augmentationList.add(createAugmentation(new AddHasLogEvents()));
225+
}
226+
222227
if (!augmentationList.isEmpty()) {
223228
Capabilities caps = ImmutableCapabilities.copyOf(((HasCapabilities) driver).getCapabilities());
224229

0 commit comments

Comments
 (0)