or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

events.mdindex.mdjavascript.mdlogging.mdnetwork.mdtargets.md

targets.mddocs/

0

# Target Management

1

2

The v133Target class provides comprehensive browser target management capabilities, enabling developers to manage browser contexts, tabs, debugging sessions, and multi-target debugging scenarios.

3

4

## Capabilities

5

6

### v133Target Class

7

8

Main target management class that implements the idealized Target interface with v133-specific implementations.

9

10

```java { .api }

11

/**

12

* v133-specific target management implementation

13

* Implements org.openqa.selenium.devtools.idealized.target.Target for browser target operations

14

*/

15

public class v133Target implements org.openqa.selenium.devtools.idealized.target.Target {

16

/**

17

* Creates a new v133Target instance

18

* Uses default constructor - no parameters required as operations are static

19

*/

20

}

21

```

22

23

### Target Discovery

24

25

Methods for discovering and listing available browser targets.

26

27

```java { .api }

28

/**

29

* Get list of all available targets (tabs, pages, workers, etc.)

30

* @return Command returning list of TargetInfo objects

31

*/

32

public Command<List<org.openqa.selenium.devtools.idealized.target.model.TargetInfo>> getTargets();

33

```

34

35

**Usage Example:**

36

37

```java

38

import org.openqa.selenium.devtools.v133.v133Target;

39

import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;

40

import java.util.List;

41

42

v133Target target = new v133Target();

43

44

// Get all available targets

45

List<TargetInfo> targets = devTools.send(target.getTargets());

46

47

for (TargetInfo targetInfo : targets) {

48

System.out.printf("Target: %s (%s) - %s%n",

49

targetInfo.getTitle(),

50

targetInfo.getType(),

51

targetInfo.getUrl()

52

);

53

}

54

```

55

56

### Target Attachment

57

58

Methods for attaching to and detaching from specific browser targets.

59

60

```java { .api }

61

/**

62

* Attach to a specific target for debugging

63

* @param targetId ID of the target to attach to

64

* @return Command returning SessionID for the debugging session

65

*/

66

public Command<SessionID> attachToTarget(TargetID targetId);

67

68

/**

69

* Detach from a target and end debugging session

70

* @param sessionId Optional session ID to detach from

71

* @param targetId Optional target ID to detach from

72

* @return Command to detach from target

73

*/

74

public Command<Void> detachFromTarget(Optional<SessionID> sessionId, Optional<TargetID> targetId);

75

```

76

77

**Usage Example:**

78

79

```java

80

import org.openqa.selenium.devtools.idealized.target.model.SessionID;

81

import org.openqa.selenium.devtools.idealized.target.model.TargetID;

82

import java.util.Optional;

83

84

// Attach to first available page target

85

List<TargetInfo> targets = devTools.send(target.getTargets());

86

Optional<TargetInfo> pageTarget = targets.stream()

87

.filter(t -> "page".equals(t.getType()))

88

.findFirst();

89

90

if (pageTarget.isPresent()) {

91

TargetID targetId = pageTarget.get().getTargetId();

92

SessionID sessionId = devTools.send(target.attachToTarget(targetId));

93

94

System.out.println("Attached to target with session: " + sessionId);

95

96

// Later, detach from the target

97

devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.of(targetId)));

98

}

99

```

100

101

### Auto-Attachment

102

103

Configure automatic attachment to new targets as they are created.

104

105

```java { .api }

106

/**

107

* Set automatic attachment to new targets

108

* Automatically attaches to new pages, workers, and other targets

109

* @return Command to enable auto-attachment

110

*/

111

public Command<Void> setAutoAttach();

112

```

113

114

**Usage Example:**

115

116

```java

117

// Enable auto-attachment to new targets

118

devTools.send(target.setAutoAttach());

119

120

// Listen for target detachment events

121

devTools.addListener(target.detached(), (TargetID detachedTargetId) -> {

122

System.out.println("Target detached: " + detachedTargetId);

123

});

124

```

