appium / appium-mac2-driver

Next-gen Appium macOS driver, backed by Apple XCTest
Apache License 2.0
115 stars 24 forks source link

Running Mac2 driver host process in sudo mode #262

Closed lxmn1 closed 6 months ago

lxmn1 commented 6 months ago

I adapted this Python script from README,

from appium import webdriver
from appium.options.mac import Mac2Options
from appium.webdriver.common.appiumby import AppiumBy
from contextlib import contextmanager

@contextmanager
def driver():
    options = Mac2Options()   #Mac2Options sets automationName='Mac2' and platformName='Mac'
    options.bundle_id = 'com.apple.TextEdit'
    options.show_server_logs = True
    drv = webdriver.Remote('http://127.0.0.1:4723', options=options)
    yield drv
    drv.quit()

with driver() as drv:
    edit_field = driver.find_element(by=AppiumBy.CLASS_NAME, value='XCUIElementTypeTextView')
    edit_field.send_keys('hello world')
    assert edit_field.text == 'hello world'
    edit_field.clear()
    assert edit_field.text == ''

However, I receive errors logged to console related to permissions denied:

[WebDriverAgentMac] Using bootstrap root: /Users/usr/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac [WebDriverAgentMac] Using xcodebuild binary at '/usr/bin/xcodebuild' [WebDriverAgentMac] There is no need to perform the project cleanup. A fresh install has been detected [WebDriverAgentMac] Using 127.0.0.1 as server host [WebDriverAgentMac] Using port 10100 [WebDriverAgentMac] Starting Mac2Driver host process: xcodebuild build-for-testing test-without-building -project /Users/usr/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac/WebDriverAgentMac.xcodeproj -scheme WebDriverAgentRunner COMPILER_INDEX_STORE_ENABLE\=NO [WD Proxy] Matched '/status' to command name 'getStatus' [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:10100/status] with no body [WD Proxy] connect ECONNREFUSED 127.0.0.1:10100 [WebDriverAgentMac] [xcodebuild] 2023-12-12 17:09:57.446 xcodebuild[73363:2813098] DVTCoreDeviceEnabledState: DVTCoreDeviceEnabledState_Disabled set via user default (DVTEnableCoreDevice=disabled) [WebDriverAgentMac] [xcodebuild] Command line invocation: [WebDriverAgentMac] [xcodebuild] /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild build-for-testing test-without-building -project /Users/usr/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac/WebDriverAgentMac.xcodeproj -scheme WebDriverAgentRunner COMPILER_INDEX_STORE_ENABLE=NO [WebDriverAgentMac] [WebDriverAgentMac] User defaults from command line: [WebDriverAgentMac] IDEPackageSupportUseBuiltinSCM = YES [WebDriverAgentMac] [WebDriverAgentMac] Build settings from command line: [WebDriverAgentMac] COMPILER_INDEX_STORE_ENABLE = NO [WD Proxy] Matched '/status' to command name 'getStatus' [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:10100/status] with no body [WD Proxy] connect ECONNREFUSED 127.0.0.1:10100 [WebDriverAgentMac] [xcodebuild] --- xcodebuild: WARNING: Using the first of multiple matching destinations: [WebDriverAgentMac] { platform:macOS, arch:arm64, id:00006020-000A711E2E33C01E } [WebDriverAgentMac] { platform:macOS, arch:x86_64, id:00006020-000A711E2E33C01E } [WebDriverAgentMac] { platform:macOS, name:Any Mac } [WebDriverAgentMac] [xcodebuild] Computing target dependency graph and provisioning inputs [WebDriverAgentMac] [xcodebuild] Create build description [WebDriverAgentMac] [xcodebuild] Command failed with a nonzero exit code [WebDriverAgentMac] [xcodebuild] error: unexpected service error: build aborted due to an internal error: unable to write manifest to '/Users/usr/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac/build/XCBuildData/8719c7c722ada64ef9014fff22e63f69.xcbuilddata/manifest.json': mkdir(/Users/usr/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac/build, S_IRWXU | S_IRWXG | S_IRWXO): Permission denied (13) [WebDriverAgentMac] [xcodebuild] 2023-12-12 17:09:57.938 xcodebuild[73363:2813098] Writing error result bundle to /var/folders/h4/j8kvjrl93mxc27n5bw4_lv040000gp/T/ResultBundle_2023-12-12_17-09-0057.xcresult [WD Proxy] Matched '/status' to command name 'getStatus' [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:10100/status] with no body [WD Proxy] connect ECONNREFUSED 127.0.0.1:10100 [WebDriverAgentMac] [xcodebuild] xcodebuild: error: Failed writing xctestrun file: The folder “WebDriverAgentRunner_macosx13.3-arm64.xctestrun” doesn’t exist.. [WebDriverAgentMac] Mac2Driver host process has exited with code 70, signal null [WebDriverAgentMac] Mac2Driver session cannot be stopped, because the server is not running [AppiumDriver@4c4a] Event 'newSessionStarted' logged at 1702372199565 (17:09:59 GMT+0800 (Singapore Standard Time)) [AppiumDriver@4c4a] Encountered internal error running command: Error: An unknown server-side error occurred while processing the command. Original error: 'GET /status' cannot be proxied to Mac2 Driver server because its process is not running (probably crashed). Check the Appium log for more details

When I run this command xcodebuild build-for-testing test-without-building -project ~/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac/WebDriverAgentMac.xcodeproj -scheme WebDriverAgentRunner COMPILER_INDEX_STORE_ENABLE=NO in my Mac terminal, I receive an error error: unexpected service error: build aborted due to an internal error: unable to write manifest to '/Users/usr/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac/build/XCBuildData/0a67e4a1bd729f617eec2c127c38bb56.xcbuilddata/manifest.json': mkdir(/Users/usr/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac/build, S_IRWXU | S_IRWXG | S_IRWXO): Permission denied (13), but it does run successfully with sudo.

Hence, I am guessing the error from the Python script is related to insufficient permissions when passing in Mac2Options, is it possible to add to Mac2Options an option to run in sudo mode?

lxmn1 commented 6 months ago

Also, I am new to Mac2 driver. Can I check if the steps I run my script now is correct?

  1. Enabled Accessibility access for Xcode Helper
  2. Run automationmodetool enable-automationmode-without-authentication
  3. Run appium server which starts appium on http://127.0.0.1:4723/
  4. Run python3 script.py for above script
lxmn1 commented 6 months ago

Update: I was indeed able to get it to work by creating the build folder in /Users/usr/.appium/node_modules/appium-mac2-driver/WebDriverAgentMac manually

KazuCocoa commented 6 months ago

Did you install appium with sudo etc? I wondered if your node env affected the configuration. For example if you managed node with nvm with user permission only, the permission issue won't happen.