diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index d910f54d09bf5..b50a7b578ac23 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -1957,11 +1957,17 @@ class Options { * WebDriver wire protocol. * * @param {string} name The name of the cookie to retrieve. + * @throws {InvalidArgumentError} - If the cookie name is empty or invalid. * @return {!Promise} A promise that will be resolved * with the named cookie * @throws {error.NoSuchCookieError} if there is no such cookie. */ async getCookie(name) { + // Validate the cookie name is non-empty and properly trimmed. + if (!name?.trim()) { + throw new error.InvalidArgumentError('Cookie name cannot be empty') + } + try { const cookie = await this.driver_.execute(new command.Command(command.Name.GET_COOKIE).setParameter('name', name)) return cookie @@ -1969,13 +1975,6 @@ class Options { if (!(err instanceof error.UnknownCommandError) && !(err instanceof error.UnsupportedOperationError)) { throw err } - - const cookies = await this.getCookies() - for (let cookie of cookies) { - if (cookie && cookie['name'] === name) { - return cookie - } - } return null } } diff --git a/javascript/node/selenium-webdriver/test/cookie_test.js b/javascript/node/selenium-webdriver/test/cookie_test.js index 82179bb343dac..c5db4bf15d9d3 100644 --- a/javascript/node/selenium-webdriver/test/cookie_test.js +++ b/javascript/node/selenium-webdriver/test/cookie_test.js @@ -54,6 +54,15 @@ suite(function (env) { }) }) + it('throw error if name is null', async function () { + const cookie = createCookieSpec() + await driver.manage().addCookie(cookie) + await assert.rejects(async () => await driver.manage().getCookie(null), { + name: 'InvalidArgumentError', + message: `Cookie name cannot be empty`, + }) + }) + it('can get all cookies', async function () { const cookie1 = createCookieSpec() const cookie2 = createCookieSpec()