or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

events.mdindex.mdjavascript.mdlogging.mdnetwork.mdtarget.md

target.mddocs/

0

# Target Management

1

2

Browser target discovery, attachment, and management for working with browser tabs, workers, and other execution contexts using Chrome DevTools Protocol v110.

3

4

## Capabilities

5

6

### Target Domain Wrapper

7

8

High-level interface for CDP target management providing type-safe access to browser target operations.

9

10

```java { .api }

11

/**

12

* Target domain implementation for CDP v110 target operations

13

*/

14

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

15

// Implementation of idealized target interface

16

}

17

```

18

19

### Target Discovery

20

21

Discover and list all available browser targets including tabs, workers, and other execution contexts.

22

23

```java { .api }

24

/**

25

* Get all available targets

26

* @return Command returning list of target information

27

*/

28

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

29

```

30

31

**Usage Example:**

32

33

```java

34

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

35

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

36

37

v110Target target = new v110Target();

38

39

// Get all available targets

40

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

41

42

for (TargetInfo targetInfo : targets) {

43

System.out.println("Target ID: " + targetInfo.getTargetId());

44

System.out.println("Type: " + targetInfo.getType());

45

System.out.println("Title: " + targetInfo.getTitle());

46

System.out.println("URL: " + targetInfo.getUrl());

47

System.out.println("Attached: " + targetInfo.getAttached());

48

System.out.println("---");

49

}

50

```

51

52

### Target Attachment

53

54

Attach to specific targets to control them independently.

55

56

```java { .api }

57

/**

58

* Attach to specific target

59

* @param targetId Target identifier to attach to

60

* @return Command returning session ID for attached target

61

*/

62

public Command<SessionID> attachToTarget(TargetID targetId);

63

64

/**

65

* Enable automatic attachment to targets

66

* @return Command to set auto-attach behavior

67

*/

68

public Command<Void> setAutoAttach();

69

```

70

71

**Usage Example:**

72

73

```java

74

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

75

76

// Find a specific target (e.g., a new tab)

77

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

78

Optional<TargetInfo> pageTarget = targets.stream()

79

.filter(t -> "page".equals(t.getType()) && t.getUrl().contains("example.com"))

80

.findFirst();

81

82

if (pageTarget.isPresent()) {

83

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

84

85

// Attach to the target

86

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

87

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

88

89

// Now you can send commands to this specific target

90

// using the session ID

91

}

92

```

93

94

### Target Detachment

95

96

Detach from targets when no longer needed.

97

98

```java { .api }

99

/**

100

* Detach from target

101

* @param sessionId Optional session ID to detach from

102

* @param targetId Optional target ID to detach from

103

* @return Command to detach

104

*/

105

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

106

107

/**

108

* Get target detached events

109

* @return Event stream for target detachment

110

*/

111

public Event<TargetID> detached();

112

```

113

114

**Usage Example:**

115

116

```java

117

// Listen for target detachment events

118

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

119

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

120

});

121

122

// Detach from specific target

123

devTools.send(target.detachFromTarget(

124

Optional.of(sessionId),

125

Optional.empty()

126

));

127

128

// Or detach by target ID

129

devTools.send(target.detachFromTarget(

130

Optional.empty(),

131

Optional.of(targetId)

132

));

133

```

134

135

### Auto-Attachment

136

137

Configure automatic attachment to new targets as they are created.

138

139

```java { .api }

140

/**

141

* Enable auto-attach to targets

142

* @return Command to enable automatic target attachment

143

*/

144

public Command<Void> setAutoAttach();

145

```

146

147

**Usage Example:**

148

149

```java

150

import org.openqa.selenium.devtools.v110.target.Target;

151

152

// Enable auto-attach to new targets

153

devTools.send(target.setAutoAttach());

154

155

// Listen for new targets

156

devTools.addListener(Target.targetCreated(), targetInfo -> {

157

System.out.println("New target created: " + targetInfo.getTitle());

158

159

// Automatically attach if it's a page

160

if ("page".equals(targetInfo.getType())) {

161

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

162

System.out.println("Auto-attached with session: " + session);

163

}

164

});

165

```

166

167

## CDP Domain Classes

168

169

### Target Domain

170

171

Direct access to CDP Target domain for low-level target operations.

172

173

