Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cookies regression: send Page commands for cookies handling #1534

Merged
merged 2 commits into from
Apr 1, 2023

Conversation

KazuCocoa
Copy link
Member

@KazuCocoa KazuCocoa commented Mar 30, 2023

Using this.remote for cookies handling (except for setCookies) is current intentional behavior instead of JavaScript call. (https://gist.github.com/KazuCocoa/b935fbca6efa1abf4688cc94bc14366a for reference)

#1532

This change will send _rpc_forwardSocketData with Page. commands.

[HTTP] --> GET /session/8cec8681-efd8-4b28-a207-393fcb59d679/cookie
[HTTP] {}
[debug] [XCUITestDriver@17d4 (8cec8681)] Calling AppiumDriver.getCookies() with args: ["8cec8681-efd8-4b28-a207-393fcb59d679"]
[debug] [XCUITestDriver@17d4 (8cec8681)] Executing command 'getCookies'
[debug] [RemoteDebugger] Getting cookies
[debug] [RemoteDebugger] Sending '_rpc_forwardSocketData:' message to app 'PID:57553', page '1', target 'page-7' (id: 38): 'Page.getCookies'
[debug] [RemoteDebugger] Received data response from send (id: 38): '{"cookies":[]}'
[debug] [RemoteDebugger] Sending to Web Inspector took 34ms
[debug] [XCUITestDriver@17d4 (8cec8681)] Responding to client with driver.getCookies() result: []
[HTTP] <-- GET /session/8cec8681-efd8-4b28-a207-393fcb59d679/cookie 200 39 ms - 12
[HTTP]
[HTTP] --> DELETE /session/8cec8681-efd8-4b28-a207-393fcb59d679/cookie
[HTTP] {}
[debug] [XCUITestDriver@17d4 (8cec8681)] Calling AppiumDriver.deleteCookies() with args: ["8cec8681-efd8-4b28-a207-393fcb59d679"]
[debug] [XCUITestDriver@17d4 (8cec8681)] Executing command 'deleteCookies'
[debug] [RemoteDebugger] Getting cookies
[debug] [RemoteDebugger] Sending '_rpc_forwardSocketData:' message to app 'PID:57553', page '1', target 'page-7' (id: 40): 'Page.getCookies'
[debug] [RemoteDebugger] Received data response from send (id: 40): '{"cookies":[]}'
[debug] [RemoteDebugger] Sending to Web Inspector took 4ms
[debug] [XCUITestDriver@17d4 (8cec8681)] Responding to client with driver.deleteCookies() result: true
[HTTP] <-- DELETE /session/8cec8681-efd8-4b28-a207-393fcb59d679/cookie 200 7 ms - 14
[HTTP]
[HTTP] --> DELETE /session/8cec8681-efd8-4b28-a207-393fcb59d679/cookie/name
[HTTP] {}
[debug] [XCUITestDriver@17d4 (8cec8681)] Calling AppiumDriver.deleteCookie() with args: ["name","8cec8681-efd8-4b28-a207-393fcb59d679"]
[debug] [XCUITestDriver@17d4 (8cec8681)] Executing command 'deleteCookie'
[debug] [RemoteDebugger] Getting cookies
[debug] [RemoteDebugger] Sending '_rpc_forwardSocketData:' message to app 'PID:57553', page '1', target 'page-7' (id: 42): 'Page.getCookies'
[debug] [RemoteDebugger] Received data response from send (id: 42): '{"cookies":[]}'
[debug] [RemoteDebugger] Sending to Web Inspector took 31ms
[debug] [XCUITestDriver@17d4 (8cec8681)] Cookie 'name' not found. Ignoring.
[debug] [XCUITestDriver@17d4 (8cec8681)] Responding to client with driver.deleteCookie() result: true
[HTTP] <-- DELETE /session/8cec8681-efd8-4b28-a207-393fcb59d679/cookie/name 200 34 ms - 14

After setting cookies once with driver.manage.add_cookie(name: "key", value: "value")

[HTTP] --> GET /session/827a81cb-0306-4706-ad0a-ad4d1dad256d/cookie
[HTTP] {}
[debug] [XCUITestDriver@8956 (827a81cb)] Calling AppiumDriver.getCookies() with args: ["827a81cb-0306-4706-ad0a-ad4d1dad256d"]
[debug] [XCUITestDriver@8956 (827a81cb)] Executing command 'getCookies'
[debug] [RemoteDebugger] Getting cookies
[debug] [RemoteDebugger] Sending '_rpc_forwardSocketData:' message to app 'PID:58046', page '1', target 'page-7' (id: 40): 'Page.getCookies'
[debug] [RemoteDebugger] Received data response from send (id: 40): '{"cookies":[{"name":"key","value":"value","domain":"0.0.0.0","path":"/","expires":0,"session":true,"httpOnly":false,"secure":false,"sameSite":"None"}]}'
[debug] [RemoteDebugger] Sending to Web Inspector took 37ms
[debug] [XCUITestDriver@8956 (827a81cb)] Responding to client with driver.getCookies() result: [{"name":"key","value":"value","domain":"0.0.0.0","path":"/","expires":0,"session":true,"httpOnly":false,"secure":false,"sameSite":"None"}]
[HTTP] <-- GET /session/827a81cb-0306-4706-ad0a-ad4d1dad256d/cookie 200 42 ms - 149
[HTTP]
[HTTP] --> DELETE /session/827a81cb-0306-4706-ad0a-ad4d1dad256d/cookie
[HTTP] {}
[debug] [XCUITestDriver@8956 (827a81cb)] Calling AppiumDriver.deleteCookies() with args: ["827a81cb-0306-4706-ad0a-ad4d1dad256d"]
[debug] [XCUITestDriver@8956 (827a81cb)] Executing command 'deleteCookies'
[debug] [RemoteDebugger] Getting cookies
[debug] [RemoteDebugger] Sending '_rpc_forwardSocketData:' message to app 'PID:58046', page '1', target 'page-7' (id: 42): 'Page.getCookies'
[debug] [RemoteDebugger] Received data response from send (id: 42): '{"cookies":[{"name":"key","value":"value","domain":"0.0.0.0","path":"/","expires":0,"session":true,"httpOnly":false,"secure":false,"sameSite":"None"}]}'
[debug] [RemoteDebugger] Sending to Web Inspector took 40ms
[debug] [RemoteDebugger] Deleting cookie 'key' on 'http://0.0.0.0/'
[debug] [RemoteDebugger] Sending '_rpc_forwardSocketData:' message to app 'PID:58046', page '1', target 'page-7' (id: 44): 'Page.deleteCookie'
[debug] [RemoteDebugger] Received data response from send (id: 44): '{}'
[debug] [RemoteDebugger] Sending to Web Inspector took 4ms
[debug] [XCUITestDriver@8956 (827a81cb)] Responding to client with driver.deleteCookies() result: true
[HTTP] <-- DELETE /session/827a81cb-0306-4706-ad0a-ad4d1dad256d/cookie 200 48 ms - 14
[HTTP]
[HTTP] --> DELETE /session/827a81cb-0306-4706-ad0a-ad4d1dad256d/cookie/name
[HTTP] {}
[debug] [XCUITestDriver@8956 (827a81cb)] Calling AppiumDriver.deleteCookie() with args: ["name","827a81cb-0306-4706-ad0a-ad4d1dad256d"]
[debug] [XCUITestDriver@8956 (827a81cb)] Executing command 'deleteCookie'
[debug] [RemoteDebugger] Getting cookies
[debug] [RemoteDebugger] Sending '_rpc_forwardSocketData:' message to app 'PID:58046', page '1', target 'page-7' (id: 46): 'Page.getCookies'
[debug] [RemoteDebugger] Received data response from send (id: 46): '{"cookies":[]}'
[debug] [RemoteDebugger] Sending to Web Inspector took 4ms
[debug] [XCUITestDriver@8956 (827a81cb)] Cookie 'name' not found. Ignoring.
[debug] [XCUITestDriver@8956 (827a81cb)] Responding to client with driver.deleteCookie() result: true
[HTTP] <-- DELETE /session/827a81cb-0306-4706-ad0a-ad4d1dad256d/cookie/name 200 10 ms - 14

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: []};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what remote.getCookies() returns if no cookies found?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{"cookies":[]} in my test, but not sure if it has a case that could return null for example since I haven't tested old versions.

