0
# Target Domain
1
2
Target domain implementation for browser target and session management. Enables control over browser tabs, windows, debugging sessions, and provides capabilities for multi-target debugging scenarios.
3
4
## Capabilities
5
6
### Target Attachment and Detachment
7
8
Attach to and detach from browser targets for debugging control.
9
10
```java { .api }
11
public Command<SessionID> attachToTarget(TargetID targetId);
12
public Command<Void> detachFromTarget(Optional<SessionID> sessionId, Optional<TargetID> targetId);
13
public Event<TargetID> detached();
14
```
15
16
Usage example:
17
```java
18
v111Target target = domains.target();
19
20
// Get available targets
21
List<TargetInfo> targets = devTools.send(target.getTargets());
22
23
// Find a specific target (e.g., a specific tab)
24
Optional<TargetInfo> pageTarget = targets.stream()
25
.filter(t -> "page".equals(t.getType()) && t.getUrl().contains("example.com"))
26
.findFirst();
27
28
if (pageTarget.isPresent()) {
29
TargetID targetId = pageTarget.get().getTargetId();
30
31
// Attach to the target for debugging
32
SessionID sessionId = devTools.send(target.attachToTarget(targetId));
33
System.out.println("Attached to target with session: " + sessionId);
34
35
// Later detach when done
36
devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.of(targetId)));
37
}
38
```
39
40
### Target Discovery
41
42
Discover and list all available browser targets.
43
44
```java { .api }
45
public Command<List<org.openqa.selenium.devtools.idealized.target.model.TargetInfo>> getTargets();
46
```
47
48
Usage example:
49
```java
50
// Get all available targets
51
List<TargetInfo> targets = devTools.send(target.getTargets());
52
53
for (TargetInfo targetInfo : targets) {
54
System.out.println("Target ID: " + targetInfo.getTargetId());
55
System.out.println("Type: " + targetInfo.getType());
56
System.out.println("Title: " + targetInfo.getTitle());
57
System.out.println("URL: " + targetInfo.getUrl());
58
System.out.println("Attached: " + targetInfo.getAttached());
59
60
// Check if target has opener (popup/new window relationship)
61
targetInfo.getOpenerId().ifPresent(openerId ->
62
System.out.println("Opened by: " + openerId));
63
64
// Check browser context
65
targetInfo.getBrowserContextId().ifPresent(contextId ->
66
System.out.println("Browser context: " + contextId));
67
68
System.out.println("---");
69
}
70
```
71
72
### Auto-Attach Configuration
73
74
Configure automatic attachment to new targets.
75
76
```java { .api }
77
public Command<Void> setAutoAttach();
78
```
79
80
Usage example:
81
```java
82
// Enable auto-attach to new targets
83
devTools.send(target.setAutoAttach());
84
85
// Listen for target detachment events
86
devTools.addListener(target.detached(), detachedTargetId -> {
87
System.out.println("Target detached: " + detachedTargetId);
88
// Clean up resources for this target
89
});
90
```
91
92
## Multi-Target Debugging Example
93
94
Complete example showing how to manage multiple browser targets:
95
96
```java
97
public class MultiTargetManager {
98
private final DevTools devTools;
99
private final v111Target target;
100
private final Map<TargetID, SessionID> activeSessions = new HashMap<>();
101
private final Map<TargetID, TargetInfo> knownTargets = new HashMap<>();
102
103
public void initializeTargetManagement() {
104
// Enable auto-attach for new targets
105
devTools.send(target.setAutoAttach());
106
107
// Listen for target detachment
108
devTools.addListener(target.detached(), this::handleTargetDetached);
109
110
// Discover existing targets
111
refreshTargetList();
112
}
113
114
public void refreshTargetList() {
115
List<TargetInfo> targets = devTools.send(target.getTargets());
116
117
for (TargetInfo targetInfo : targets) {
118
knownTargets.put(targetInfo.getTargetId(), targetInfo);
119
120
// Auto-attach to page targets
121
if ("page".equals(targetInfo.getType()) && !targetInfo.getAttached()) {
122
attachToTarget(targetInfo.getTargetId());
123
}
124
}
125
}
126
127
public void attachToTarget(TargetID targetId) {
128
try {
129
SessionID sessionId = devTools.send(target.attachToTarget(targetId));
130
activeSessions.put(targetId, sessionId);
131
132
TargetInfo targetInfo = knownTargets.get(targetId);
133
System.out.println("Attached to target: " + targetInfo.getTitle());
134
135
// Configure attached target (enable domains, set up listeners, etc.)
136
configureAttachedTarget(targetId, sessionId);
137
138
} catch (Exception e) {
139
System.err.println("Failed to attach to target " + targetId + ": " + e.getMessage());
140
}
141
}
142
143
private void configureAttachedTarget(TargetID targetId, SessionID sessionId) {
144
// Configure domains for this specific target
145
// Note: You would typically create separate domain instances per target
146
147
TargetInfo targetInfo = knownTargets.get(targetId);
148
System.out.println("Configuring target: " + targetInfo.getUrl());
149
150
// Example: Enable network monitoring for this target
151
// devTools.send(sessionId, networkDomain.enableFetchForAllPatterns());
152
}
153
154
private void handleTargetDetached(TargetID targetId) {
155
SessionID sessionId = activeSessions.remove(targetId);
156
TargetInfo targetInfo = knownTargets.remove(targetId);
157
158
if (targetInfo != null) {
159
System.out.println("Target detached: " + targetInfo.getTitle());
160
}
161
162
// Clean up resources for this target
163
cleanupTargetResources(targetId, sessionId);
164
}
165
166
public void detachFromTarget(TargetID targetId) {
167
SessionID sessionId = activeSessions.get(targetId);
168
if (sessionId != null) {
169
devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.of(targetId)));
170
}
171
}
172
173
public void detachFromAllTargets() {
174
for (Map.Entry<TargetID, SessionID> entry : activeSessions.entrySet()) {
175
devTools.send(target.detachFromTarget(
176
Optional.of(entry.getValue()),
177
Optional.of(entry.getKey())
178
));
179
}
180
activeSessions.clear();
181
}
182
183
public List<TargetInfo> getPageTargets() {
184
return knownTargets.values().stream()
185
.filter(t -> "page".equals(t.getType()))
186
.collect(Collectors.toList());
187
}
188
189
public List<TargetInfo> getWorkerTargets() {
190
return knownTargets.values().stream()
191
.filter(t -> t.getType().contains("worker"))
192
.collect(Collectors.toList());
193
}
194
}
195
```
196
197
## Types
198
199
### Target Information
200
201
```java { .api }
202
class TargetInfo {
203
public TargetInfo(TargetID targetId, String type, String title, String url,
204
boolean attached, Optional<TargetID> openerId,
205
Optional<BrowserContextID> browserContextId);
206
207
public TargetID getTargetId(); // Unique target identifier
208
public String getType(); // Target type: "page", "background_page", "service_worker", etc.
209
public String getTitle(); // Target title (page title for pages)
210
public String getUrl(); // Target URL
211
public boolean getAttached(); // Whether target is currently attached for debugging
212
public Optional<TargetID> getOpenerId(); // ID of target that opened this target (for popups)
213
public Optional<BrowserContextID> getBrowserContextId(); // Browser context (for incognito/profile separation)
214
}
215
```
216
217
### Session and Context Types
218
219
```java { .api }
220
class TargetID {
221
public TargetID(String id);
222
public String toString(); // Get the string representation of the target ID
223
}
224
225
class SessionID {
226
public SessionID(String id);
227
public String toString(); // Get the string representation of the session ID
228
}
229
230
class BrowserContextID {
231
public BrowserContextID(String id);
232
public String toString(); // Get the string representation of the browser context ID
233
}
234
```
235
236
## Target Types
237
238
Common target types you'll encounter:
239
240
- **page**: Regular web pages and tabs
241
- **background_page**: Extension background pages
242
- **service_worker**: Service worker instances
243
- **shared_worker**: Shared worker instances
244
- **browser**: Browser-level target
245
- **other**: Other specialized targets
246
247
## Target Relationships
248
249
Targets can have relationships through the `openerId` field:
250
251
```java
252
// Find all targets opened by a specific target
253
TargetID parentId = new TargetID("parent-target-id");
254
List<TargetInfo> childTargets = targets.stream()
255
.filter(t -> t.getOpenerId().map(id -> id.equals(parentId)).orElse(false))
256
.collect(Collectors.toList());
257
258
// Find the parent of a target
259
Optional<TargetInfo> parent = targets.stream()
260
.filter(t -> childTarget.getOpenerId().map(id -> id.equals(t.getTargetId())).orElse(false))
261
.findFirst();
262
```
263
264
## Browser Context Management
265
266
Browser contexts provide isolation similar to incognito windows:
267
268
```java
269
// Group targets by browser context
270
Map<BrowserContextID, List<TargetInfo>> targetsByContext = targets.stream()
271
.filter(t -> t.getBrowserContextId().isPresent())
272
.collect(Collectors.groupingBy(t -> t.getBrowserContextId().get()));
273
274
// Find targets in default context (no browser context ID)
275
List<TargetInfo> defaultContextTargets = targets.stream()
276
.filter(t -> t.getBrowserContextId().isEmpty())
277
.collect(Collectors.toList());
278
```