0

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.

pavelsaman
  • 7,399
  • 1
  • 14
  • 32
DWA
  • 530
  • 1
  • 5
  • 29
  • 1
    How about just delete all cookies and then fire up the test? Conditions make your tests more difficult, that means more risk you'll eventually get it wrong. – pavelsaman Jun 30 '21 at 17:59

2 Answers2

1

Please refer to this thread: Clearing all cookies with JavaScript It has a good code sample that demonstrates how you can parse \ clear cookies. You can use this code in your ClientFunction as a starting point to detect and clear a specific cookie.

Pavel Avsenin
  • 254
  • 1
  • 2
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Anton Menshov Jul 24 '21 at 13:44
1

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

Helen Dikareva
  • 1,026
  • 6
  • 7