Overview
Concrete value object for holding username and password credentials provided by the Selenium WebDriver library.
Description
UsernameAndPassword is an immutable value object that implements the Credentials interface and holds a pair of non-null username and password strings. It validates that neither value is null at construction time using Require.nonNull(). The class also provides a static factory method of() that returns a Supplier<Credentials>, which is useful for registering credential providers in authentication handlers. The username() and password() methods provide access to the stored values.
Usage
Use UsernameAndPassword when you need to supply credentials for browser authentication dialogs (HTTP Basic/Digest authentication). It is typically used with Selenium's HasAuthentication interface to automatically handle authentication prompts, or with the BiDi protocol's authentication handling capabilities.
Code Reference
Source Location
Signature
public class UsernameAndPassword implements Credentials {
private final String username;
private final String password;
public UsernameAndPassword(String username, String password)
public static Supplier<Credentials> of(String username, String password)
public String username()
public String password()
}
Import
import org.openqa.selenium.UsernameAndPassword;
I/O Contract
| Parameter |
Type |
Description |
Constraint
|
| username |
String |
The username for authentication |
Must not be null
|
| password |
String |
The password for authentication |
Must not be null
|
| Method |
Return Type |
Description
|
username() |
String |
Returns the stored username
|
password() |
String |
Returns the stored password
|
of(String, String) |
Supplier<Credentials> |
Static factory that returns a supplier wrapping a UsernameAndPassword instance
|
| Validation |
Constraint |
Exception
|
| Null username |
Username must not be null |
NullPointerException (via Require.nonNull())
|
| Null password |
Password must not be null |
NullPointerException (via Require.nonNull())
|
Usage Examples
// Create credentials directly
UsernameAndPassword creds = new UsernameAndPassword("admin", "secret123");
String user = creds.username(); // "admin"
String pass = creds.password(); // "secret123"
// Use the static factory to create a Supplier<Credentials>
Supplier<Credentials> credentialSupplier = UsernameAndPassword.of("admin", "secret123");
Credentials credentials = credentialSupplier.get();
// Register authentication handler with HasAuthentication
((HasAuthentication) driver).register(UsernameAndPassword.of("user", "pass"));
// Use with a URI predicate to match specific domains
((HasAuthentication) driver).register(
uri -> uri.getHost().contains("example.com"),
UsernameAndPassword.of("user", "pass")
);
// Use in BiDi authentication handling
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails -> network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("admin", "password")
)
);
}
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.