getCookie(name)
This is a TDK feature
This is not supported in Testim's visual editor
Gets a cookie by a specific name:
- returns: Promise which fulfills with a
Cookie
object for the domain you are in (for another domain you have tonavigate
first:name
{string} cookie namevalue
{string} cookie valuedomain
{string} domain the cookie is visible topath
{string} cookie pathexpires
{number} when the cookie expires, specified in seconds since midnight, January 1, 1970 UTChttpOnly
{boolean} whether the cookie is an httpOnly cookiesecure
{boolean} whether the cookie is a secure cookie
const cookie = await getCookie('whatilanloves');
console.log(cookie.name, cookie.value);
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 goto('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');
});
});
Updated almost 3 years ago