t.getCookies Method
Retrieves cookies from test pages. Cannot be chained with other TestController
methods.
Note
The order of cookies in the array depends on circumstances beyond the framework’s control. Do not expect cookie order to persist between test runs. Reference cookies by name, and not by their array index.
Multiple method overloads are available. Review overload options below to learn about different ways to specify target cookies.
The following example uses a t.getCookies
overload to retrieve a cookie with the specified name. The method searches all tested pages.
fixture`[API] Get Cookies`
.page('https://devexpress.github.io/testcafe/example/');
test('Should retrieve a cookie by name', async t => {
//set a cookie for the Example page
await t.setCookies({ name: 'apiCookie1', value: 'value1' });
//retrieve the named cookie from any of the tested pages
const cookies = await t.getCookies('apiCookie1');
const { name, value } = cookies[0];
await t
.expect(name).eql('apiCookie1')
.expect(value).eql('value1');
});
The following example uses a t.getCookies
overload to retrieve cookies with the specified names and URLs.
fixture('[API] Get Cookies')
.page('https://devexpress.github.io/testcafe/example/');
test('Should retrieve a cookie 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');
//retrieve all cookies
let cookies = await t.getCookies(['apiCookie1', 'apiCookie2']);
await t.expect(cookies.length).eql(2);
//retrieve cookies from the Example page
cookies = await t.getCookies(['apiCookie1', 'apiCookie2'], 'https://devexpress.github.io/testcafe/example/');
await t.expect(cookies.length).eql(1);
//retrieve cookies from localhost
cookies = await t.getCookies(['apiCookie1', 'apiCookie2'], 'https://localhost');
await t.expect(cookies.length).eql(1);
});
The following example uses a t.getCookies
overload to retrieve all cookies from the specified URLs.
fixture('[API] Get Cookies')
.page('https://devexpress.github.io/testcafe/example/');
test('Should retrieve all 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');
//retrieve all cookies from devexpress.github.io
let cookies = await t.getCookies({ domain: 'devexpress.github.io' });
await t.expect(cookies.length).eql(2);
//retrieve all the cookies from the Example page
cookies = await t.getCookies({ domain: 'devexpress.github.io', path: '/testcafe/example/' });
await t
.expect(cookies.length).eql(1)
.expect(cookies[0]).contains({ name: 'apiCookie1', value: 'value1' });
//retrieve all the cookies from the 'Thank you' page
cookies = await t.getCookies({ domain: 'devexpress.github.io', path: '/testcafe/example/thank-you.html' });
//the method returns two cookies, because the Example page cookie
//applies to the nested 'Thank You' page as well
await t
.expect(cookies.length).eql(2)
.expect(cookies[0]).contains({ name: 'apiCookie1', value: 'value1' })
.expect(cookies[1]).contains({ name: 'apiCookie2', value: 'value2' });
});
t.getCookies()
getCookies() → Promise<CookieOptions[]>
Returns all cookies from all test pages.
t.getCookies(names, urls)
getCookies(names: string | string[], urls?: string | string[]) → Promise<CookieOptions[]>;
Returns cookies with the specified name(s) and URL context(s).
Parameter | Type | Description |
---|---|---|
names |
string \| string[] |
Cookie name(s) |
urls (optional) |
string \| string[] |
Test page URL(s). If not specified, the method returns cookies from all the tested pages. |
t.getCookies(cookies)
getCookies(cookies: CookieOptions | CookieOptions[]) → Promise<CookieOptions[]>
Returns cookies that match the specified cookie object(s).
Parameter | Type | Description |
---|---|---|
cookies |
CookieOptions \| CookieOptions[] |
Cookie object(s) |
If the cookie object doesn’t specify domain
and path
, the method returns cookies from all the tested pages.
Cookie Object
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. |