question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Cookies for other domain not present in Chromium or Firefox DevTools after using addCookies

See original GitHub issue

Context:

  • 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:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
aslushnikovcommented, Jul 8, 2020

@mwallace72 ah, this explains it!

I can verify this by additionally setting a cookie {name: ‘cookie1’, value: ‘value1’, url: ‘http://google.com/’} and this cookie does not show in DevTools.

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…

Despite all this, the Playwright context.cookies() does have all cookies I need

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. 🤷‍♂️

0reactions
Billy-commented, Jun 8, 2022

DevTools only show cookies for the currently-open page, so only cookies for the particular URL

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make it possible to add cookies to the storage inspector
To enter a new > cookie the new line should always appear at the top or the bottom of the > list of...
Read more >
[chromium-discuss] Developer Tools not showing cookie ...
1. Go to Tools -> Options · 2. Click Content Settings · 3. Click Show cookies and other site data · 4. Type...
Read more >
Cookies on localhost with explicit domain - Stack Overflow
But Developer tools > Application > Cookies will not show a non-secure cookie when there is a secure cookie of the same name...
Read more >
56211 - chrome.cookies fails for localhost domains - Monorail
working on localhost, when I use the function setcookie() in PHP and give it all the parameters, the cookie won't be stored!! which...
Read more >
How to handle Cookies in Selenium WebDriver | BrowserStack
How to clear the Browser Cache using Selenium WebDriver? Method 1; Method 2. Introduction to Selenium WebDriver Cookies. A cookie is a small ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found