Use own javascript methods via executeScript

Hello everyone,

I am stuck and thought I will ask here as I normally find older support requests by others that help me later on. :slight_smile:

Details for my environment:
Appium Server: 2.6.0
XCUITest: 7.17.3
Appium Java Client: 9.2.3

Test device: iPhone 15, iOS 17.5.1.

The tests are written in Kotlin. Capability “autoWebview” is enabled.

We wrote a hybrid shopping app, meaning we need to switch between native and webview contexts all the time. Inside the iOS app code, we created an own JS method called getSafariId() that returns the ID of the webview context that gets returned when using driver.contextHandles.
For example when the context handle is WEBVIEW_15292.1, the according webview ID is A5E0FE7A-5B25-486C-AAD1-CEF15A5A1D8F.
The webview ID is also found in the native tree so we can compare and see which webview is the one we need to access. It’s a bit complicated.

But now to my point.
For a while now it’s not possible anymore to get the webview ID via this getSafariId() method. Running it goes in a timeout after about two minutes (which should be more than enough, normally it worked almost instantly). Using the Safari dev tools on the local Macbook works, meaning that the method itself apparently isn’t broken. The ID is still found in the native tree and the webview contexts can be found, the only issue is the ID of the webview context.

Has anyone of you ever used a method inside an app that you wrote yourself and does it still work?

PS: The error stack in the appium console looks extremely weird, but I will provide it here as well.

[RemoteDebugger] Starting to listen for JavaScript console
[RemoteDebugger] Starting to listen for network events
[XCUITestDriver@cc27 (89bafdd0)] Responding to client with driver.setContext() result: null
[HTTP] ← POST /wd/hub/session/89bafdd0-170a-4b7d-92b9-dcf637b94400/context 200 11024 ms - 14
[HTTP] → POST /wd/hub/session/89bafdd0-170a-4b7d-92b9-dcf637b94400/execute/sync
[HTTP] {“args”:,“script”:“getSafariId()”}
[XCUITestDriver@cc27 (89bafdd0)] Calling AppiumDriver.execute() with args: [“getSafariId()”,,“89bafdd0-170a-4b7d-92b9-dcf637b94400”]
[XCUITestDriver@cc27 (89bafdd0)] Executing command ‘execute’
[RemoteDebugger] Executing atom ‘execute_script’ with ‘args=[“getSafariId()”,]; frames=’
[RemoteDebugger] Executing ‘execute_script’ atom in default context
[RemoteDebugger] Sending javascript command: ‘(function(){return (function(){var e=this||self…’
[RemoteDebugger] Sending ‘_rpc_forwardSocketData:’ message to app ‘PID:15292’, page ‘1’, target ‘page-8’ (id: 34): ‘Runtime.evaluate’
[XCUITestDriver@cc27 (89bafdd0)] Matched ‘/alert/text’ to command name ‘getAlertText’
[XCUITestDriver@cc27 (89bafdd0)] Proxying [GET /alert/text] to [GET] with no body
[XCUITestDriver@cc27 (89bafdd0)] Got response with status 404: {“value”:{“error”:“no such alert”,“message”:“An attempt was made to operate on a modal dialog when one was not open”,“traceback”:“”},“sessionId”:“E89D3E9D-9C0A-466A-A796-764286472EA5”}
[W3C] Matched W3C error code ‘no such alert’ to NoSuchAlertError

Maybe try appium-xcuitest-driver/docs/reference/ at master · appium/appium-xcuitest-driver · GitHub ? It allows to receive more info about available webviews, which could help to identify each of them

1 Like

Hi @mykola-mokhnach , thank you so much for that advice! I will try this and see if it helps to resolve my problem. :slight_smile: