[BUG] Cookies for other domain not present in Chromium or Firefox DevTools after using addCookies
See original GitHub issueContext:
- Playwright Version: 1.1.1
- Operating System: Mac
- Node version: v12.14.1
- Browser: Chromium & Firefox (untested with Webkit)
- Extra:
Code Snippet
Help us help you! Put down a short code snippet that illustrates your bug and that we can run and debug locally. For example:
const json = JSON.parse(rawJson)
Object.keys(json).forEach(url => {
Object.keys(json[url]).forEach(urlExt => {
Object.keys(json[url][urlExt]).forEach(cookieName => {
if (json[url][urlExt][cookieName].value) {
const cookie = {
name: cookieName,
...json[url][urlExt][cookieName],
}
// Firefox doesn't like the following cookie keys
delete cookie.key
delete cookie.maxAge
delete cookie.extensions
delete cookie.creation
delete cookie.hostOnly
delete cookie.lastAccessed
// Fix cookie dates for Playwright
if (cookie.expires) {
cookie.expires = new Date(cookie.expires).getTime() / 1000
}
cookies.push(cookie)
}
})
})
})
await this.context.addCookies(cookies)
let browserCookies = await this.context.cookies()
console.log(browserCookies.length) // ==> logs 8 (as expected)
Describe the bug
The above code does not generate an error - further, the log of cookies does indeed display cookies present (by length) for browser context. However, upon inspection of the browsers opened (the “Application” tab of Dev options in Chrome, and the “Storage” tab of Dev options in Firefox), there are no existing cookies shown.
Apologies for some of the cookie processing mess, we’re acquiring the cookies from a separate tool and some transform was needed before the cookies were accepted by Playwright, and specifically Firefox.
An additional note: on a previous version of playwright, using the same code, we were able to observe cookies in Firefox “Storage” tab as expected.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (5 by maintainers)
@mwallace72 ah, this explains it!
DevTools only show cookies for the currently-open page, so only cookies for the particular URL. I thought there was an option to display all cookies for all URLs in DevTools, but I no longer can find one…
Playwright cookies API work on the context level, so that you can introspect all cookies in the context for all the URLs. So it looks like Playwright is doing a good job here, but browsers devtools aren’t there yet. 🤷♂️
In the application I am currently working on, it is served from localhost but API calls are made to a different domain. I can see requests to those domains pick up an authentication cookie which is stored against that domain, these cookies are present on localhost but with the domain value matching the API server. I can see cookies for both localhost and the API server when inspecting cookies on local host via dev tools.
I am trying to now add those cookies to the browser context in my playwright tests, but as the original issue here states, those cookies with a different domain do not appear when the localhost page loads. I do believe this is an issue, could we re-open this?