From 908ed1a6d64e4522d95502e125bde32032a3e686 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 1 Apr 2023 11:59:58 +0900 Subject: [PATCH] fix: cookies regression: send Page commands for cookies handling (#1534) * chore: use remote debugger for cookies * fix naming --- lib/commands/web.js | 60 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/commands/web.js b/lib/commands/web.js index 5a8730fcc..f677c8122 100644 --- a/lib/commands/web.js +++ b/lib/commands/web.js @@ -195,25 +195,28 @@ const commands = { * @this {XCUITestDriver} */ async getCookies() { - if (!this.isWebContext()) { - throw new errors.NotImplementedError(); - } - - this.log.debug('Retrieving all cookies'); + if (!this.isWebContext()) { + throw new errors.NotImplementedError(); + } - let script = 'return document.cookie'; - let jsCookies = await this.executeAtom('execute_script', [script, []]); + // get the cookies from the remote debugger, or an empty object + const cookies = await this.remote.getCookies() || {cookies: []}; - let cookies = []; - try { - for (let [name, value] of _.toPairs(cookieUtils.createJWPCookie(undefined, jsCookies))) { - cookies.push({name, value}); + // the value is URI encoded, so decode it safely + const decodedCookieValues = cookies.cookies.map((cookie) => { + try { + return decodeURI(cookie.value); + } catch (error) { + this.log.debug(`Cookie ${cookie.name} was not decoded successfully. Cookie value: ${cookie.value}`); + this.log.warn(error); + return undefined; } - return cookies; - } catch (err) { - this.log.error(err); - throw new errors.UnknownError(`Error parsing cookies from result: '${jsCookies}'`); - } + }); + + // zip cookies with decoded value, removing undefined cookie values + return _.zip(cookies.cookies, decodedCookieValues) + .filter(([, value]) => !_.isUndefined(value)) + .map(([cookie, value]) => Object.assign({}, cookie, {value})); }, /** * @this {XCUITestDriver} @@ -250,14 +253,15 @@ const commands = { throw new errors.NotImplementedError(); } - // check cookie existence - let cookies = await this.getCookies(); - if (_.indexOf(_.map(cookies, 'name'), cookieName) === -1) { + const cookies = await this.getCookies(); + const cookie = cookies.find((cookie) => cookie.name === cookieName); + if (!cookie) { this.log.debug(`Cookie '${cookieName}' not found. Ignoring.`); return true; } - return await this._deleteCookie(cookieName); + await this._deleteCookie(cookie); + return true; }, /** * @this {XCUITestDriver} @@ -267,11 +271,9 @@ const commands = { throw new errors.NotImplementedError(); } - let cookies = await this.getCookies(); - if (cookies.length) { - for (let cookie of cookies) { - await this._deleteCookie(cookie.name); - } + const cookies = await this.getCookies(); + for (const cookie of cookies) { + await this._deleteCookie(cookie); } return true; }, @@ -281,11 +283,9 @@ const helpers = { /** * @this {XCUITestDriver} */ - async _deleteCookie(cookieName) { - this.log.debug(`Deleting cookie '${cookieName}'`); - let webCookie = cookieUtils.expireCookie(cookieName, {path: '/'}); - let script = `document.cookie = ${JSON.stringify(webCookie)}`; - await this.executeAtom('execute_script', [script, []]); + async _deleteCookie(cookie) { + const url = `http${cookie.secure ? 's' : ''}://${cookie.domain}${cookie.path}`; + return await this.remote.deleteCookie(cookie.name, url); }, /** * @this {XCUITestDriver}