Logging in through the form takes about three seconds. With eighty tests that is four minutes of every run, spent typing the same password.
Selenium has no built-in feature for saving a session, so teams solve it themselves. The usual way is to get the session cookie once, then set it directly in later tests.
Step 1: get the cookie after a real login. Do this once, in a setup step:
await driver.get('/');
await driver.findElement(By.css('[data-testid="email-input"]')).sendKeys('demo@nimbus.app');
await driver.findElement(By.css('[data-testid="password-input"]')).sendKeys('secure123');
await driver.findElement(By.css('[data-testid="login-button"]')).click();
await driver.wait(until.elementLocated(By.css('[data-testid="welcome-message"]')), 5000);
const sessionCookie = await driver.manage().getCookie('session');
Step 2: set it in later tests instead of logging in.
beforeEach(async function () {
driver = await new Builder().forBrowser('chrome').build();
await driver.get('/'); // must be on the domain first
await driver.manage().addCookie(sessionCookie);
await driver.navigate().refresh(); // reload so the app reads it
});
The order matters and catches everyone. You cannot set a cookie before visiting the domain, because the browser does not know which site it belongs to. So you visit, set the cookie, then reload.
An even faster option, if your app has a login API: send the login request with an HTTP library, take the cookie from the response, and set it the same way. Then you never open the login page at all.
Each test still gets a fresh browser and only the cookie is restored. Your tests do not share a live session, so they stay independent.
Two warnings. Cookies expire, so get a new one if your suite runs for a long time. And never do this in the test that checks login itself, because that test must go through the real form.
Remember this: visit the domain, then set the cookie, then reload.
Reference: Working with cookies.