appium / appium-uiautomator2-driver

Appium driver for Android UIAutomator2
Apache License 2.0
607 stars 178 forks source link

UIAutomator2 getContexts() command does not return webview contexts on Mac M3 Pro laptop #832

Closed harveykwan9 closed 1 month ago

harveykwan9 commented 1 month ago

Hi!

I am unable to use Appium with UIAutomator2 (we tried multiple versions) with an android emulator on a Apple Mac M3 Pro laptop. On an M2 Pro laptop it works fine with the same package versions (Appium, UIAutomator2, Android Emulator OS API version)


Versions used:

npx appium --version
2.12.0

npx appium driver ls
✔ Listing available drivers
- xcuitest@7.28.1 [installed (npm)]
- uiautomator2@3.8.0 [installed (npm)]

Prerequisites:

Steps to reproduce:

  1. create a new project with package.json as so:
    {
    "devDependencies": {
        "appium": "^2.12.0",
        "appium-uiautomator2-driver": "^3.8.0",
        "appium-xcuitest-driver": "^7.28.1"
    }
    }
  2. install packages npm i
  3. start appium server npx appium
  4. start android emulator (with API level 34 from Android Studio)
  5. open appium inspector and create and start session with following json for the demo app (update the "appium:app" path)
    {
    "platformName": "Android",
    "appium:deviceName": "Android",
    "appium:automationName": "UiAutomator2",
    "appium:disableIdLocatorAutocompletion": true,
    "appium:app": "/Users/.../apps/android.wdio.native.app.v1.0.8.apk",
    "appium:udid": "emulator-5554"
    }
  6. In the demo app click 'Webview' on the bottom navigation bar
  7. In appium inspector click Refresh Source & Screenshot -> click Commands at the top navigation -> click Context dropdown -> click getContexts
  8. Verify results

Expected results:

Actual results:


Screenshot of it working on a M2 Pro chip:

Screenshot 2024-10-23 at 9 12 17 AM

Screenshot of it not working on a M3 Pro chip:

Screenshot 2024-10-24 at 11 08 07 AM

Attached is a log of the getContexts() appium command execution with an error while executing the following command getContext-log.txt

[09e98ec2][ADB] Running '/Users//Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 forward tcp:10900 localabstract:webview_devtools_remote_6790'
KazuCocoa commented 1 month ago

Did you run tinyproxy? Then, could you try out without the proxy runing? From your attached log:

    data: '<?xml version="1.0" encoding="UTF-8" ?>\n' +
      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n' +
      '\n' +
      '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n' +
      '\n' +
      '<head>\n' +
      '<title>500 Unable to connect</title>\n' +
      '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n' +
      '</head>\n' +
      '\n' +
      '<body>\n' +
      '\n' +
      '<h1>Unable to connect</h1>\n' +
      '\n' +
      '<p>Tinyproxy was unable to connect to the remote web server.</p>\n' +
      '\n' +
      '<hr />\n' +
      '\n' +
      '<p><em>Generated by <a href="https://tinyproxy.github.io/">tinyproxy</a> version 1.10.0.</em></p>\n' +
      '\n' +
      '</body>\n' +
      '\n' +
      '</html>\n'
  },
  status: 500

My Apple M3 with macOS 14.5 (23F79) had no issue as below. I don't have any proxies.

[7afdb9d1][HTTP] --> GET /session/7afdb9d1-8f9a-4879-a5aa-c4c85f9368b5/contexts {}
[7afdb9d1][AndroidUiautomator2Driver@ca63] Calling AppiumDriver.getContexts() with args: ["7afdb9d1-8f9a-4879-a5aa-c4c85f9368b5"]
[7afdb9d1][AndroidUiautomator2Driver@ca63] Getting a list of available webviews
[7afdb9d1][ADB] Running '/Users/kazu/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell cat /proc/net/unix'
[7afdb9d1][AndroidUiautomator2Driver@ca63] Parsed 1 active devtools socket: ["@webview_devtools_remote_7709"]
[7afdb9d1][AndroidUiautomator2Driver@ca63] Collecting CDP data of 1 webview
[7afdb9d1][AndroidUiautomator2Driver@ca63] Forwarding remote port webview_devtools_remote_7709 to a local port in range 10900..11000
[7afdb9d1][AndroidUiautomator2Driver@ca63] You could use the 'webviewDevtoolsPort' capability to customize the starting port number
[7afdb9d1][ADB] Running '/Users/kazu/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 forward tcp:10900 localabstract:webview_devtools_remote_7709'
[7afdb9d1][ADB] Removing forwarded port socket connection: 10900
[7afdb9d1][ADB] Running '/Users/kazu/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 forward --remove tcp:10900'
[7afdb9d1][AndroidUiautomator2Driver@ca63] CDP data collection completed
[7afdb9d1][AndroidUiautomator2Driver@ca63] WEBVIEW_7709 mapped to pid 7709
[7afdb9d1][AndroidUiautomator2Driver@ca63] Getting process name for webview 'WEBVIEW_7709'
[7afdb9d1][ADB] Running '/Users/kazu/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell ps --help'
[7afdb9d1][ADB] Running '/Users/kazu/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell ps -A'
[7afdb9d1][AndroidUiautomator2Driver@ca63] Got process name: 'io.appium.android.apis'
[7afdb9d1][AndroidUiautomator2Driver@ca63] Found 1 webview: ["WEBVIEW_io.appium.android.apis"]
[7afdb9d1][AndroidUiautomator2Driver@ca63] Available contexts: ["NATIVE_APP","WEBVIEW_io.appium.android.apis"]
[7afdb9d1][AndroidUiautomator2Driver@ca63] Responding to client with driver.getContexts() result: ["NATIVE_APP","WEBVIEW_io.appium.android.apis"]
[7afdb9d1][HTTP] <-- GET /session/7afdb9d1-8f9a-4879-a5aa-c4c85f9368b5/contexts 200 525 ms - 57
harveykwan9 commented 1 month ago

I had never run tinproxy, and did not have it installed. It's great to know that it is working with your computer, just need to figure out whats going on with mine right now 😢

harveykwan9 commented 1 month ago

Hey! was able to figure out the issue. It had to do with my environment variables.

I had export HTTP_PROXY={...} set in my .zprofile which for some reason was impacting the request which UIAutomator does to 127.0.0.1 to interact or connect to webviews.

The fix for me was to remove the line export HTTP_PROXY={...} or to add export NO_PROXY=127.0.0.1 to my .zprofile

mykola-mokhnach commented 1 month ago

I assume it is an axios-specific feature. See, for example, https://www.scrapingbee.com/blog/nodejs-axios-proxy/ -> Setting a proxy via environment variables. Not sure if we should disable this feature explicitly in the wdproxy code @KazuCocoa @jlipps

KazuCocoa commented 1 month ago

Maybe then env var also could work for it instead of controlling it by Appium internally? https://github.com/axios/axios/blob/cda671af81ca87a00c6bf5a07bffcb07468756d3/README.md?plain=1#L530-L540

harveykwan9 commented 4 weeks ago

If any help is needed with this please let me know!