-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
CdpEventTypes.java
120 lines (98 loc) · 4.12 KB
/
CdpEventTypes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 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.devtools.events;
import com.google.common.io.Resources;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.logging.EventType;
import org.openqa.selenium.logging.HasLogEvents;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.json.Json.MAP_TYPE;
public class CdpEventTypes {
private static final Json JSON = new Json();
private CdpEventTypes() {
// Utility class.
}
public static EventType<ConsoleEvent> consoleEvent(Consumer<ConsoleEvent> handler) {
Require.nonNull("Handler", handler);
return new EventType<ConsoleEvent>() {
public void consume(ConsoleEvent event) {
handler.accept(event);
}
@Override
public void initializeListener(WebDriver webDriver) {
Require.precondition(webDriver instanceof HasDevTools, "Loggable must implement HasDevTools");
DevTools tools = ((HasDevTools) webDriver).getDevTools();
tools.createSessionIfThereIsNotOne(webDriver.getWindowHandle());
tools.getDomains().events().addConsoleListener(handler);
}
};
}
public static EventType<Void> domMutation(Consumer<DomMutationEvent> handler) {
Require.nonNull("Handler", handler);
URL url = CdpEventTypes.class.getResource("/org/openqa/selenium/devtools/mutation-listener.js");
if (url == null) {
throw new IllegalStateException("Unable to find helper script");
}
String script;
try {
script = Resources.toString(url, UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Unable to read helper script");
}
return new EventType<Void>() {
@Override
public void consume(Void event) {
handler.accept(null);
}
@Override
public void initializeListener(WebDriver driver) {
Require.precondition(driver instanceof HasDevTools, "Loggable must implement HasDevTools");
DevTools tools = ((HasDevTools) driver).getDevTools();
tools.createSessionIfThereIsNotOne(driver.getWindowHandle());
tools.getDomains().javascript().pin("__webdriver_attribute", script);
// And add the script to the current page
((JavascriptExecutor) driver).executeScript(script);
tools.getDomains().javascript().addBindingCalledListener(
json -> {
Map<String, Object> values = JSON.toType(json, MAP_TYPE);
String id = (String) values.get("target");
List<WebElement> elements = driver.findElements(By.cssSelector(String.format("*[data-__webdriver_id='%s']", id)));
if (!elements.isEmpty()) {
DomMutationEvent event = new DomMutationEvent(
elements.get(0),
String.valueOf(values.get("name")),
String.valueOf(values.get("value")),
String.valueOf(values.get("oldValue")));
handler.accept(event);
}
});
}
};
}
}