or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

console-logging.mddomain-management.mdindex.mdjavascript-integration.mdnetwork-operations.mdruntime-events.mdtarget-management.mdversion-system.md

target-management.mddocs/

0

# Target Management

1

2

Browser target (tab/window) management and session control for Chrome DevTools Protocol v138. Enables working with multiple browser contexts, coordinating cross-target operations, and managing debugging sessions.

3

4

## Capabilities

5

6

### Target Handler

7

8

Main target management handler providing browser context and session control.

9

10

```java { .api }

11

/**

12

* Target/tab management for CDP v138

13

* Default constructor - stateless implementation

14

*/

15

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

16

// No explicit constructor - uses default constructor

17

}

18

```

19

20

**Usage Example:**

21

22

```java

23

import org.openqa.selenium.devtools.v138.v138Target;

24

import org.openqa.selenium.devtools.DevTools;

25

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

26

27

v138Target target = new v138Target();

28

29

// Get list of available targets

30

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

31

targets.forEach(info ->

32

System.out.println("Target: " + info.getTitle() + " - " + info.getUrl()));

33

```

34

35

### Target Discovery

36

37

Discovers and lists all available browser targets (tabs, windows, service workers).

38

39

```java { .api }

40

/**

41

* Get list of all available targets

42

* @return Command that returns list of target information

43

*/

44

@Override

45

public Command<List<TargetInfo>> getTargets();

46

```

47

48

**Usage Example:**

49

50

```java

51

// Discover all browser targets

52

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

53

54

for (TargetInfo info : targets) {

55

System.out.printf("Target ID: %s%n", info.getTargetId());

56

System.out.printf(" Type: %s%n", info.getType());

57

System.out.printf(" Title: %s%n", info.getTitle());

58

System.out.printf(" URL: %s%n", info.getUrl());

59

System.out.printf(" Attached: %s%n", info.getAttached());

60

61

if (info.getOpenerId().isPresent()) {

62

System.out.printf(" Opener: %s%n", info.getOpenerId().get());

63

}

64

65

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

66

}

67

```

68

69

### Target Attachment

70

71

Attaches to specific targets to enable debugging and control.

72

73

```java { .api }

74

/**

75

* Attach to a specific target for debugging

76

* @param targetId ID of the target to attach to

77

* @return Command that returns SessionID for the attached session

78

*/

79

@Override

80

public Command<SessionID> attachToTarget(TargetID targetId);

81

```

82

83

**Usage Example:**

84

85

```java

86

// Find a page target and attach to it

87

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

88

Optional<TargetInfo> pageTarget = targets.stream()

89

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

90

.filter(info -> info.getUrl().contains("example.com"))

91

.findFirst();

92

93

if (pageTarget.isPresent()) {

94

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

95

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

96

97

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

98

99

// Now you can send commands to this specific target

100

// using the session ID for target-specific operations

101

}

102

```

103

104

### Target Detachment

105

106

Detaches from targets to release debugging connections.

107

108

```java { .api }

109

/**

110

* Detach from a target

111

* @param sessionId Optional session ID to detach from

112

* @param targetId Optional target ID to detach from

113

* @return Command to detach from target

114

*/

115

@Override

116

public Command<Void> detachFromTarget(Optional<SessionID> sessionId,

117

Optional<TargetID> targetId);

118

```

119

120

**Usage Example:**

121

122

```java

123

// Detach using session ID

124

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

125

// ... do work with attached target ...

126

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

127

128

// Or detach using target ID

129

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

130

```

131

132

### Auto-Attach Configuration

133

134

Configures automatic attachment to new targets as they are created.

135

136

```java { .api }

137

/**

138

* Set up automatic attachment to new targets

139

* @return Command to enable auto-attach with flattening

140

*/

141

@Override

142

public Command<Void> setAutoAttach();

143

```

144

145

**Usage Example:**

146

147

```java

148

// Enable auto-attachment to new targets

149

devTools.send(target.setAutoAttach());

150

151

// Listen for target detachment events

152

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

153

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

154

});

155

156

// Now when new tabs/windows are created, they will be automatically

157

// attached for debugging without manual intervention

158

driver.executeScript("window.open('https://example.com', '_blank');");

159

```

160

161

### Target Event Monitoring

162

163

