0

I am new to Testcafe and implementing a Client Function to read all Cookies by Name that are set by the Website to be tested, as there is no direct way to read all the Cookies in Testcafe and check if a Cookie has a certain name; hence, a Client Function should do the trick. Unfortunately, after checking every possible Ressource on the web, I couldn´t figure out how to call the Client Function with parameters, so that the name of the Cookie to be tested can be passed as Parameter to the Client Function. I didn´t found any example on how to pass Parameters in Testcafe Client Functions.

This is the Client Function:

 const getCookiesFromSite = ClientFunction(() => {
        let cookies = document.cookie.split(";");


    })

This is the Javascript Code that I want to implement in the Client Function and that needs a Cookie Name as a Parameter:

function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

It can also be any other Javascript Function that allows to check the name of the Cookies; the problem arises here, in this line, where I need to check against a certain Cookie name:

 await t.wait(3000);
        await t.navigateTo('http://www.myfavouritewebsite.com')
        const cookies1 = await getCookiesFromSite();
        //await t.expect(cookies1.length).eql(15 || 20)
        //await t.expect(getCookies).contains('cookie_name')

In the following line, I want to check if a Cookie with a certain name has been set; as I need to check against a couple of names, I would even need an Array to do that:

  await t.expect(getCookiesFromSite()).contains('cookie_name');

This is the rest of the test:

   await t.switchToIframe(Selector('*[id^=sp_message_iframe_]'));
        await t.expect(Selector('button[title="Accept all"]').exists).ok();
        await t.switchToMainWindow();
        await consentLayer.clickAcceptButton();
        await t.eval(() => location.reload(true))
        const cookies2 = await getCookiesFromSite();

Hence, two questions in focus here:

  1. How do I pass parameters such as Cookie Names to Client Functions in Testcafe?
  2. How could I check if an Array returned by Testcafe contains a Cookie name or even several Cookie names?

Any hints or help would be very much appreciated, thanks in advance

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
DWA
  • 530
  • 1
  • 5
  • 29

1 Answers1

2

Here is an example of how to pass parameters to the ClientFunction: Pass Parameters to Client Functions.

The contains assertion can check if a particular element exists in the array. To check several cookie names, you can return the whole cookie string from the ClientFunction and use the t.expect.match method:

const getCookiesFromSite = ClientFunction(() => 'Cookie_1; Cookie_2; Cookie_3');

await t.expect(getCookiesFromSite()).match(/Cookie_1|Cookie_2/);
Dmitry Ostashev
  • 2,305
  • 1
  • 5
  • 21