```java { .api }

174

import org.openqa.selenium.devtools.v110.target.Target;

175

176

/**

177

* Get available targets

178

*/

179

public static Command<GetTargetsResponse> getTargets(Optional<Boolean> filter);

180

181

/**

182

* Attach to target

183

*/

184

public static Command<AttachToTargetResponse> attachToTarget(

185

org.openqa.selenium.devtools.v110.target.model.TargetID targetId,

186

Optional<Boolean> flatten

187

);

188

189

/**

190

* Detach from target

191

*/

192

public static Command<Void> detachFromTarget(

193

Optional<org.openqa.selenium.devtools.v110.target.model.SessionID> sessionId,

194

Optional<org.openqa.selenium.devtools.v110.target.model.TargetID> targetId

195

);

196

197

/**

198

* Set auto-attach to targets

199

*/

200

public static Command<Void> setAutoAttach(

201

Boolean autoAttach,

202

Boolean waitForDebuggerOnStart,

203

Optional<Boolean> flatten,

204

Optional<List<String>> filter

205

);

206

207

/**

208

* Target created event

209

*/

210

public static Event<TargetCreated> targetCreated();

211

212

/**

213

* Target destroyed event

214

*/

215

public static Event<TargetDestroyed> targetDestroyed();

216

217

/**

218

* Target crashed event

219

*/

220

public static Event<TargetCrashed> targetCrashed();

221

222

/**

223

* Attached to target event

224

*/

225

public static Event<AttachedToTarget> attachedToTarget();

226

227

/**

228

* Detached from target event

229

*/

230

public static Event<DetachedFromTarget> detachedFromTarget();

231

```

232

233

## Model Classes

234

235

### Target Information Models

236

237

```java { .api }

238

import org.openqa.selenium.devtools.v110.target.model.*;

239

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

240

import org.openqa.selenium.devtools.idealized.browser.model.BrowserContextID;

241

242

/**

243

* CDP Target information

244

*/

245

public class org.openqa.selenium.devtools.v110.target.model.TargetInfo {

246

/**

247

* Target identifier

248

*/

249

TargetID getTargetId();

250

251

/**

252

* Target type (page, background_page, service_worker, etc.)

253

*/

254

String getType();

255

256

/**

257

* Target title

258

*/

259

String getTitle();

260

261

/**

262

* Target URL

263

*/

264

String getUrl();

265

266

/**

267

* Whether target is attached

268

*/

269

Boolean getAttached();

270

271

/**

272

* Opener target ID if opened by another target

273

*/

274

Optional<TargetID> getOpenerId();

275

276

/**

277

* Browser context ID

278

*/

279

Optional<BrowserContextID> getBrowserContextId();

280

}

281

282

/**

283

* Idealized target information (high-level)

284

*/

285

public class org.openqa.selenium.devtools.idealized.target.model.TargetInfo {

286

org.openqa.selenium.devtools.idealized.target.model.TargetID getTargetId();

287

String getType();

288

String getTitle();

289

String getUrl();

290

Boolean getAttached();

291

Optional<org.openqa.selenium.devtools.idealized.target.model.TargetID> getOpenerId();

292

Optional<BrowserContextID> getBrowserContextId();

293

}

294

295

/**

296

* Target identifier

297

*/

298

public class org.openqa.selenium.devtools.v110.target.model.TargetID {

299

String toString();

300

}

301

302

/**

303

* Idealized target ID

304

*/

305

public class org.openqa.selenium.devtools.idealized.target.model.TargetID {

306

String toString();

307

}

308

309

/**

310

* Session identifier for attached targets

311

*/

312

public class org.openqa.selenium.devtools.v110.target.model.SessionID {

313

String toString();

314

}

315

316

/**

317

* Idealized session ID

318

*/

319

public class org.openqa.selenium.devtools.idealized.target.model.SessionID {

320

String toString();

321

}

322

```

323

324

### Target Event Models

325

326

```java { .api }

327

/**

328

* Target created event

329

*/

330

public class TargetCreated {

331

TargetInfo getTargetInfo();

332

}

333

334

/**

335

* Target destroyed event

336

*/

337

public class TargetDestroyed {

338

TargetID getTargetId();

339

}

340

341

/**

342

* Target crashed event

343

*/

344

public class TargetCrashed {

345

TargetID getTargetId();

346

String getStatus();

347

Integer getErrorCode();

348

}

349

350

/**

351

* Attached to target event

352

*/

353

public class AttachedToTarget {

354

SessionID getSessionId();

355

TargetInfo getTargetInfo();

356

Boolean getWaitingForDebugger();

357

}

358

359

/**

360

* Detached from target event

361

*/

362

public class DetachedFromTarget {

363

SessionID getSessionId();

364

Optional<TargetID> getTargetId();

365

}

366

```

