1

I don't want to have to make the script log in every time. Is there to stop the web driver opening a "fresh" browser and instead make it open my standard version/load cookies so it auto-logs in?

1
1

The way I've done this in C# is to have a test login normally and then set the cookies in the feature context. When a subsequent test runs, it looks in the feature context for the cookies, sets them, and then navigates directly into the app, bypassing the login.

To get the cookies, you need to call the Manage method on IWebdriver

var cookies = driver.Manage().Cookies.AllCookies;
featureContext.Set(cookies, $"{username}_cookies");

This gets you all cookies. If you don't want all cookies, have a look at the other methods on the ICookieJar interface to get specific cookies.

I've then got a condition before the login method that checks if cookie sharing is turned on in my configuration (sometimes we might want all tests to login normally) and if an entry exists for that user in the feature context. If both of these are true, you can simply do this to set the cookies:

foreach (var cookie in cookieJar)
{
    driver.Manage().Cookies.AddCookie(cookie);
}

After this, you should be able to simply navigate to some URL that would normally require authentication.

I'm not familiar with the python bindings but it looks like you can use

driver.get_cookies()

to get all cookies and

driver.set_cookies()

to set it. Check out the python docs for more info.

If you wanted this to be done without having a forerunner test, you could capture the cookies into a file and then just have your test framework read them in and check if the cookie(s) have expired, only logging in and renewing them when needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.