MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / addCookie

Method addCookie

javascript/selenium-webdriver/lib/webdriver.js:2011–2051  ·  view source on GitHub ↗

* Adds a cookie. * * __Sample Usage:__ * * // Set a basic cookie. * driver.manage().addCookie({name: 'foo', value: 'bar'}); * * // Set a cookie that expires in 10 minutes. * let expiry = new Date(Date.now() + (10 * 60 * 1000)); * driver.manage().addCook

({ name, value, path, domain, secure, httpOnly, expiry, sameSite })

Source from the content-addressed store, hash-verified

2009 * @throws {TypeError} if `spec` is not a cookie object.
2010 */
2011 addCookie({ name, value, path, domain, secure, httpOnly, expiry, sameSite }) {
2012 // We do not allow '=' or ';' in the name.
2013 if (/[;=]/.test(name)) {
2014 throw new error.InvalidArgumentError('Invalid cookie name "' + name + '"')
2015 }
2016
2017 // We do not allow ';' in value.
2018 if (/;/.test(value)) {
2019 throw new error.InvalidArgumentError('Invalid cookie value "' + value + '"')
2020 }
2021
2022 if (typeof expiry === 'number') {
2023 expiry = Math.floor(expiry)
2024 } else if (expiry instanceof Date) {
2025 let date = /** @type {!Date} */ (expiry)
2026 expiry = Math.floor(date.getTime() / 1000)
2027 }
2028
2029 if (sameSite && !['Strict', 'Lax', 'None'].includes(sameSite)) {
2030 throw new error.InvalidArgumentError(
2031 `Invalid sameSite cookie value '${sameSite}'. It should be one of "Lax", "Strict" or "None"`,
2032 )
2033 }
2034
2035 if (sameSite === 'None' && !secure) {
2036 throw new error.InvalidArgumentError('Invalid cookie configuration: SameSite=None must be Secure')
2037 }
2038
2039 return this.driver_.execute(
2040 new command.Command(command.Name.ADD_COOKIE).setParameter('cookie', {
2041 name: name,
2042 value: value,
2043 path: path,
2044 domain: domain,
2045 secure: !!secure,
2046 httpOnly: !!httpOnly,
2047 expiry: expiry,
2048 sameSite: sameSite,
2049 }),
2050 )
2051 }
2052
2053 /**
2054 * Deletes all cookies visible to the current page.

Callers

nothing calls this directly

Calls 4

includesMethod · 0.80
setParameterMethod · 0.80
executeMethod · 0.65
testMethod · 0.45

Tested by

no test coverage detected