question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Appium hangs when listing context in iOS

See original GitHub issue

The problem

Appium sporadically hangs when an appium client lists contexts, the application under test has WebViews and is in the background. That can happen when you have a test that combines application and Safari interactions.

Environment

  • Appium version (or git revision) that exhibits the issue: 1.16.0 beta 1
  • Node.js version (unless using Appium.app|exe): 12.12.0
  • Npm or Yarn package manager: npm
  • Mobile platform/version under test: iOS
  • Real device or emulator/simulator: real

Details

I have debugged the problem and tracked the call flow:

  • RemoteDebugger.searchForApp (appium-remote-debugger)
  • for each app registered previously:
    • RpcClient.selectApp (appium-remote-debugger)
    • RpcClient.send(‘connectToApp’, …) (appium-remote-debugger)
    • RpcClient.sendToDevice(‘connectToApp’, …) (appium-remote-debugger)
    • RpcClientRealDevice.sendMessage(…) (appium-remote-debugger)
    • WebInspectorService.sendMessage(…) (appium-ios-device)

The call to RpcClient.sendToDevice is blocked until WebInspectorService receives the response but that response never arrives. If I put the application in the foreground manually while Appium is blocked, the response from WebInspectorService arrives and Appium unblocks. You can see this behaviour in the logs:

  • At the time 11:39:03:577 the Appium hangs after the ‘Sending message to Web Inspector:’ log.
  • At 11:39:49:560, I sent the application to foreground in the iPhone (by hand) and Appium receives Web Inspector response (log ‘Received message from Web Inspector:’). If I don’t open the App, getContext request ends with a timeout and Appium hangs.

It seems that the component that is blocking the flow is the com.apple.webinspector. I don’t know anything about Web Inspector protocol and if it is normal behaviour or it is a bug in the Apple component.

It could be related with https://github.com/appium/appium/issues/13361

Link to Appium logs

Appium logs: https://gist.github.com/anorimaki/bf3e57aa3dfe20e0e15aed32d28e7849

Code To Reproduce Issue

This kotlin client code can sporadically reproduce the problem (I ommit the creation of the driver). Hangs happens with the last driver.contextHandles.

    @Test
    fun testIOSListContextAppInBackGroundSuspended() {
        val nContext = driver.contextHandles
        driver.runAppInBackground(Duration.ofSeconds(-1))
        val state = driver.queryAppState( device.platform.applicationId )
        assertEquals( ApplicationState.RUNNING_IN_BACKGROUND_SUSPENDED, state )
        assertEquals( nContext, driver.contextHandles )
    }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
imurchiecommented, Nov 18, 2019

I have published, in the latest Appium beta, the results of all of my experimentation. For my tests things are much better, hopefully for others as well.

  • appium-xcuitest-driver should be at least v3.6.0
  • appium-ios-device should be at least v1.2.1
  • appium-remote-debugger should be at least v6.7.0
1reaction
anorimakicommented, Nov 11, 2019

I have solved my problems by patching some functions of remote-debugger.js (appium-remote-debugger).

  • RemoteDebugger.searchForApp function to skip applications that aren’t active. With this change Appium doesn’t hangs.
for (const attemptedAppIdKey of possibleAppIds) {
try {
  if (this.appDict[attemptedAppIdKey].isActive === 0) {           //Added
    log.debug(`Skipping app ${attemptedAppIdKey} because isn't active`);    //Added
    continue;                                          //Added
  }                                                       //Added
  log.debug(`Selecting app ${attemptedAppIdKey} (try #${i + 1} of ${maxTries})`);
  • RemoteDebugger.selectApp doesn’t returns pages that belongs to applications that aren’t active.
for (const [app, info] of _.toPairs(this.appDict)) {
  if (!_.isArray(info.pageArray) ||
	 (info.isActive === 0)) {		//Added
    continue;
  }
  • RemoteDebugger.searchForPage skips applications that aren’t active
for (const appDict of _.values(appsDict)) {
  if (!appDict || appDict.isActive === 0 /* Added */ || !appDict.pageArray || appDict.pageArray.promise) {
	continue;
  }

I have used current beta without the branch appium-xcuitest-driver.git\#isaac-rd-try. If I use this branch, Appium (with the above patch) can list context when app is in background, but fails to interact with the application WebView (in foreground). Sometimes it hangs when switching to WebView context and other times it hangs when searching for an element in the WebView.

I think it isn’t a general solution because applications that are in background (suspended) have the same behaviuor as not running applications. In my case, it works because I always check pages that are in the foreground but I don’t know in Appium allowed to do thinks with background WebViews.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Appium command hangs when sending requests to Safari ...
Hi, After a successful switch to webview on Safari in iOS devices, the command hangs after the first command in the current page....
Read more >
Switching (and even getting list of) contexts crashes Appium
I need to input email|pass and click 'login' in webview. And here comes the trouble. Calling driver.getContextHandles(); (to find the webview ...
Read more >
Different context name for webview in Hybrid Android App test ...
While testing Hybrid android app in Appium, a change of context from NATIVE to WEBVIEW is expected. But, the name of the WEBVIEW...
Read more >
Test Configuration Options - Sauce Labs Documentation
Appium Capabilities: Required for tests using Appium on mobile apps and ... Supplying the list ["files/*_log", "*crash*"] to an iOS app test ...
Read more >
Mobile Automation Testing using WebdriverIO - YouTube
testautomation # appium #webdriverio Get started with Mobile Automation ... Different strategies to find elements for both iOS & Android apps ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found