125

126

### Target Events

127

128

Monitor target lifecycle events such as creation and destruction.

129

130

```java { .api }

131

/**

132

* Get the target detached event for monitoring target lifecycle

133

* @return Event for monitoring target detachment

134

*/

135

public Event<TargetID> detached();

136

```

137

138

## Target Data Types

139

140

### TargetInfo

141

142

Information about a browser target including its type, URL, and metadata.

143

144

```java { .api }

145

// From org.openqa.selenium.devtools.idealized.target.model.TargetInfo

146

public class TargetInfo {

147

public TargetInfo(TargetID targetId, String type, String title, String url,

148

Boolean attached, Optional<TargetID> openerId,

149

Optional<BrowserContextID> browserContextId);

150

151

public TargetID getTargetId(); // Unique target identifier

152

public String getType(); // Target type (page, worker, etc.)

153

public String getTitle(); // Target title

154

public String getUrl(); // Target URL

155

public Boolean getAttached(); // Whether target is attached

156

public Optional<TargetID> getOpenerId(); // ID of target that opened this one

157

public Optional<BrowserContextID> getBrowserContextId(); // Browser context ID

158

}

159

```

160

161

### Identifier Types

162

163

Unique identifiers for targets and sessions.

164

165

```java { .api }

166

// From org.openqa.selenium.devtools.idealized.target.model.TargetID

167

public class TargetID {

168

public TargetID(String id);

169

public String toString(); // String representation of target ID

170

}

171

172

// From org.openqa.selenium.devtools.idealized.target.model.SessionID

173

public class SessionID {

174

public SessionID(String id);

175

public String toString(); // String representation of session ID

176

}

177

178

// From org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID

179

public class BrowserContextID {

180

public BrowserContextID(String id);

181

public String toString(); // String representation of context ID

182

}

183

```

184

185

## Advanced Target Management Patterns

186

187

### Multi-Target Debugging

188

189

Manage multiple browser targets simultaneously:

190

191

```java

192

import java.util.HashMap;

193

import java.util.Map;

194

195

public class MultiTargetManager {

196

private final Map<TargetID, SessionID> activeSessions = new HashMap<>();

197

private final v133Target target = new v133Target();

198

199

public void attachToAllPageTargets(DevTools devTools) {

200

List<TargetInfo> targets = devTools.send(target.getTargets());

201

202

targets.stream()

203

.filter(t -> "page".equals(t.getType()))

204

.forEach(targetInfo -> {

205

try {

206

SessionID sessionId = devTools.send(target.attachToTarget(targetInfo.getTargetId()));

207

activeSessions.put(targetInfo.getTargetId(), sessionId);

208

System.out.printf("Attached to page: %s (Session: %s)%n",

209

targetInfo.getTitle(), sessionId);

210

} catch (Exception e) {

211

System.err.printf("Failed to attach to target %s: %s%n",

212

targetInfo.getTargetId(), e.getMessage());

213

}

214

});

215

}

216

217

public void detachFromAllTargets(DevTools devTools) {

218

activeSessions.forEach((targetId, sessionId) -> {

219

try {

220

devTools.send(target.detachFromTarget(Optional.of(sessionId), Optional.of(targetId)));

221

System.out.println("Detached from target: " + targetId);

222

} catch (Exception e) {

223

System.err.printf("Failed to detach from target %s: %s%n", targetId, e.getMessage());

224

}

225

});

226

activeSessions.clear();

227

}

228

229

public Map<TargetID, SessionID> getActiveSessions() {

230

return new HashMap<>(activeSessions);

231

}

232

}

233

```

234

235

### Target Filtering and Selection

236

237

Filter targets by type and characteristics:

238

239

