or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

browser-configuration.mdcontext-management.mddriver-management.mdextension-management.mdindex.mdprofile-management.mdscreenshot-capabilities.mdservice-management.md

context-management.mddocs/

0

# Context Management

1

2

Firefox-specific context switching between content and chrome contexts for advanced browser automation scenarios. This feature allows automation of both web content and browser UI elements.

3

4

## Capabilities

5

6

### HasContext Interface

7

8

Interface for managing Firefox command execution context.

9

10

```java { .api }

11

/**

12

* Interface for managing Firefox command context.

13

* Allows switching between content context (web pages) and chrome context (browser UI).

14

*/

15

@Beta

16

public interface HasContext {

17

18

/**

19

* Sets the current command execution context.

20

* @param context FirefoxCommandContext specifying the target context

21

*/

22

void setContext(FirefoxCommandContext context);

23

24

/**

25

* Gets the current command execution context.

26

* @return Current FirefoxCommandContext

27

*/

28

FirefoxCommandContext getContext();

29

}

30

```

31

32

**Usage Examples:**

33

34

```java

35

import org.openqa.selenium.firefox.FirefoxDriver;

36

import org.openqa.selenium.firefox.HasContext;

37

import org.openqa.selenium.firefox.FirefoxCommandContext;

38

39

FirefoxDriver driver = new FirefoxDriver();

40

HasContext contextManager = driver; // FirefoxDriver implements HasContext

41

42

// Get current context

43

FirefoxCommandContext currentContext = contextManager.getContext();

44

System.out.println("Current context: " + currentContext);

45

46

// Switch to chrome context for browser UI automation

47

contextManager.setContext(FirefoxCommandContext.CHROME);

48

49

// Switch back to content context for web page automation

50

contextManager.setContext(FirefoxCommandContext.CONTENT);

51

```

52

53

### FirefoxCommandContext Enum

54

55

Enumeration defining valid Firefox command execution contexts.

56

57

```java { .api }

58

/**

59

* Valid values for Firefox command context.

60

* Determines whether commands target web content or browser chrome.

61

*/

62

public enum FirefoxCommandContext {

63

64

/**

65

* Content context - targets web page content.

66

* Default context for standard WebDriver operations.

67

*/

68

CONTENT("content"),

69

70

/**

71

* Chrome context - targets browser UI elements.

72

* Used for automating Firefox browser interface.

73

*/

74

CHROME("chrome");

75

76

/**

77

* Returns string representation of the context.

78

* @return Context name as string

79

*/

80

public String toString();

81

82

/**

83

* Creates FirefoxCommandContext from string representation.

84

* @param text String representation ("content" or "chrome")

85

* @return Corresponding FirefoxCommandContext enum value

86

* @throws IllegalArgumentException if text is not valid context name

87

*/

88

public static FirefoxCommandContext fromString(String text);

89

}

90

```

91

92

**Usage Examples:**

93

94

```java

95

// Create contexts from strings

96

FirefoxCommandContext contentCtx = FirefoxCommandContext.fromString("content");

97

FirefoxCommandContext chromeCtx = FirefoxCommandContext.fromString("chrome");

98

99

// Convert to string

100

String contextName = FirefoxCommandContext.CHROME.toString(); // "chrome"

101

102

// Use in context switching

103

driver.setContext(FirefoxCommandContext.CONTENT);

104

driver.setContext(FirefoxCommandContext.CHROME);

105

```

106

107

### Content Context Operations

108

109

The content context (default) is used for standard web page automation.

110

111

**Content Context Example:**

112

113

```java

114

import org.openqa.selenium.firefox.FirefoxDriver;

115

import org.openqa.selenium.firefox.FirefoxCommandContext;

116

import org.openqa.selenium.By;

117

import org.openqa.selenium.WebElement;

118

119

FirefoxDriver driver = new FirefoxDriver();

120

121

// Ensure we're in content context (default)

122

driver.setContext(FirefoxCommandContext.CONTENT);

123

124

// Standard web page automation

125

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

126

WebElement element = driver.findElement(By.id("search-box"));

127

element.sendKeys("test query");

128

element.submit();

129

130

// All standard WebDriver operations work in content context

131

String title = driver.getTitle();

132

String currentUrl = driver.getCurrentUrl();

133

```

