or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

application-launching.mdbrowser-configuration.mddevtools-protocol.mdindex.mdmedia-casting.mdnetwork-conditions.mdpermissions-management.mdservice-management.mdwebdriver-operations.md

webdriver-operations.mddocs/

0

# WebDriver Operations

1

2

The ChromiumDriver extends standard WebDriver functionality with Chromium-specific features including script pinning, authentication handling, and enhanced logging capabilities.

3

4

## Core WebDriver Methods

5

6

### Get Capabilities

7

8

Retrieve the current browser capabilities with Chromium-specific enhancements.

9

10

```java { .api }

11

public Capabilities getCapabilities();

12

```

13

14

**Returns:** Capabilities - Browser capabilities including CDP and BiDi endpoints when available

15

16

**Usage Example:**

17

```java

18

Capabilities caps = driver.getCapabilities();

19

String browserVersion = caps.getBrowserVersion();

20

String cdpEndpoint = (String) caps.getCapability("se:cdp");

21

```

22

23

## Script Management

24

25

### Script Pinning

26

27

Pin JavaScript scripts for reuse across page navigations and refreshes.

28

29

```java { .api }

30

public ScriptKey pin(String script);

31

```

32

33

**Parameters:**

34

- `script` (String): JavaScript code to pin for reuse

35

36

**Returns:** ScriptKey - Reference to the pinned script

37

38

**Usage Example:**

39

```java

40

ChromiumDriver driver = new ChromiumDriver(commandExecutor, capabilities, "chrome");

41

42

// Pin a script for reuse

43

ScriptKey scriptKey = driver.pin("return document.title;");

44

45

// Execute the pinned script

46

String title = (String) driver.executeScript(scriptKey);

47

```

48

49

### Get Pinned Scripts

50

51

Retrieve all currently pinned scripts.

52

53

```java { .api }

54

public Set<ScriptKey> getPinnedScripts();

55

```

56

57

**Returns:** Set<ScriptKey> - Collection of all pinned script references

58

59

### Unpin Script

60

61

Remove a pinned script from memory.

62

63

```java { .api }

64

public void unpin(ScriptKey key);

65

```

66

67

**Parameters:**

68

- `key` (ScriptKey): Reference to the script to unpin

69

70

**Throws:** JavascriptException if the script key is not found

71

72

### Execute Pinned Script

73

74

Execute a previously pinned script with optional arguments.

75

76

```java { .api }

77

public Object executeScript(ScriptKey key, Object... args);

78

```

79

80

**Parameters:**

81

- `key` (ScriptKey): Reference to the pinned script

82

- `args` (Object...): Arguments to pass to the script

83

84

**Returns:** Object - Result of script execution

85

86

**Throws:** JavascriptException if the script key is not found

87

88

## Authentication

89

90

### Register Authentication Handler

91

92

Register an authentication handler for specific URL patterns.

93

94

```java { .api }

95

public void register(Predicate<URI> whenThisMatches, Supplier<Credentials> useTheseCredentials);

96

```

97

98

**Parameters:**

99

- `whenThisMatches` (Predicate<URI>): Predicate to determine when to apply authentication

100

- `useTheseCredentials` (Supplier<Credentials>): Supplier providing credentials

101

102

**Usage Example:**

103

```java

104

import org.openqa.selenium.Credentials;

105

import java.net.URI;

106

107

driver.register(

108

uri -> uri.getHost().equals("secure.example.com"),

109

() -> new Credentials("username", "password")

110

);

111

```

112

113

## Event Logging

114

115

### Register Log Event Listener

116

117

Register a listener for specific log event types.

118

119

```java { .api }

120

public <X> void onLogEvent(EventType<X> kind);

121

```

122

123

**Parameters:**

124

- `kind` (EventType<X>): Type of log event to listen for

125

126

**Usage Example:**

127

```java

128

import org.openqa.selenium.logging.EventType;

129

130

// Listen for console log events

131

driver.onLogEvent(EventType.CONSOLE_API);

132

```

133

134

## Legacy Storage and Location APIs

135

136

### Local Storage (Deprecated)

137

138

Access browser's local storage.

139

140

```java { .api }

141

@Deprecated

142

public LocalStorage getLocalStorage();

143

```

144

145

**Returns:** LocalStorage - Interface to local storage operations

146

147

### Session Storage (Deprecated)

148

149

Access browser's session storage.

150

151

```java { .api }

152

@Deprecated

153

public SessionStorage getSessionStorage();

154

```

155

156

**Returns:** SessionStorage - Interface to session storage operations

157

158

### Location (Deprecated)

159

160

Get current geolocation.

161

162

```java { .api }

163

@Deprecated

164

public Location location();

165

```

166

167

**Returns:** Location - Current geolocation

168

169

### Set Location (Deprecated)

170

171

Set browser geolocation.

172

173

```java { .api }

174

@Deprecated

175

public void setLocation(Location location);

176

```

177

178

**Parameters:**

179

- `location` (Location): New geolocation to set

180

181

## File Detection

182

183

### Set File Detector

184

185

Note: File detection is not supported for local ChromiumDriver instances.

186

187

```java { .api }

188

public void setFileDetector(FileDetector detector);

189

```

190

191

**Throws:** WebDriverException - Always throws as this is only supported for remote instances

192

193

## Types

194

195

```java { .api }

196

// Script reference for pinned scripts

197

public class ScriptKey {

198

public String getIdentifier();

199

}

200

201

// Authentication credentials

202

public class Credentials {

203

public Credentials(String username, String password);

204

public String getUsername();

205

public String getPassword();

206

}

207

208

// Event types for logging

209

public abstract class EventType<T> {

210

public void initializeListener(WebDriver driver);

211

}

212

213

// Geolocation data

214

public class Location {

215

public Location(double latitude, double longitude, double altitude);

216

public double getLatitude();

217

public double getLongitude();

218

public double getAltitude();

219

}

220

```

221

222

## Error Handling

223

224

Common exceptions that may be thrown:

225

226

- **JavascriptException**: When script execution fails or script key is invalid

227

- **WebDriverException**: For general WebDriver errors including unsupported file detection

228

- **IllegalArgumentException**: When null or invalid parameters are provided

229

230

## Best Practices

231

232

1. **Script Pinning**: Use script pinning for frequently executed JavaScript to improve performance

233

2. **Authentication**: Register authentication handlers before navigating to protected URLs

234

3. **Event Logging**: Set up log event listeners early in the test lifecycle

235

4. **Resource Cleanup**: Always call `quit()` to properly clean up pinned scripts and resources

236

5. **Exception Handling**: Wrap script operations in try-catch blocks to handle JavaScript errors gracefully