Skip to content

Commit f068585

Browse files
chore: Ditch unused cookie-related functions (#2647)
1 parent 11d1793 commit f068585

File tree

3 files changed

+82
-122
lines changed

3 files changed

+82
-122
lines changed

lib/commands/web.js

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {timing, util} from 'appium/support';
33
import {retryInterval} from 'asyncbox';
44
import B, {TimeoutError, AggregateError} from 'bluebird';
55
import _ from 'lodash';
6-
import * as cookieUtils from '../cookies';
76

87
const IPHONE_TOP_BAR_HEIGHT = 71;
98
const IPHONE_SCROLLED_TOP_BAR_HEIGHT = 41;
@@ -88,6 +87,9 @@ export async function setFrame(frame) {
8887
/**
8988
* @this {XCUITestDriver}
9089
* @group Mobile Web Only
90+
* @param {string} propertyName
91+
* @param {Element | string} el
92+
* @returns {Promise<string>}
9193
*/
9294
export async function getCssProperty(propertyName, el) {
9395
if (!this.isWebContext()) {
@@ -129,6 +131,7 @@ export async function refresh() {
129131
/**
130132
* @this {XCUITestDriver}
131133
* @group Mobile Web Only
134+
* @returns {Promise<string>}
132135
*/
133136
export async function getUrl() {
134137
if (!this.isWebContext()) {
@@ -141,6 +144,7 @@ export async function getUrl() {
141144
/**
142145
* @this {XCUITestDriver}
143146
* @group Mobile Web Only
147+
* @returns {Promise<string>}
144148
*/
145149
export async function title() {
146150
if (!this.isWebContext()) {
@@ -153,6 +157,7 @@ export async function title() {
153157
/**
154158
* @this {XCUITestDriver}
155159
* @group Mobile Web Only
160+
* @returns {Promise<import('@appium/types').Cookie[]>}
156161
*/
157162
export async function getCookies() {
158163
if (!this.isWebContext()) {
@@ -182,34 +187,36 @@ export async function getCookies() {
182187
/**
183188
* @this {XCUITestDriver}
184189
* @group Mobile Web Only
190+
* @param {import('@appium/types').Cookie} cookie
191+
* @returns {Promise<void>}
185192
*/
186193
export async function setCookie(cookie) {
187194
if (!this.isWebContext()) {
188195
throw new errors.NotImplementedError();
189196
}
190197

191-
cookie = _.clone(cookie);
192-
198+
const clonedCookie = _.clone(cookie);
193199
// if `path` field is not specified, Safari will not update cookies as expected; eg issue #1708
194-
if (!cookie.path) {
195-
cookie.path = '/';
200+
if (!clonedCookie.path) {
201+
clonedCookie.path = '/';
196202
}
197-
198-
const jsCookie = cookieUtils.createJSCookie(cookie.name, cookie.value, {
199-
expires: _.isNumber(cookie.expiry)
200-
? new Date(cookie.expiry * 1000).toUTCString()
201-
: cookie.expiry,
202-
path: cookie.path,
203-
domain: cookie.domain,
204-
httpOnly: cookie.httpOnly,
205-
secure: cookie.secure,
203+
const jsCookie = createJSCookie(clonedCookie.name, clonedCookie.value, {
204+
expires: _.isNumber(clonedCookie.expiry)
205+
? new Date(clonedCookie.expiry * 1000).toUTCString()
206+
: clonedCookie.expiry,
207+
path: clonedCookie.path,
208+
domain: clonedCookie.domain,
209+
httpOnly: clonedCookie.httpOnly,
210+
secure: clonedCookie.secure,
206211
});
207-
let script = `document.cookie = ${JSON.stringify(jsCookie)}`;
212+
const script = `document.cookie = ${JSON.stringify(jsCookie)}`;
208213
await this.executeAtom('execute_script', [script, []]);
209214
}
210215

211216
/**
212217
* @this {XCUITestDriver}
218+
* @param {string} cookieName
219+
* @returns {Promise<void>}
213220
* @group Mobile Web Only
214221
*/
215222
export async function deleteCookie(cookieName) {
@@ -224,32 +231,27 @@ export async function deleteCookie(cookieName) {
224231
return;
225232
}
226233

227-
await this._deleteCookie(cookie);
234+
await _deleteCookie.bind(this)(cookie);
228235
}
229236

230237
/**
231238
* @this {XCUITestDriver}
232239
* @group Mobile Web Only
240+
* @returns {Promise<void>}
233241
*/
234242
export async function deleteCookies() {
235243
if (!this.isWebContext()) {
236244
throw new errors.NotImplementedError();
237245
}
238246

239247
const cookies = await this.getCookies();
240-
await B.all(cookies.map((cookie) => this._deleteCookie(cookie)));
241-
}
242-
243-
/**
244-
* @this {XCUITestDriver}
245-
*/
246-
export async function _deleteCookie(cookie) {
247-
const url = `http${cookie.secure ? 's' : ''}://${cookie.domain}${cookie.path}`;
248-
return await (/** @type {RemoteDebugger} */ (this.remote)).deleteCookie(cookie.name, url);
248+
await B.all(cookies.map((cookie) => _deleteCookie.bind(this)(cookie)));
249249
}
250250

251251
/**
252252
* @this {XCUITestDriver}
253+
* @param {Element | string} el
254+
* @returns {Element | string}
253255
*/
254256
export function cacheWebElement(el) {
255257
if (!_.isPlainObject(el)) {
@@ -268,14 +270,16 @@ export function cacheWebElement(el) {
268270

269271
/**
270272
* @this {XCUITestDriver}
273+
* @param {any} response
274+
* @returns {any}
271275
*/
272276
export function cacheWebElements(response) {
273-
const toCached = (v) => (_.isArray(v) || _.isPlainObject(v) ? this.cacheWebElements(v) : v);
277+
const toCached = (/** @type {any} */ v) => (_.isArray(v) || _.isPlainObject(v)) ? this.cacheWebElements(v) : v;
274278

275279
if (_.isArray(response)) {
276280
return response.map(toCached);
277281
} else if (_.isPlainObject(response)) {
278-
const result = {...response, ...this.cacheWebElement(response)};
282+
const result = {...response, ...(/** @type {Element} */ (this.cacheWebElement(response)))};
279283
return _.toPairs(result).reduce((acc, [key, value]) => {
280284
acc[key] = toCached(value);
281285
return acc;
@@ -345,8 +349,13 @@ export function convertElementsForAtoms(args = []) {
345349
});
346350
}
347351

352+
/**
353+
*
354+
* @param {any} element
355+
* @returns {string | undefined}
356+
*/
348357
export function getElementId(element) {
349-
return element.ELEMENT || element[W3C_WEB_ELEMENT_IDENTIFIER];
358+
return element?.ELEMENT || element?.[W3C_WEB_ELEMENT_IDENTIFIER];
350359
}
351360

352361
/**
@@ -362,12 +371,17 @@ export function hasElementId(element) {
362371

363372
/**
364373
* @this {XCUITestDriver}
374+
* @param {string} strategy
375+
* @param {string} selector
376+
* @param {boolean} [many]
377+
* @param {Element | string | null} [ctx]
378+
* @returns {Promise<Element | Element[]>}
365379
*/
366380
export async function findWebElementOrElements(strategy, selector, many, ctx) {
367381
const contextElement = _.isNil(ctx) ? null : this.getAtomsElement(ctx);
368382
const atomName = many ? 'find_elements' : 'find_element_fragment';
369383
let element;
370-
let doFind = async () => {
384+
const doFind = async () => {
371385
element = await this.executeAtom(atomName, [strategy, selector, contextElement]);
372386
return !_.isNull(element);
373387
};
@@ -1045,6 +1059,45 @@ function isValidElementIdentifier(id) {
10451059
return true;
10461060
}
10471061

1062+
/**
1063+
* Creates a JavaScript Cookie
1064+
*
1065+
* @param {string} key
1066+
* @param {string} value
1067+
* @param {CookieOptions} [options={}]
1068+
* @returns {string}
1069+
*/
1070+
function createJSCookie(key, value, options = {}) {
1071+
return [
1072+
encodeURIComponent(key),
1073+
'=',
1074+
value,
1075+
options.expires ? `; expires=${options.expires}` : '',
1076+
options.path ? `; path=${options.path}` : '',
1077+
options.domain ? `; domain=${options.domain}` : '',
1078+
options.secure ? '; secure' : '',
1079+
].join('');
1080+
}
1081+
1082+
/**
1083+
* @this {XCUITestDriver}
1084+
* @param {import('@appium/types').Cookie} cookie
1085+
* @returns {Promise<any>}
1086+
*/
1087+
async function _deleteCookie(cookie) {
1088+
const url = `http${cookie.secure ? 's' : ''}://${cookie.domain}${cookie.path}`;
1089+
return await (/** @type {RemoteDebugger} */ (this.remote)).deleteCookie(cookie.name, url);
1090+
}
1091+
1092+
/**
1093+
* @typedef {Object} CookieOptions
1094+
* @property {string} [expires]
1095+
* @property {string} [path]
1096+
* @property {string} [domain]
1097+
* @property {boolean} [secure]
1098+
* @property {boolean} [httpOnly]
1099+
*/
1100+
10481101
/**
10491102
* @typedef {import('../driver').XCUITestDriver} XCUITestDriver
10501103
* @typedef {import('@appium/types').Rect} Rect

lib/cookies.js

Lines changed: 0 additions & 92 deletions
This file was deleted.

lib/driver.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2240,7 +2240,6 @@ export class XCUITestDriver extends BaseDriver {
22402240
setCookie = webCommands.setCookie;
22412241
deleteCookie = webCommands.deleteCookie;
22422242
deleteCookies = webCommands.deleteCookies;
2243-
_deleteCookie = webCommands._deleteCookie;
22442243
cacheWebElement = webCommands.cacheWebElement;
22452244
cacheWebElements = webCommands.cacheWebElements;
22462245
executeAtom = webCommands.executeAtom;

0 commit comments

Comments
 (0)