```java

240

import java.util.function.Predicate;

241

242

public class TargetSelector {

243

private final v133Target target = new v133Target();

244

245

public List<TargetInfo> findTargets(DevTools devTools, Predicate<TargetInfo> filter) {

246

List<TargetInfo> allTargets = devTools.send(target.getTargets());

247

return allTargets.stream()

248

.filter(filter)

249

.toList();

250

}

251

252

public Optional<TargetInfo> findMainPage(DevTools devTools) {

253

return findTargets(devTools, t ->

254

"page".equals(t.getType()) &&

255

!t.getUrl().startsWith("chrome-") &&

256

!t.getUrl().equals("about:blank")

257

).stream().findFirst();

258

}

259

260

public List<TargetInfo> findWorkers(DevTools devTools) {

261

return findTargets(devTools, t ->

262

"worker".equals(t.getType()) ||

263

"service_worker".equals(t.getType()) ||

264

"shared_worker".equals(t.getType())

265

);

266

}

267

268

public List<TargetInfo> findBackgroundPages(DevTools devTools) {

269

return findTargets(devTools, t ->

270

"background_page".equals(t.getType())

271

);

272

}

273

}

274

```

275

276

### Target Lifecycle Monitoring

277

278

Monitor target creation and destruction:

279

280

```java

281

public class TargetLifecycleMonitor {

282

private final v133Target target = new v133Target();

283

private final Set<TargetID> knownTargets = ConcurrentHashMap.newKeySet();

284

285

public void startMonitoring(DevTools devTools) {

286

// Enable auto-attachment to detect new targets

287

devTools.send(target.setAutoAttach());

288

289

// Listen for target detachment

290

devTools.addListener(target.detached(), this::handleTargetDetached);

291

292

// Periodically check for new targets

293

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

294

scheduler.scheduleAtFixedRate(() -> checkForNewTargets(devTools), 0, 5, TimeUnit.SECONDS);

295

}

296

297

private void checkForNewTargets(DevTools devTools) {

298

try {

299

List<TargetInfo> currentTargets = devTools.send(target.getTargets());

300

301

for (TargetInfo targetInfo : currentTargets) {

302

if (knownTargets.add(targetInfo.getTargetId())) {

303

handleNewTarget(targetInfo);

304

}

305

}

306

} catch (Exception e) {

307

System.err.println("Error checking for new targets: " + e.getMessage());

308

}

309

}

310

311

private void handleNewTarget(TargetInfo targetInfo) {

312

System.out.printf("πŸ†• New target detected: %s (%s) - %s%n",

313

targetInfo.getTitle(),

314

targetInfo.getType(),

315

targetInfo.getUrl()

316

);

317

}

318

319

private void handleTargetDetached(TargetID targetId) {

320

knownTargets.remove(targetId);

321

System.out.println("❌ Target detached: " + targetId);

322

}

323

}

324

```

325

326

## Target Types

327

328

Common target types you may encounter:

329

330

| Type | Description |

331

|------|-------------|

332

| `page` | Regular web pages and tabs |

333

| `background_page` | Extension background pages |

334

| `worker` | Web workers |

335

| `service_worker` | Service workers |

336

| `shared_worker` | Shared workers |

337

| `browser` | Browser process itself |

338

| `other` | Other target types |

339

340

## Integration with DevTools Sessions

341

342

Target management integrates with DevTools sessions:

343

344

```java

345

// Create session for specific target

346

SessionID session = devTools.send(target.attachToTarget(targetId));

347

348

// Use session for target-specific operations

349

// Note: Session management varies by DevTools implementation

350

// Some operations may require session context switching

351

352

// Detach when done

353

devTools.send(target.detachFromTarget(Optional.of(session), Optional.of(targetId)));

354

```

355

356

## Error Handling

357

358

Target management includes error handling for:

359

360

- **Attachment Failures**: When targets cannot be attached (already attached, invalid ID, etc.)

361

- **Session Management**: When debugging sessions become invalid or disconnected

362

- **Target Lifecycle**: When targets are destroyed unexpectedly

363

- **Permission Issues**: When browser security policies prevent target access

364

- **Resource Limits**: When maximum number of debugging sessions is exceeded