Monitors target lifecycle events such as detachment.

164

165

```java { .api }

166

/**

167

* Stream of target detachment events

168

* @return Event stream for when targets are detached

169

*/

170

@Override

171

public Event<TargetID> detached();

172

```

173

174

**Usage Example:**

175

176

```java

177

// Monitor target lifecycle

178

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

179

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

180

181

// Clean up any resources associated with this target

182

cleanupTargetResources(targetId);

183

});

184

185

// Enable auto-attach to monitor all targets

186

devTools.send(target.setAutoAttach());

187

```

188

189

## Complete Target Management Example

190

191

```java

192

import org.openqa.selenium.chrome.ChromeDriver;

193

import org.openqa.selenium.devtools.DevTools;

194

import org.openqa.selenium.devtools.v138.v138Target;

195

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

196

import java.util.*;

197

198

ChromeDriver driver = new ChromeDriver();

199

DevTools devTools = driver.getDevTools();

200

devTools.createSession();

201

202

v138Target target = new v138Target();

203

204

// Set up target monitoring

205

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

206

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

207

});

208

209

// Enable auto-attach for new targets

210

devTools.send(target.setAutoAttach());

211

212

// Initial target discovery

213

System.out.println("=== Initial Targets ===");

214

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

215

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

216

217

for (TargetInfo info : initialTargets) {

218

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

219

info.getType(), info.getTitle(), info.getUrl());

220

221

// Attach to page targets

222

if ("page".equals(info.getType()) && !info.getAttached()) {

223

try {

224

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

225

attachedSessions.put(info.getTargetId(), sessionId);

226

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

227

} catch (Exception e) {

228

System.out.println(" → Failed to attach: " + e.getMessage());

229

}

230

}

231

}

232

233

// Navigate and create new targets

234

System.out.println("\n=== Creating New Targets ===");

235

driver.get("https://example.com");

236

237

// Open new tab - should auto-attach due to setAutoAttach()

238

driver.executeScript("window.open('https://httpbin.org/get', '_blank');");

239

240

// Wait for new target creation

241

Thread.sleep(2000);

242

243

// Check updated target list

244

System.out.println("\n=== Updated Targets ===");

245

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

246

for (TargetInfo info : updatedTargets) {

247

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

248

info.getType(), info.getTitle(), info.getUrl(), info.getAttached());

249

}

250

251

// Demonstrate target-specific operations

252

Optional<TargetInfo> httpbinTarget = updatedTargets.stream()

253

.filter(info -> info.getUrl().contains("httpbin.org"))

254

.findFirst();

255

256

if (httpbinTarget.isPresent()) {

257

TargetID httpbinId = httpbinTarget.get().getTargetId();

258

259

if (!attachedSessions.containsKey(httpbinId)) {

260

// Attach to the new target if not already attached

261

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

262

attachedSessions.put(httpbinId, sessionId);

263

System.out.println("Manually attached to httpbin target: " + sessionId);

264

}

265

}

266

267

// Clean up - detach from all attached targets

268

System.out.println("\n=== Cleanup ===");

269

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

270

try {

271

devTools.send(target.detachFromTarget(

272

Optional.of(entry.getValue()),

273

Optional.of(entry.getKey())));

274

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

275

} catch (Exception e) {

276

System.out.println("Failed to detach from " + entry.getKey() + ": " + e.getMessage());

277

}

278

}

279

280

devTools.close();

281

driver.quit();

282

```

283

284

## Types

285

286

```java { .api }

287

// Target identification and session management

288

class TargetID {

289

String toString(); // Unique target identifier

290

}

291

292

class SessionID {

293

String toString(); // Unique session identifier for attached targets

294

}

295

296

// Target information structure

297

class TargetInfo {

298

TargetID getTargetId(); // Unique target identifier

299

String getType(); // Target type ("page", "background_page", "service_worker", etc.)

300

String getTitle(); // Target title (page title, extension name, etc.)

301

String getUrl(); // Target URL

302

Boolean getAttached(); // Whether target is currently attached for debugging

303

Optional<TargetID> getOpenerId(); // ID of target that opened this target (for popups)

304

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

305

}

306

307

// Browser context identification

308

class BrowserContextID {

309

String toString(); // Browser context identifier

310

}

311

```