Skip to content

Commit

Permalink
[JS] Add detailed error message for invalid cookie name validation in…
Browse files Browse the repository at this point in the history
… getCookie method
  • Loading branch information
harsha509 committed Jan 13, 2025
1 parent e37334c commit c657e70
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 6 additions & 7 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1957,25 +1957,24 @@ 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<?Options.Cookie>} 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
} catch (err) {
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
}
}
Expand Down
9 changes: 9 additions & 0 deletions javascript/node/selenium-webdriver/test/cookie_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c657e70

Please sign in to comment.