134

135

### Chrome Context Operations

136

137

The chrome context allows automation of Firefox browser UI elements.

138

139

**Chrome Context Example:**

140

141

```java

142

import org.openqa.selenium.firefox.FirefoxDriver;

143

import org.openqa.selenium.firefox.FirefoxCommandContext;

144

import org.openqa.selenium.By;

145

import org.openqa.selenium.WebElement;

146

import org.openqa.selenium.JavascriptExecutor;

147

148

FirefoxDriver driver = new FirefoxDriver();

149

150

// Switch to chrome context for browser UI automation

151

driver.setContext(FirefoxCommandContext.CHROME);

152

153

// Access browser UI elements using XUL/XPath selectors

154

try {

155

// Example: Access address bar (this is browser-specific and may vary)

156

WebElement urlBar = driver.findElement(By.id("urlbar-input"));

157

158

// Example: Access browser menus or buttons

159

WebElement bookmarkButton = driver.findElement(

160

By.cssSelector("toolbarbutton[data-l10n-id='navbar-bookmarks']")

161

);

162

163

// Execute JavaScript in chrome context (browser context)

164

JavascriptExecutor js = (JavascriptExecutor) driver;

165

Object result = js.executeScript(

166

"return Components.classes['@mozilla.org/preferences-service;1']" +

167

".getService(Components.interfaces.nsIPrefBranch)" +

168

".getBoolPref('browser.tabs.remote.autostart');"

169

);

170

171

System.out.println("Remote tabs enabled: " + result);

172

173

} catch (Exception e) {

174

System.err.println("Chrome context operation failed: " + e.getMessage());

175

} finally {

176

// Always switch back to content context for normal operations

177

driver.setContext(FirefoxCommandContext.CONTENT);

178

}

179

```

180

181

### Context Switching Patterns

182

183

Common patterns for context management in automation scenarios.

184

185

**Safe Context Switching:**

186

187

```java

188

import org.openqa.selenium.firefox.FirefoxDriver;

189

import org.openqa.selenium.firefox.FirefoxCommandContext;

190

191

public class ContextSwitchingHelper {

192

private FirefoxDriver driver;

193

194

public ContextSwitchingHelper(FirefoxDriver driver) {

195

this.driver = driver;

196

}

197

198

/**

199

* Executes code in chrome context, then returns to previous context.

200

*/

201

public void executeInChromeContext(Runnable chromeOperation) {

202

FirefoxCommandContext originalContext = driver.getContext();

203

204

try {

205

driver.setContext(FirefoxCommandContext.CHROME);

206

chromeOperation.run();

207

} finally {

208

driver.setContext(originalContext);

209

}

210

}

211

212

/**

213

* Executes code in content context, then returns to previous context.

214

*/

215

public void executeInContentContext(Runnable contentOperation) {

216

FirefoxCommandContext originalContext = driver.getContext();

217

218

try {

219

driver.setContext(FirefoxCommandContext.CONTENT);

220

contentOperation.run();

221

} finally {

222

driver.setContext(originalContext);

223

}

224

}

225

}

226

227

// Usage

228

FirefoxDriver driver = new FirefoxDriver();

229

ContextSwitchingHelper helper = new ContextSwitchingHelper(driver);

230

231

// Execute browser UI operations safely

232

helper.executeInChromeContext(() -> {

233

// Chrome context operations

234

WebElement menuButton = driver.findElement(By.id("PanelUI-menu-button"));

235

menuButton.click();

236

});

237

238

// Context automatically restored to previous state

239

driver.get("https://example.com"); // Works in content context

240

```

241

242

**Browser Configuration via Chrome Context:**

243

244

