setCookie(options)

🚧

This is a TDK feature

This is not supported in Testim's visual editor

Sets a cookie on a given page.

  • options {object}
    • name {string} cookie name
    • value {string} cookie value
    • domain {string} domain the cookie is visible to
    • path {string} cookie path
    • expires {number} when the cookie expires, specified in seconds since midnight, January 1, 1970 UTC
    • httpOnly {boolean} whether the cookie is an httpOnly cookie
    • secure {boolean} whether the cookie is a secure cookie
    • returns: Promise that fulfills when the action is complete.
await setCookie({
  name: 'testimcookie',
  value: 'loacker'
});

Full Example:

'use strict';

const expect = require('chai').expect;
const { go, it, describe, getCookie, setCookie, evaluate } = require('testim');

describe('cookies', () => {
    it('sets and gets cookies', async () => {

        const cookieData = {
            name: 'first',
            value: '33'
        }
    
        await go('http://jsbin.testim.io/fim/');
        await setCookie(cookieData);
    
        const cookie = await getCookie('first');
    
        expect(cookie.name).to.eq(cookieData.name);
        expect(cookie.value).to.eq(cookieData.value);
    
        cookie.name = 'second';
    
        await setCookie(cookie);
    
        const cookie2 = await getCookie('second');
        expect(cookie2.name).to.eq(cookie.name);
        expect(cookie2.value).to.eq(cookie.value);
    
        const clientCookie = await evaluate(() => {
            return document.cookie;
        });

        expect(clientCookie).to.eq('first=33; second=33');
    });

    it('sets and gets httpOnly cookie', async () => {

        const cookieData = {
            name: 'first',
            value: '33',
            httpOnly: true
        }
    
        await go('http://jsbin.testim.io/fim/');
        await setCookie(cookieData);
    
        const cookie = await getCookie('first');
    
        expect(cookie.name).to.eq(cookieData.name);
        expect(cookie.value).to.eq(cookieData.value);
        expect(cookie.httpOnly).to.eq(cookieData.httpOnly);

        const clientCookie = await evaluate(() => {
            return document.cookie;
        });

        expect(clientCookie).to.not.contain('first');
    });
});