def test(data):
    desire_caps = {}
    desire_caps["platformName"] = data["platformName"]
    desire_caps["platformVersion"] = data["platformVersion"]
    desire_caps["deviceName"] = data["deviceName"]
    if data["platformName"] == "iOS":
        desire_caps["startIWDP"] = True
        desire_caps["automationName"] = "XCUITest"
        desire_caps["bundleId"] = "cn.btclass.sierra"
        desire_caps["xcodeSigningId"] = "iPhone Developer"
        desire_caps["xcodeOrgId"] = "test"
        desire_caps["autoAcceptAlerts"] = True   # appium 自动处理系统弹出的警告
        desire_caps["wdaLocalPort"] = data["wdaLocalPort"]   # iOS  wda代理端口设置(用于控制多机测试)
    desire_caps["udid"] = data["udid"]
    desire_caps["noReset"] = True
    desire_caps['recreateChromeDriverSessions'] = True   # 如果driver 切换到原生的context 下就关闭webdriver
    # desire_caps["autoWebview"] = True   # 直接自动切换到webview 上下文
    desire_caps['systemPort'] = data["systemPort"]


    desire_caps["newCommandTimeout"] = 72000
    driver = webdriver.Remote('http://'+str(data['ip'])+':'+str(data['port'])+'/wd/hub',desire_caps)
    return driver

Appium 日志

[debug] [35m[RemoteDebugger][39m Page element JSON: [{"devtoolsFrontendUrl":"/devtools/devtools.html?ws=localhost:27753/devtools/page/1","faviconUrl":"","thumbnailUrl":"/thumb/","title":"JSContext","url":"","webSocketDebuggerUrl":"ws://localhost:27753/devtools/page/1","appId":"PID:793"},{"devtoolsFrontendUrl":"/devtools/devtools.html?ws=localhost:27753/devtools/page/2","faviconUrl":"","thumbnailUrl":"/thumb/https://whale.btclass.net/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEwMDAwMDksImRlYWRsaW5lIjoxNTY5NzM0NzI5LCJyYW5kb20iOiJvZmQ4em9jZWJ2aSIsImlhdCI6MTU2OTcyNzUyOX0.fF69Umw6howudnJEfRXyrDApxwW2ZynoTALWZ79a7qg&userId=1000009","title":"","url":"https://whale.btclass.net/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEwMDAwMDksImRlYWRsaW5lIjoxNTY5NzM0NzI5LCJyYW5kb20iOiJvZmQ4em9jZWJ2aSIsImlhdCI6MTU2OTcyNzUyOX0.fF69Umw6howudnJEfRXyrDApxwW2ZynoTALWZ79a7qg&userId=1000009","webSocketDebuggerUrl":"ws://localhost:27753/devtools/page/2","appId":"PID:793"}]
[debug] [35m[W3C (7b76c43e)][39m Responding to client with driver.getContexts() result: ["NATIVE_APP","WEBVIEW_2"]
[35m[HTTP][39m [37m<-- GET /wd/hub/session/7b76c43e-0352-42fa-817c-1f59da60a587/contexts [39m[32m200[39m [90m10 ms - 36[39m
[35m[HTTP][39m [90m[39m
[35m[HTTP][39m [37m-->[39m [37mPOST[39m [37m/wd/hub/session/7b76c43e-0352-42fa-817c-1f59da60a587/context[39m
[35m[HTTP][39m [90m{"name":"NATIVE_APP","sessionId":"7b76c43e-0352-42fa-817c-1f59da60a587"}[39m
[debug] [35m[W3C (7b76c43e)][39m Calling AppiumDriver.setContext() with args: ["NATIVE_APP","7b76c43e-0352-42fa-817c-1f59da60a587"]
[debug] [35m[XCUITest][39m Executing command 'setContext'
[debug] [35m[iOS][39m Attempting to set context to 'NATIVE_APP'
[debug] [35m[W3C (7b76c43e)][39m Responding to client with driver.setContext() result: null
[35m[HTTP][39m [37m<-- POST /wd/hub/session/7b76c43e-0352-42fa-817c-1f59da60a587/context [39m[32m200[39m [90m4 ms - 14[39m
[35m[HTTP][39m [90m[39m
[35m[HTTP][39m [37m-->[39m [37mPOST[39m [37m/wd/hub/session/7b76c43e-0352-42fa-817c-1f59da60a587/context[39m
[35m[HTTP][39m [90m{"name":"WEBVIEW_2","sessionId":"7b76c43e-0352-42fa-817c-1f59da60a587"}[39m
[debug] [35m[W3C (7b76c43e)][39m Calling AppiumDriver.setContext() with args: ["WEBVIEW_2","7b76c43e-0352-42fa-817c-1f59da60a587"]
[debug] [35m[XCUITest][39m Executing command 'setContext'
[debug] [35m[iOS][39m Attempting to set context to 'WEBVIEW_2'
[debug] [35m[RemoteDebugger][39m WebKit debugger web socket connected to url: ws://localhost:27753/devtools/page/2
[debug] [35m[RemoteDebugger][39m Starting to listen for JavaScript console
[debug] [35m[RemoteDebugger][39m Sending WebKit data: {"method":"Console.enable","params":{"objectGroup":"console","includeCommandLineAPI":true,"doNotPauseOnExceptionsAndMuteConsole":true}}
[debug] [35m[RemoteDebugger][39m Webkit response timeout: 5000
[debug] [35m[RemoteDebugger][39m Received WebKit data: '{"method":"Target.targetCreated","params":{"targetInfo":{"targetId":"page-1","type":"page"}}}'
[debug] [35m[RemoteDebugger][39m Found method 'Target.targetCreated'
[debug] [35m[RemoteDebugger][39m There is no handler scheduled for method 'Target.targetCreated' in the recent message
[debug] [35m[RemoteDebugger][39m Received WebKit data: '{"error":{"code":-32601,"message":"'Console' domain was not found","data":[{"code":-32601,"message":"'Console' domain was not found"}]},"id":1}'
[debug] [35m[RemoteDebugger][39m Found method 'Console.enable' for '1' message
[debug] [35m[W3C (7b76c43e)][39m Encountered internal error running command: 'Console' domain was not found
[35m[HTTP][39m [37m<-- POST /wd/hub/session/7b76c43e-0352-42fa-817c-1f59da60a587/context [39m[31m500[39m [90m50 ms - 611[39m


↙↙↙阅读原文可查看相关链接,并与作者交流