Overview
HasPermissions is a Java interface that provides the ability to programmatically set browser permissions (such as geolocation, notifications, or camera access) on Chromium-based browsers.
Description
The HasPermissions interface, annotated with @Beta, declares a single method setPermission(String name, String value) that adjusts a browser permission to a specified state. The permission name corresponds to a W3C Permissions API name (e.g., "geolocation", "notifications", "camera"), and the value is the desired permission state (e.g., "granted", "denied", "prompt"). This interface is implemented by Chromium-based driver classes such as ChromeDriver and EdgeDriver.
Usage
Use HasPermissions when testing web application behavior that depends on browser permissions, such as geolocation-based features or notification prompts. By setting permissions programmatically, you can avoid interactive permission dialogs during automated tests. Cast the driver to HasPermissions to access the setPermission method.
Code Reference
Source Location
Signature
@Beta
public interface HasPermissions {
void setPermission(String name, String value);
}
Import
import org.openqa.selenium.chromium.HasPermissions;
I/O Contract
Methods
| Method |
Parameters |
Return Type |
Description
|
setPermission(String, String) |
name -- the permission name to set; value -- the permission state to assign |
void |
Sets the specified browser permission to the given state.
|
Input
| Parameter |
Type |
Description
|
name |
String |
The name of the permission to modify. Common values include: "geolocation", "notifications", "camera", "microphone", "clipboard-read", "clipboard-write".
|
value |
String |
The desired state for the permission. Valid values: "granted", "denied", "prompt".
|
Output
| Return Type |
Description
|
void |
No return value. The permission is set as a side effect on the browser session.
|
Usage Examples
ChromeDriver driver = new ChromeDriver();
// Cast the driver to HasPermissions
HasPermissions permissionsDriver = (HasPermissions) driver;
// Grant geolocation permission to avoid the permission prompt
permissionsDriver.setPermission("geolocation", "granted");
// Deny notification permission
permissionsDriver.setPermission("notifications", "denied");
// Grant camera access
permissionsDriver.setPermission("camera", "granted");
// Grant clipboard read access
permissionsDriver.setPermission("clipboard-read", "granted");
// Reset a permission back to the prompt state
permissionsDriver.setPermission("microphone", "prompt");
// Now navigate and test features that depend on these permissions
driver.get("https://www.example.com/location-feature");
driver.quit();
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.