Skip to content

Commit

Permalink
fix: cookies regression: send Page commands for cookies handling (#1534)
Browse files Browse the repository at this point in the history
* chore: use remote debugger for cookies

* fix naming
  • Loading branch information
KazuCocoa authored Apr 1, 2023
1 parent f246dce commit 908ed1a
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions lib/commands/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand All @@ -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;
},
Expand All @@ -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}
Expand Down

0 comments on commit 908ed1a

Please sign in to comment.