microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.99k stars 3.67k forks source link

[Question] How to get cookies from api response (APIResponse)? #22597

Open forelock7 opened 1 year ago

forelock7 commented 1 year ago

Use case:

  1. Need to execute special http request and get cookies from response;
  2. Put that cookies into browser context.

Cannot find how to get cookies from api response (APIResponse)?

Before I did it via Java: public Cookies getUserCookies(String host, String userName, String password) { return response(host, userName, password) **.getDetailedCookies()**; } (io.restassured.response.ResponseOptions#getDetailedCookies)

pavelfeldman commented 1 year ago

Please provide more information on your use case. Cookies set in the API responses are set into the browser context automatically.

forelock7 commented 1 year ago

@pavelfeldman It's needed for login via API without filling login form. Case:

  1. Sent request and get 3 cookies from response
  2. Put those cookies into browser and open required page without login.

So is there any easy way to get cookies from response using Playwright? (I could do it from getting headerArray then parsing and so on... but it's not elegant way. Rest-Assured on java provides such)

vibhanshuzscaler commented 1 year ago

I am also facing the same issue do we have any workaround for this ?

har16o commented 1 year ago

I have the same problem. I get a token from the Response Coockies field and I need to take it from there in order to log in without ui. Is there any way to get them? ResponseCoockies

asevrin commented 11 months ago

The same problem. How i can extract cookie value from the server response?

AndreiTen17 commented 7 months ago

Hello everybody , Any luck here ? I got same problem

Diablo-rf commented 4 months ago

Anyone found a solution to this?

AndreiTen17 commented 4 months ago

This works for me , substring from response const setCookie = Login.headers()["set-cookie"] const sessionCookie = setCookie.substring(8, 56)