diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index 2dd4f6dd332dd..64e407db3996c 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -1111,7 +1111,7 @@ class Options { * invalid. * @throws {TypeError} if `spec` is not a cookie object. */ - addCookie({name, value, path, domain, secure, httpOnly, expiry}) { + addCookie({name, value, path, domain, secure, httpOnly, expiry, sameSite}) { // We do not allow '=' or ';' in the name. if (/[;=]/.test(name)) { throw new error.InvalidArgumentError( @@ -1131,6 +1131,11 @@ class Options { expiry = Math.floor(date.getTime() / 1000); } + if(sameSite && sameSite !== 'Strict' && sameSite !== 'Lax') { + throw new error.InvalidArgumentError( + `Invalid sameSite cookie value '${sameSite}'. It should be either "Lax" (or) "Strict" `); + } + return this.driver_.execute( new command.Command(command.Name.ADD_COOKIE). setParameter('cookie', { @@ -1140,7 +1145,8 @@ class Options { 'domain': domain, 'secure': !!secure, 'httpOnly': !!httpOnly, - 'expiry': expiry + 'expiry': expiry, + 'sameSite': sameSite })); } @@ -1399,6 +1405,17 @@ Options.Cookie.prototype.httpOnly; Options.Cookie.prototype.expiry; +/** + * When the cookie applies to a SameSite policy. + * + * When {@linkplain Options#addCookie() adding a cookie}, this may be specified + * as a {@link string} object which is either 'Lax' or 'Strict'. + * + * + * @type {(!Date|number|undefined)} + */ +Options.Cookie.prototype.sameSite; + /** * An interface for managing the current window. * diff --git a/javascript/node/selenium-webdriver/test/cookie_test.js b/javascript/node/selenium-webdriver/test/cookie_test.js index b124fcf367487..04d2ed737d5f0 100644 --- a/javascript/node/selenium-webdriver/test/cookie_test.js +++ b/javascript/node/selenium-webdriver/test/cookie_test.js @@ -168,6 +168,24 @@ suite(function(env) { await driver.sleep(expirationDelay); await assertHasCookies(); }); + + it('can add same site cookie property to `Strict`', async function() { + let cookie = createSameSiteCookieSpec('Strict'); + let childUrl = fileserver.whereIs('child/childPage.html'); + await driver.get(childUrl); + await driver.manage().addCookie(cookie); + const actual = await driver.manage().getCookie(cookie.name); + assert.equal(actual.sameSite, "Strict"); + }); + + it('can add same site cookie property to `Lax`', async function() { + let cookie = createSameSiteCookieSpec('Lax'); + let childUrl = fileserver.whereIs('child/childPage.html'); + await driver.get(childUrl); + await driver.manage().addCookie(cookie); + const actualCookie = await driver.manage().getCookie(cookie.name); + assert.equal(actualCookie.sameSite, "Lax"); + }); }); function createCookieSpec(opt_options) { @@ -181,6 +199,14 @@ suite(function(env) { return spec; } + function createSameSiteCookieSpec(sameSiteVal) { + return { + name: getRandomString(), + value: getRandomString(), + sameSite: sameSiteVal + }; + } + function buildCookieMap(cookies) { var map = {}; cookies.forEach(function(cookie) {