t.deleteCookies Method

Deletes cookies from test pages. Chainable.

Multiple method overloads are available. Review overload options below to learn about different ways to specify target cookies.

The following example uses a t.deleteCookies overload that removes a cookie with the specified name.

fixture`[API] Delete Cookies`
    .page('https://devexpress.github.io/testcafe/example/');

test('Should delete the cookie with the specified name', async t => {
    await t.setCookies({ name: 'apiCookie1', value: 'value1' });
    await t.expect((await t.getCookies()).length).eql(1);

    await t.deleteCookies('apiCookie1');
    await t.expect((await t.getCookies()).length).eql(0);
});

The following example uses a t.deleteCookies overload that deletes a cookie with the specified URL.

fixture('[API] Delete Cookies')
    .page('https://devexpress.github.io/testcafe/example/');

test('Should delete cookies with the specified url', async t => {
    //set a cookie for the Example page
    await t.setCookies({ name: 'apiCookie1', value: 'value1' });

    //set a cookie for localhost
    await t.setCookies({ name: 'apiCookie2', value: 'value2' }, 'http://localhost');

    //check the total number of cookies
    await t.expect((await t.getCookies()).length).eql(2);

    //delete the localhost cookie
    await t.deleteCookies('apiCookie2', 'http://localhost');

    const cookies = await t.getCookies();

    await t
        .expect(cookies.length).eql(1)
        .expect(cookies[0]).contains({ name: 'apiCookie1', value: 'value1', domain: 'devexpress.github.io' });
});

The following example uses a t.deleteCookies overload that deletes all cookies with the specified URL.

fixture('[API] Delete Cookies')
    .page('https://devexpress.github.io/testcafe/example/');

test('Should delete all the cookies with the specified url', async t => {
    //set a cookie for the Example page
    await t.setCookies({ name: 'apiCookie1', value: 'value1' });

    //set a cookie for the 'Thank you' page
    await t.setCookies({
        name:  'apiCookie2',
        value: 'value2',
    }, 'https://devexpress.github.io/testcafe/example/thank-you.html');

    //check the cookies
    let cookies = await t.getCookies();

    await t
        .expect(cookies.length).eql(2)
        .expect(cookies[0]).contains({ name: 'apiCookie1', path: '/testcafe/example/' })
        .expect(cookies[1]).contains({ name: 'apiCookie2', path: '/testcafe/example/thank-you.html' });

    //delete cookies from the 'Thank you' page
    await t.deleteCookies({ domain: 'devexpress.github.io', path: '/testcafe/example/thank-you.html' });

    //check the cookies
    cookies = await t.getCookies();

    await t
        .expect(cookies.length).eql(1)
        .expect(cookies[0]).contains({ name: 'apiCookie1', path: '/testcafe/example/' });
});

t.deleteCookies()

deleteCookies() → this | Promise<any>

Deletes all cookies from all tested pages.

t.deleteCookies(name, urls)

deleteCookies(names: string | string[], urls?: string | string[]) → this | Promise<any>

Deletes cookies with the specified name and context.

Parameter Type Description
names string \| string[] Cookie name(s)
urls (optional) string \| string[] Test page URL(s). If not specified, the method deletes cookies from all tested pages.

t.deleteCookies(cookies)

deleteCookies(cookies: CookieOptions | CookieOptions[]) → this | Promise<any>

Deletes cookies that match the specified cookie object(s).

Parameter Type Description
cookies CookieOptions \| CookieOptions[] Cookie object(s)

If the cookie object does not specify domain and path, the method deletes cookies from all tested pages.

A cookie object contains all properties that can be associated with a web cookie. The CookieOptions interface declares the cookie object structure.

export interface CookieOptions {
    name?: string;
    value?: string;
    domain?: string;
    path?: string;
    expires?: Date;
    maxAge?: number | 'Infinity' | '-Infinity';
    secure?: boolean;
    httpOnly?: boolean;
    sameSite?: string;
}
Field Type Description
name (optional) string Specifies the name of the cookie.
value (optional) string Specifies the value of the cookie.
domain (optional) string Specifies the domain associated with the cookie.
path (optional) string Specifies the path associated with the cookie.
expires (optional) Date Specifies the cookie expiration date (the time when the cookie is automatically erased).
maxAge (optional) number \| 'Infinity' \| '-Infinity' Specifies the cookie’s expiration in seconds from the current moment.
secure (optional) boolean Specifies that the cookie should be transferred only over HTTPS.
httpOnly (optional) boolean Specifies that JavaScript code should not have access to the cookie.
sameSite (optional) string Doesn’t allow the browser to send the cookie with requests that come from a different site. This option helps prevent XSRF attacks.