How to get cookies from Browser

Ask general questions here.
User avatar
jarrettmk
Posts: 15
Joined: Wed Sep 19, 2018 6:10 pm

How to get cookies from Browser

Post by jarrettmk » Mon Jan 07, 2019 8:36 pm

Our web application sets a cookie that I need to retrieve and decrypt in order to get some information needed to validate later in my test.

I have looked at Host.Current as well as the following to try and get the cookies with no luck...
var cookies = Repository.OURSITE.Self.ExecuteScript("document.cookie");
and
var cookies = Repository.OURSITE.Self.ExecuteScript("document.cookie.split(';');");
both return "undefined".

I know with straight Selenium you can use something like:
var cookies = remoteWebDriver.Manage().Cookies;
or
var cookies = driver.Manage().Cookies;
However, I cannot find anything like this that is equivalent in Ranorex.

Does anyone know how to get the browser's cookies?
Jarrett Karyshyn
Quality Assurance & Automation Engineer
USA - EST (UTC−05:00)/EDT (UTC−04:00)
Image

User avatar
RobinHood42
Posts: 324
Joined: Fri Jan 09, 2015 3:24 pm

Re: How to get cookies from Browser

Post by RobinHood42 » Tue Jan 08, 2019 9:10 am

Hi,

Please try the following code:
var cookies = Repository.OURSITE.Self.ExecuteScript("return JSON.stringify(document.cookie)");
Cheers,
Robin

User avatar
jarrettmk
Posts: 15
Joined: Wed Sep 19, 2018 6:10 pm

Re: How to get cookies from Browser

Post by jarrettmk » Tue Jan 08, 2019 6:22 pm

That did the trick. Thanks!
Jarrett Karyshyn
Quality Assurance & Automation Engineer
USA - EST (UTC−05:00)/EDT (UTC−04:00)
Image

User avatar
jarrettmk
Posts: 15
Joined: Wed Sep 19, 2018 6:10 pm

Re: How to get cookies from Browser

Post by jarrettmk » Tue Mar 16, 2021 6:03 pm

The previously posted solution worked for me up until the latest build our developers sent over. They said that they have changed the cookies so that now they cannot be accessed via JavaScript so the above mentioned solution is now breaking. Is there another way to grab cookies from the browser without using JS?
Jarrett Karyshyn
Quality Assurance & Automation Engineer
USA - EST (UTC−05:00)/EDT (UTC−04:00)
Image