|
| 1 | +import { browser } from 'k6/browser'; |
| 2 | +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; |
| 3 | + |
| 4 | +export const options = { |
| 5 | + scenarios: { |
| 6 | + ui: { |
| 7 | + executor: 'shared-iterations', |
| 8 | + options: { |
| 9 | + browser: { |
| 10 | + type: 'chromium', |
| 11 | + }, |
| 12 | + }, |
| 13 | + }, |
| 14 | + }, |
| 15 | + thresholds: { |
| 16 | + checks: ["rate==1.0"] |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +export default async function () { |
| 21 | + const page = await browser.newPage(); |
| 22 | + const context = page.context(); |
| 23 | + |
| 24 | + try { |
| 25 | + // get cookies from the browser context |
| 26 | + await check(await context.cookies(), { |
| 27 | + 'initial number of cookies should be zero': c => c.length === 0, |
| 28 | + }); |
| 29 | + |
| 30 | + // add some cookies to the browser context |
| 31 | + const unixTimeSinceEpoch = Math.round(new Date() / 1000); |
| 32 | + const day = 60*60*24; |
| 33 | + const dayAfter = unixTimeSinceEpoch+day; |
| 34 | + const dayBefore = unixTimeSinceEpoch-day; |
| 35 | + await context.addCookies([ |
| 36 | + // this cookie expires at the end of the session |
| 37 | + { |
| 38 | + name: 'testcookie', |
| 39 | + value: '1', |
| 40 | + sameSite: 'Strict', |
| 41 | + domain: '127.0.0.1', |
| 42 | + path: '/', |
| 43 | + httpOnly: true, |
| 44 | + secure: true, |
| 45 | + }, |
| 46 | + // this cookie expires in a day |
| 47 | + { |
| 48 | + name: 'testcookie2', |
| 49 | + value: '2', |
| 50 | + sameSite: 'Lax', |
| 51 | + domain: '127.0.0.1', |
| 52 | + path: '/', |
| 53 | + expires: dayAfter, |
| 54 | + }, |
| 55 | + // this cookie expires in the past, so it will be removed. |
| 56 | + { |
| 57 | + name: 'testcookie3', |
| 58 | + value: '3', |
| 59 | + sameSite: 'Lax', |
| 60 | + domain: '127.0.0.1', |
| 61 | + path: '/', |
| 62 | + expires: dayBefore |
| 63 | + } |
| 64 | + ]); |
| 65 | + let cookies = await context.cookies(); |
| 66 | + await check(cookies.length, { |
| 67 | + 'number of cookies should be 2': n => n === 2, |
| 68 | + }); |
| 69 | + await check(cookies[0], { |
| 70 | + 'cookie 1 name should be testcookie': c => c.name === 'testcookie', |
| 71 | + 'cookie 1 value should be 1': c => c.value === '1', |
| 72 | + 'cookie 1 should be session cookie': c => c.expires === -1, |
| 73 | + 'cookie 1 should have domain': c => c.domain === '127.0.0.1', |
| 74 | + 'cookie 1 should have path': c => c.path === '/', |
| 75 | + 'cookie 1 should have sameSite': c => c.sameSite == 'Strict', |
| 76 | + 'cookie 1 should be httpOnly': c => c.httpOnly === true, |
| 77 | + 'cookie 1 should be secure': c => c.secure === true, |
| 78 | + }); |
| 79 | + await check(cookies[1], { |
| 80 | + 'cookie 2 name should be testcookie2': c => c.name === 'testcookie2', |
| 81 | + 'cookie 2 value should be 2': c => c.value === '2', |
| 82 | + }); |
| 83 | + |
| 84 | + // let's add more cookies to filter by urls. |
| 85 | + await context.addCookies([ |
| 86 | + { |
| 87 | + name: "foo", |
| 88 | + value: "42", |
| 89 | + sameSite: "Strict", |
| 90 | + url: "http://foo.com", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "bar", |
| 94 | + value: "43", |
| 95 | + sameSite: "Lax", |
| 96 | + url: "https://bar.com", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "baz", |
| 100 | + value: "44", |
| 101 | + sameSite: "Lax", |
| 102 | + url: "https://baz.com", |
| 103 | + }, |
| 104 | + ]); |
| 105 | + cookies = await context.cookies("http://foo.com", "https://baz.com"); |
| 106 | + await check(cookies.length, { |
| 107 | + 'number of filtered cookies should be 2': n => n === 2, |
| 108 | + }); |
| 109 | + await check(cookies[0], { |
| 110 | + 'the first filtered cookie name should be foo': c => c.name === 'foo', |
| 111 | + 'the first filtered cookie value should be 42': c => c.value === '42', |
| 112 | + }); |
| 113 | + await check(cookies[1], { |
| 114 | + 'the second filtered cookie name should be baz': c => c.name === 'baz', |
| 115 | + 'the second filtered cookie value should be 44': c => c.value === '44', |
| 116 | + }); |
| 117 | + |
| 118 | + // clear cookies |
| 119 | + await context.clearCookies(); |
| 120 | + await check(await context.cookies(), { |
| 121 | + 'number of cookies should be zero': c => c.length === 0, |
| 122 | + }); |
| 123 | + } finally { |
| 124 | + await page.close(); |
| 125 | + } |
| 126 | +} |
0 commit comments