This PR is from the previous deleted code as-is not to change anything right now. (Refactoring would be a separate PR)

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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we could destruct the object above, like const {cookies} =....

});

// zip cookies with decoded value, removing undefined cookie values
return _.zip(cookies.cookies, decodedCookieValues)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may decode and return in a single chain. I don't see a need for the intermediate decodedCookieValues variable

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be cookies.find(({name}) => name === cookieName)

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be always return true?

}
const cookies = await this.getCookies();
for (const cookie of cookies) {
await this._deleteCookie(cookie);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may we use B.all here to speed up the process?

}
const cookies = await this.getCookies();
for (const cookie of cookies) {
await this._deleteCookie(cookie);
}
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we always return true?

@KazuCocoa
Copy link
Member Author

KazuCocoa commented Mar 30, 2023

I'll do refactoring (for comments as well) in #1538 as a followup pr

@boneskull
Copy link
Contributor

@KazuCocoa I don't understand if this PR is a verbatim revert of a previous changeset, and if so, which one?

@KazuCocoa
Copy link
Member Author

KazuCocoa commented Mar 30, 2023

This is for #1484 (comment). Cookies handling except for setCookies is via remote debugger's Page commands directly instead of Runtime.evaluate, ordinally, afaik. https://gist.github.com/KazuCocoa/b935fbca6efa1abf4688cc94bc14366a is the comparison in the comment.

I have checked the commit history as well, then noticed maybe something regression potentially happened in appium-ios-driver migration after Appium 1.22.3 if your debugging the mixin resolved wrongly. (I mean Runtime.evaluate was alive at that time, then, it was a regression as cookies)
XCUITest 4.19.1 was via the remote debugger's Page, so potentially unexpected regression for cookies handling existed between Appium 1.22.3 and XCUITest 4.19.1 or around. (I haven't done git bisect)

@KazuCocoa KazuCocoa changed the title chore: send Page commands for cookies handling fix: cookies regression: send Page commands for cookies handling Mar 31, 2023
@KazuCocoa
Copy link
Member Author

let me tweak the review in #1538 and merge this to keep the module behavior via Page command at this time

@KazuCocoa KazuCocoa merged commit 908ed1a into master Apr 1, 2023
@KazuCocoa KazuCocoa deleted the kazucocoa/cookies branch April 1, 2023 02:59
github-actions bot pushed a commit that referenced this pull request Apr 1, 2023
## [4.21.3](v4.21.2...v4.21.3) (2023-04-01)

### Bug Fixes

* cookies regression: send Page commands for cookies handling ([#1534](#1534)) ([908ed1a](908ed1a))
@github-actions
Copy link
Contributor

github-actions bot commented Apr 1, 2023

🎉 This PR is included in version 4.21.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@boneskull
Copy link
Contributor

@KazuCocoa The return true from deleteCookie violates the type defined in ExternalDriver. Did you need this for something? I don't see it used anywhere.

@KazuCocoa
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants