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
  • 6,227
  • 1
  • 11
  • 30
DWA
  • 482
  • 1
  • 4
  • 19
  • 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

1 Answers1

2

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.

  • 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