Introduction
I'm using TestCafe and there are some redundant steps I'm using which can be easily replaced by making direct API calls to save a lot of time. Also, in my current tests, I'm using UserRole functionality of TestCafe to avoid login in every tests.
What's the problem?
To make API calls, I would need a token. UserRole already saves it in a cookie but I can't find a way to fetch it.
What I did so far?
I did debug test to look for cookies and I see there are a bunch of cookies in the browser but I can't see relevant which can be used as a token.
There is a way for me to get the cookie using this part of the code which I'm able to fetch when I don't use User Role functionality but unsuccessful in using the below with User Role functionality:
const getCookie = ClientFunction((name) => {
const nameEQ = `${name}=`;
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i += 1) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
});
but I need to know the name of the cookie which I'm not aware of how TestCafe had set it.
Usually, an authentication cookie has the httpOnly property. It means you cannot access such a cookie from the client side. TestCafe repeats the native browser behavior and restricts access to the httpOnly
cookie from code inside of the ClientFunction
. Also, at present, there is no public API to use httpOnly
cookies in test code.
Note that the TestCafe team does not recommended you manually set up cookies. This may cause unstable tests.