Skip to content
29 changes: 6 additions & 23 deletions java/src/org/openqa/selenium/UsernameAndPassword.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,19 @@

package org.openqa.selenium;

import java.util.function.Supplier;
import org.jspecify.annotations.NullMarked;
import org.openqa.selenium.internal.Require;

/** A combination of username and password to use when authenticating a browser with a website. */
@NullMarked
public class UsernameAndPassword implements Credentials {
public record UsernameAndPassword(String username, String password) implements Credentials {

private final String username;
private final String password;

public UsernameAndPassword(String username, String password) {
this.username = Require.nonNull("User name", username);
this.password = Require.nonNull("Password", password);
}

public static Supplier<Credentials> of(String username, String password) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, note that this method is used by Selenium users, for example:

import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {

  public static void main(String[] args) throws InterruptedException {
    var driver = new ChromeDriver();
    driver.register(UsernameAndPassword.of("foo", "bar"));

    driver.get("http://httpbin.org/basic-auth/foo/bar");

    Thread.sleep(10_000);
    driver.quit();
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mk868 New commit with the new code added. Thanks

Require.nonNull("User name", username);
Require.nonNull("Password", password);

Credentials creds = new UsernameAndPassword(username, password);

return () -> creds;
}

public String username() {
return username;
public UsernameAndPassword {
username = Require.nonNull("User name", username);
password = Require.nonNull("Password", password);
}

public String password() {
return password;
public static UsernameAndPassword of(String username, String password) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. It is changing the function return type.

return new UsernameAndPassword(username, password);
}
}