0
# JavaScript Execution
1
2
The v129Javascript class provides comprehensive JavaScript execution and binding capabilities through the Chrome DevTools Protocol. It enables script injection, JavaScript binding management, and code execution in browser contexts.
3
4
## Capabilities
5
6
### JavaScript Domain
7
8
Core JavaScript domain for script execution and binding management.
9
10
```java { .api }
11
/**
12
* JavaScript domain for script execution and binding management
13
* @param devtools - DevTools instance for protocol communication
14
*/
15
public class v129Javascript extends Javascript<ScriptIdentifier, BindingCalled> {
16
public v129Javascript(DevTools devtools);
17
}
18
```
19
20
### Runtime Domain Control
21
22
Control the Runtime domain for JavaScript execution capabilities.
23
24
```java { .api }
25
/**
26
* Enables the Runtime domain for JavaScript execution
27
* @return Command to enable Runtime domain
28
*/
29
protected Command<Void> enableRuntime();
30
31
/**
32
* Disables the Runtime domain
33
* @return Command to disable Runtime domain
34
*/
35
protected Command<Void> disableRuntime();
36
```
37
38
### JavaScript Binding Management
39
40
Manage JavaScript bindings between browser and Java code.
41
42
```java { .api }
43
/**
44
* Adds a JavaScript binding with the specified name
45
* @param scriptName - Name of the binding to add
46
* @return Command to add JavaScript binding
47
*/
48
protected Command<Void> doAddJsBinding(String scriptName);
49
50
/**
51
* Removes a JavaScript binding with the specified name
52
* @param scriptName - Name of the binding to remove
53
* @return Command to remove JavaScript binding
54
*/
55
protected Command<Void> doRemoveJsBinding(String scriptName);
56
57
/**
58
* Event fired when a JavaScript binding is called
59
* @return Event for binding calls
60
*/
61
protected Event<BindingCalled> bindingCalledEvent();
62
63
/**
64
* Extracts payload from binding called event
65
* @param event - BindingCalled event
66
* @return Payload string from the binding call
67
*/
68
protected String extractPayload(BindingCalled event);
69
```
70
71
### Page Domain Control
72
73
Control the Page domain for document-level script management.
74
75
```java { .api }
76
/**
77
* Enables the Page domain for document script management
78
* @return Command to enable Page domain
79
*/
80
protected Command<Void> enablePage();
81
82
/**
83
* Disables the Page domain
84
* @return Command to disable Page domain
85
*/
86
protected Command<Void> disablePage();
87
```
88
89
### Script Injection Management
90
91
Manage scripts that execute automatically on new documents.
92
93
```java { .api }
94
/**
95
* Adds script to evaluate on every new document
96
* @param script - JavaScript code to execute on new documents
97
* @return Command returning ScriptIdentifier for the added script
98
*/
99
protected Command<ScriptIdentifier> addScriptToEvaluateOnNewDocument(String script);
100
101
/**
102
* Removes script from evaluation on new documents
103
* @param id - ScriptIdentifier of the script to remove
104
* @return Command to remove the script
105
*/
106
protected Command<Void> removeScriptToEvaluateOnNewDocument(ScriptIdentifier id);
107
```
108
109
**Usage Examples:**
110
111
```java
112
import org.openqa.selenium.devtools.v129.v129Javascript;
113
import org.openqa.selenium.devtools.DevTools;
114
import org.openqa.selenium.devtools.v129.page.model.ScriptIdentifier;
115
116
// Create JavaScript domain
117
DevTools devTools = driver.getDevTools();
118
v129Javascript javascript = new v129Javascript(devTools);
119
120
// Enable domains
121
devTools.send(javascript.enableRuntime());
122
devTools.send(javascript.enablePage());
123
124
// Add JavaScript binding
125
devTools.send(javascript.doAddJsBinding("myBinding"));
126
127
// Listen for binding calls
128
devTools.addListener(javascript.bindingCalledEvent(), bindingEvent -> {
129
String payload = javascript.extractPayload(bindingEvent);
130
System.out.println("Binding called with payload: " + payload);
131
132
// Process the payload and potentially respond
133
if (payload.contains("getData")) {
134
// Handle data request from JavaScript
135
}
136
});
137
138
// Add script to execute on new documents
139
String initScript = """
140
window.myBinding = function(data) {
141
// Call Java binding
142
window.binding('myBinding', JSON.stringify(data));
143
};
144
145
console.log('Initialization script loaded');
146
""";
147
148
ScriptIdentifier scriptId = devTools.send(
149
javascript.addScriptToEvaluateOnNewDocument(initScript)
150
);
151
152
// Navigate to trigger script execution
153
driver.get("https://example.com");
154
155
// Execute JavaScript that uses the binding
156
driver.executeScript("""
157
window.myBinding({
158
action: 'getData',
159
timestamp: Date.now()
160
});
161
""");
162
```
163
164
### Advanced JavaScript Patterns
165
166
```java
167
// Dynamic script injection based on page conditions
168
String conditionalScript = """
169
if (window.location.hostname === 'specific-site.com') {
170
// Site-specific initialization
171
window.customAPI = {
172
sendData: function(data) {
173
window.binding('dataBinding', JSON.stringify(data));
174
}
175
};
176
}
177
""";
178
179
devTools.send(javascript.addScriptToEvaluateOnNewDocument(conditionalScript));
180
181
// Multiple bindings for different purposes
182
devTools.send(javascript.doAddJsBinding("dataBinding"));
183
devTools.send(javascript.doAddJsBinding("errorBinding"));
184
devTools.send(javascript.doAddJsBinding("logBinding"));
185
186
// Handle different binding types
187
devTools.addListener(javascript.bindingCalledEvent(), bindingEvent -> {
188
String payload = javascript.extractPayload(bindingEvent);
189
190
try {
191
JsonObject data = JsonParser.parseString(payload).getAsJsonObject();
192
String bindingType = data.get("binding").getAsString();
193
194
switch (bindingType) {
195
case "dataBinding":
196
handleDataBinding(data);
197
break;
198
case "errorBinding":
199
handleErrorBinding(data);
200
break;
201
case "logBinding":
202
handleLogBinding(data);
203
break;
204
}
205
} catch (Exception e) {
206
System.err.println("Error processing binding: " + e.getMessage());
207
}
208
});
209
210
// Cleanup scripts when done
211
devTools.send(javascript.removeScriptToEvaluateOnNewDocument(scriptId));
212
devTools.send(javascript.doRemoveJsBinding("myBinding"));
213
```
214
215
## Types
216
217
### JavaScript Event Types
218
219
```java { .api }
220
// From v129.page.model package
221
import org.openqa.selenium.devtools.v129.page.model.ScriptIdentifier;
222
223
// From v129.runtime.model package
224
import org.openqa.selenium.devtools.v129.runtime.model.BindingCalled;
225
```
226
227
### Script Identifier
228
229
```java { .api }
230
// ScriptIdentifier for managing injected scripts
231
class ScriptIdentifier {
232
public ScriptIdentifier(String value);
233
public String getValue();
234
public String toString();
235
}
236
```
237
238
### Binding Event Types
239
240
```java { .api }
241
// BindingCalled event structure
242
class BindingCalled {
243
public String getName(); // Binding name
244
public String getPayload(); // Data payload
245
public Optional<Integer> getExecutionContextId();
246
}
247
```
248
249
### JavaScript Execution Context
250
251
```java { .api }
252
// Base JavaScript interface from idealized package
253
import org.openqa.selenium.devtools.idealized.Javascript;
254
255
// Command and Event base types
256
import org.openqa.selenium.devtools.Command;
257
import org.openqa.selenium.devtools.Event;
258
```
259
260
### Integration Types
261
262
```java { .api }
263
// For advanced JavaScript integration
264
import org.openqa.selenium.devtools.v129.runtime.Runtime;
265
import org.openqa.selenium.devtools.v129.page.Page;
266
267
// Optional parameters for enhanced functionality
268
import java.util.Optional;
269
```