Authentication and sessions

Getting past a login once, answering HTTP basic authentication prompts, and reusing a signed-in state across tests without driving the login form every time.

Almost every test starts behind a login. Driving the login form in each test is slow, it makes every test depend on the login page working, and some forms (two-factor prompts, third-party identity providers, CAPTCHAs) cannot be automated at all. HTTP basic authentication is a different problem again: the browser shows a native dialog that WebDriver cannot see.

Log in through the user interface in exactly one test, the one that tests the login form. Everywhere else, establish the session some other way and start the test already signed in.

For cookie-based sessions, navigate to the application’s domain, add the session cookie with WebDriver’s cookie API, and reload. The cookie must be added while the browser is on a page in the domain the cookie belongs to, otherwise the browser rejects it. Where the cookie comes from is up to you: an API call that logs in, a fixture created on the server, or a value captured from a single interactive login.

For HTTP basic authentication, use the network module of WebDriver BiDi. Register a handler for the authRequired phase and answer it with the credentials; the browser never shows the dialog. Embedding credentials in the URL (https://user:pass@host/) is blocked by modern browsers and should not be relied on.

Keep tests independent: each test should set up the state it needs rather than inheriting it from the one before, and a fresh browser per test is the simplest way to guarantee that.

Add a cookie to the current browsing context:

        driver.get("https://www.selenium.dev/selenium/web/blank.html");
        // Add cookie into current browser context
        driver.manage().addCookie(new Cookie("key", "value"));
    driver = webdriver.Chrome()
    driver.get("http://www.example.com")

    # Adds the cookie into current browser context
    driver.add_cookie({"name": "key", "value": "value"})
         driver.Url="https://www.selenium.dev/selenium/web/blank.html";
         // Add cookie into current browser context
         driver.Manage().Cookies.AddCookie(new Cookie("key", "value"));
    driver.navigate.to 'https://www.selenium.dev/selenium/web/blank.html'
    # Add cookie into current browser context
    driver.manage.add_cookie(name: 'key', value: 'value')
    await driver.get('https://www.selenium.dev/selenium/web/blank.html');

    // set a cookie on the current domain
    await driver.manage().addCookie({ name: 'key', value: 'value' });

Answer a basic authentication challenge with BiDi:

            network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
            network.onAuthRequired(
                    responseDetails ->
                            network.continueWithAuth(
                                    responseDetails.getRequest().getRequestId(),
                                    new UsernameAndPassword("admin", "admin")));
    await network.authRequired(async (event) => {
      await network.continueWithAuth(event.request.request, 'admin','admin')
    })

Common errors

Reference