jeudi 30 avril 2020

In TestCafe, Is there a way to fetch the token set by USER ROLE functionality?

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:

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.

Aucun commentaire:

Enregistrer un commentaire