All files / webdriverio/src/commands/browser getCookies.js

100% Statements 9/9
100% Branches 8/8
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                  5x   5x 1x     4x 1x     3x   9x    
/**
 *
 * Retrieve a [cookie](https://w3c.github.io/webdriver/webdriver-spec.html#cookies)
 * visible to the current page. You can query a specific cookie by providing the cookie name or
 * retrieve all.
 *
 * <example>
    :getCookies.js
    it('should return a cookie for me', () => {
        browser.setCookies([
            {name: 'test', value: '123'},
            {name: 'test2', value: '456'}
        ])
        const testCookie = browser.getCookies(['test'])
        console.log(testCookie); // outputs: [{ name: 'test', value: '123' }]
 
        const allCookies = browser.getCookies()
        console.log(allCookies);
        // outputs:
        // [
        //    { name: 'test', value: '123' },
        //    { name: 'test2', value: '456' }
        // ]
    })
 * </example>
 *
 * @alias browser.getCookies
 * @param {String[]=} names  names of requested cookies
 * @return {Object[]}        requested cookies if existing
 * @uses webdriver/getAllCookies
 *
 */
export default async function getCookies(names) {
    const namesList = typeof names !== 'undefined' && !Array.isArray(names) ? [names] : names
 
    if (typeof namesList === 'undefined') {
        return this.getAllCookies()
    }
 
    if (namesList.every(obj => typeof obj !== 'string')) {
        throw new Error('Invalid input (see https://webdriver.io/docs/api/browser/getCookies.html for documentation.')
    }
 
    const allCookies = await this.getAllCookies()
 
    return allCookies.filter(cookie => namesList.includes(cookie.name))
}