Search code examples
testingcookiesautomated-testse2e-testingtestcafe

Checking if no Cookie exists and if a Cookie exists, delete the Cookie in Testcafe


I am new to Testcafé and working on a Testcase to check if no Cookies exist beforehand and if there is a cookie, to delete the Cookie. I have checked this and this post but don´t see any option on how to check if a cookie does exist or does not exist, as the posts describe on how to set a Cookie.

My first implementation would look like this:

    import { ClientFunction } from 'testcafe';
    
    const setCookie = ClientFunction(() => {
      if (document.cookie.length!==0)
{
document.cookie = 'COOKIE_NAME=; Max-Age=0; path=/; domain=' + location.host;
}
    });
    
    fixture `fixture`
        .page `http://google.com`;
    
    test(`1`, async t => {
        await setCookie();
    
        await t.typeText('input[type=text]', 'test');
    
        await t.debug();
    });

I checked the Testcafé API but didn't find any corresponding method; hence, any help or hints would be very much appreciated, thanks.


Solution

  • Since TestCafe 1.19.0 version there is no need to create excessive Client Functions to interact with browser cookies. Our cookie management API offers a flexible and convenient way to set, get, or delete page cookies regardless of the HttpOnly flag status. You can learn more here https://testcafe.io/403938/release-notes/framework/2022-05-26-testcafe-v1-19-0-released#cookie-management