```java

245

import org.openqa.selenium.firefox.FirefoxDriver;

246

import org.openqa.selenium.firefox.FirefoxCommandContext;

247

import org.openqa.selenium.JavascriptExecutor;

248

249

FirefoxDriver driver = new FirefoxDriver();

250

JavascriptExecutor js = (JavascriptExecutor) driver;

251

252

// Configure browser settings via chrome context

253

driver.setContext(FirefoxCommandContext.CHROME);

254

255

try {

256

// Set preferences through chrome context

257

js.executeScript(

258

"Components.classes['@mozilla.org/preferences-service;1']" +

259

".getService(Components.interfaces.nsIPrefBranch)" +

260

".setBoolPref('dom.webnotifications.enabled', false);"

261

);

262

263

// Get current preference values

264

Boolean notificationsEnabled = (Boolean) js.executeScript(

265

"return Components.classes['@mozilla.org/preferences-service;1']" +

266

".getService(Components.interfaces.nsIPrefBranch)" +

267

".getBoolPref('dom.webnotifications.enabled');"

268

);

269

270

System.out.println("Notifications enabled: " + notificationsEnabled);

271

272

} finally {

273

// Return to content context for normal web automation

274

driver.setContext(FirefoxCommandContext.CONTENT);

275

}

276

277

// Now use the browser with modified settings

278

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

279

```

280

281

**Extension Management via Chrome Context:**

282

283

```java

284

import org.openqa.selenium.firefox.FirefoxDriver;

285

import org.openqa.selenium.firefox.FirefoxCommandContext;

286

import org.openqa.selenium.JavascriptExecutor;

287

288

FirefoxDriver driver = new FirefoxDriver();

289

290

// Switch to chrome context for extension management

291

driver.setContext(FirefoxCommandContext.CHROME);

292

293

try {

294

JavascriptExecutor js = (JavascriptExecutor) driver;

295

296

// Get list of installed extensions via chrome context

297

Object extensions = js.executeScript(

298

"let extensions = [];" +

299

"for (let addon of AddonManager.getAllAddons()) {" +

300

" extensions.push({" +

301

" id: addon.id," +

302

" name: addon.name," +

303

" enabled: addon.isActive" +

304

" });" +

305

"}" +

306

"return extensions;"

307

);

308

309

System.out.println("Installed extensions: " + extensions);

310

311

} finally {

312

driver.setContext(FirefoxCommandContext.CONTENT);

313

}

314

```

315

316

**Complete Context Management Example:**

317

318

```java

319

import org.openqa.selenium.firefox.FirefoxDriver;

320

import org.openqa.selenium.firefox.FirefoxCommandContext;

321

import org.openqa.selenium.By;

322

import org.openqa.selenium.WebElement;

323

import org.openqa.selenium.JavascriptExecutor;

324

325

public class AdvancedFirefoxAutomation {

326

private FirefoxDriver driver;

327

328

public AdvancedFirefoxAutomation() {

329

this.driver = new FirefoxDriver();

330

}

331

332

public void demonstrateContextSwitching() {

333

// Start in default content context

334

System.out.println("Current context: " + driver.getContext());

335

336

// 1. Perform standard web automation

337

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

338

WebElement searchBox = driver.findElement(By.name("q"));

339

searchBox.sendKeys("test search");

340

341

// 2. Switch to chrome context for browser configuration

342

driver.setContext(FirefoxCommandContext.CHROME);

343

344

JavascriptExecutor js = (JavascriptExecutor) driver;

345

346

// Configure browser via chrome context

347

js.executeScript(

348

"Components.classes['@mozilla.org/preferences-service;1']" +

349

".getService(Components.interfaces.nsIPrefBranch)" +

350

".setIntPref('security.tls.version.max', 4);"

351

);

352

353

// 3. Return to content context

354

driver.setContext(FirefoxCommandContext.CONTENT);

355

356

// Continue with web automation

357

searchBox.submit();

358

359

// Verify we're back in content context

360

String title = driver.getTitle();

361

System.out.println("Page title: " + title);

362

}

363

364

public void cleanup() {

365

if (driver != null) {

366

driver.quit();

367

}

368

}

369

370

public static void main(String[] args) {

371

AdvancedFirefoxAutomation automation = new AdvancedFirefoxAutomation();

372

try {

373

automation.demonstrateContextSwitching();

374

} finally {

375

automation.cleanup();

376

}

377

}

378

}

379

```