367

368

### Response Models

369

370

```java { .api }

371

/**

372

* Get targets response

373

*/

374

public class GetTargetsResponse {

375

List<TargetInfo> getTargetInfos();

376

}

377

378

/**

379

* Attach to target response

380

*/

381

public class AttachToTargetResponse {

382

SessionID getSessionId();

383

}

384

```

385

386

## Advanced Usage Examples

387

388

### Multi-Target Coordination

389

390

```java

391

v110Target target = new v110Target();

392

393

// Get all page targets

394

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

395

List<TargetInfo> pageTargets = allTargets.stream()

396

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

397

.collect(Collectors.toList());

398

399

// Attach to multiple targets

400

Map<TargetID, SessionID> attachedTargets = new HashMap<>();

401

402

for (TargetInfo targetInfo : pageTargets) {

403

try {

404

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

405

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

406

System.out.println("Attached to: " + targetInfo.getTitle());

407

} catch (DevToolsException e) {

408

System.err.println("Failed to attach to target: " + e.getMessage());

409

}

410

}

411

412

// Later, detach from all targets

413

for (Map.Entry<TargetID, SessionID> entry : attachedTargets.entrySet()) {

414

devTools.send(target.detachFromTarget(

415

Optional.of(entry.getValue()),

416

Optional.of(entry.getKey())

417

));

418

}

419

```

420

421

### Service Worker Monitoring

422

423

```java

424

// Monitor for service worker targets

425

devTools.addListener(Target.targetCreated(), event -> {

426

TargetInfo targetInfo = event.getTargetInfo();

427

428

if ("service_worker".equals(targetInfo.getType())) {

429

System.out.println("Service worker created: " + targetInfo.getUrl());

430

431

// Attach to service worker for debugging

432

try {

433

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

434

System.out.println("Attached to service worker: " + sessionId);

435

436

// Enable runtime in service worker context

437

// (would need to send commands with session context)

438

} catch (DevToolsException e) {

439

System.err.println("Failed to attach to service worker: " + e.getMessage());

440

}

441

}

442

});

443

444

// Enable auto-attach for new targets

445

devTools.send(target.setAutoAttach());

446

```

447

448

### Tab Management

449

450

```java

451

// Monitor tab creation and destruction

452

devTools.addListener(Target.targetCreated(), event -> {

453

TargetInfo info = event.getTargetInfo();

454

if ("page".equals(info.getType())) {

455

System.out.println("New tab opened: " + info.getTitle());

456

System.out.println("URL: " + info.getUrl());

457

}

458

});

459

460

devTools.addListener(Target.targetDestroyed(), event -> {

461

System.out.println("Tab closed: " + event.getTargetId());

462

});

463

464

// Find and focus specific tab

465

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

466

Optional<TargetInfo> specificTab = targets.stream()

467

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

468

.filter(t -> t.getUrl().contains("important-page.com"))

469

.findFirst();

470

471

if (specificTab.isPresent()) {

472

// Attach to bring into focus (implementation specific)

473

SessionID session = devTools.send(target.attachToTarget(specificTab.get().getTargetId()));

474

System.out.println("Focused tab: " + specificTab.get().getTitle());

475

}

476

```

477

478

## Error Handling

479

480

Target management operations may encounter various error conditions:

481

482

```java

483

import org.openqa.selenium.devtools.DevToolsException;

484

485

// Handle target discovery failures

486

try {

487

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

488

} catch (DevToolsException e) {

489

System.err.println("Failed to get targets: " + e.getMessage());

490

}

491

492

// Handle attachment failures

493

devTools.addListener(Target.targetCreated(), event -> {

494

try {

495

SessionID session = devTools.send(target.attachToTarget(event.getTargetInfo().getTargetId()));

496

System.out.println("Successfully attached: " + session);

497

} catch (DevToolsException e) {

498

System.err.println("Failed to attach to target: " + e.getMessage());

499

// Continue without attachment

500

}

501

});

502

503

// Handle unexpected detachment

504

devTools.addListener(target.detached(), targetId -> {

505

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

506

// Clean up resources associated with this target

507

cleanupTarget(targetId);

508

});

509

```