【已解决】
将:
cap.setCapability("androidProcess", "com.tencent.mm:appbrand0");
替换为:
chromeOptions.setExperimentalOption("androidProcess", "com.tencent.mm:appbrand0");

就 ok 了。

这两天在研究小程序自动化,启动小程序后切换 webview 时总是卡住,还望各位大侠赐教😢
手机 Chrome 版本:66.0.3359.126
Chromedriver 版本:2.4.0

代码如下:

public class test3 {
    @Test
    public void Test(){
        AndroidDriver driver;
        DesiredCapabilities cap =  new DesiredCapabilities();
        cap.setCapability("deviceName","edd06f8e");   //设备ID
        cap.setCapability("platformName","Android");    //测试平台
        cap.setCapability("platformVersion","6.0");     //Android版本
        cap.setCapability("appPackage","com.tencent.mm");   //APP包名
        cap.setCapability("appActivity","com.tencent.mm.ui.LauncherUI"); //ActivityName
        cap.setCapability("appWaitActivity", "com.tencent.mm.app.WeChatSplashActivity");
        cap.setCapability("newCommandTimeout","2000");
        cap.setCapability("unicodeKeyboard",true);
        cap.setCapability("resetKeyboard",true);
        cap.setCapability("noSign",true);
        cap.setCapability("noReset",true);
        cap.setCapability("recreateChromeDriverSessions", true);
        //cap.setCapability("autoWebview",true);

        File chromedriver = new File(System.getProperty("user.dir") + "/chromedriver");
        cap.setCapability(AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE,chromedriver.getAbsolutePath());

        ChromeOptions chromeOptions = new ChromeOptions();
        cap.setCapability("androidProcess", "com.tencent.mm:appbrand0");
        cap.setCapability(ChromeOptions.CAPABILITY,chromeOptions);
        cap.setBrowserName("");

        URL remoteUrl = null;
        try {
            remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }


        driver = new AndroidDriver<AndroidElement>(remoteUrl, cap);

        try{
            Thread.sleep(8000);
        }catch (InterruptedException e){

        }

        new TouchAction(driver)
                .press(PointOption.point(500,733))
                .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(3)))
                .moveTo(PointOption.point(500,1600))
                .release().perform();

        try{
            Thread.sleep(6000);
        }catch (InterruptedException e) {
        }

        driver.findElementByXPath("//*[@text='xxxx']").click();

        System.out.println("切换context");

        try{
            Thread.sleep(6000);
        }catch (InterruptedException e) {
        }


        driver.context("WEBVIEW_com.tencent.mm:appbrand0");
        //driver.findElementByXPath("/html/body/wx-view/wx-view[1]/wx-form/span/wx-button").click();
        driver.quit();
    }
}

【Appium 日志】
[debug] [W3C (c728e763)] Calling AppiumDriver.click() with args: ["1","c728e763-90c1-43fc-965c-7f82d49b87d7"]
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"element:click","params":{"elementId":"1"}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"1"}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: click
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":true}
[debug] [W3C (c728e763)] Responding to client with driver.click() result: true
[HTTP] <-- POST /wd/hub/session/c728e763-90c1-43fc-965c-7f82d49b87d7/element/1/click 200 204 ms - 14
[HTTP]
[HTTP] --> POST /wd/hub/session/c728e763-90c1-43fc-965c-7f82d49b87d7/context
[HTTP] {"name":"WEBVIEW_com.tencent.mm:appbrand0"}
[debug] [W3C (c728e763)] Calling AppiumDriver.setContext() with args: ["WEBVIEW_com.tencent.mm:appbrand0","c728e763-90c1-43fc-965c-7f82d49b87d7"]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Running '/Users/boyue/Library/Android/sdk/platform-tools/adb -P 5037 -s edd06f8e shell cat /proc/net/unix'
[debug] [AndroidDriver] WEBVIEW_23955 mapped to pid 23955
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Running '/Users/boyue/Library/Android/sdk/platform-tools/adb -P 5037 -s edd06f8e shell ps'
[debug] [AndroidDriver] WEBVIEW_24477 mapped to pid 24477
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Running '/Users/boyue/Library/Android/sdk/platform-tools/adb -P 5037 -s edd06f8e shell ps'
[debug] [AndroidDriver] WEBVIEW_24682 mapped to pid 24682
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Running '/Users/boyue/Library/Android/sdk/platform-tools/adb -P 5037 -s edd06f8e shell ps'
[debug] [AndroidDriver] WEBVIEW_24362 mapped to pid 24362
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Running '/Users/boyue/Library/Android/sdk/platform-tools/adb -P 5037 -s edd06f8e shell ps'
[debug] [AndroidDriver] Parsed pid: '24362' pkg: 'com.tencent.mm:tools' from
[debug] [AndroidDriver] USER PID PPID VSIZE RSS WCHAN PC NAME
[debug] [AndroidDriver] u0_a813 24362 758 2437968 283012 SyS_epoll_ 0000000000 S com.tencent.mm:tools
[debug] [AndroidDriver] Returning process name: 'com.tencent.mm:tools'
[debug] [AndroidDriver] Parsed pid: '23955' pkg: 'com.tencent.mm' from
[debug] [AndroidDriver] USER PID PPID VSIZE RSS WCHAN PC NAME
[debug] [AndroidDriver] u0_a813 23955 758 2598076 327516 SyS_epoll_ 0000000000 S com.tencent.mm
[debug] [AndroidDriver] Returning process name: 'com.tencent.mm'
[debug] [AndroidDriver] Parsed pid: '24477' pkg: 'com.tencent.mm:toolsmp' from
[debug] [AndroidDriver] USER PID PPID VSIZE RSS WCHAN PC NAME
[debug] [AndroidDriver] u0_a813 24477 758 2316592 220092 SyS_epoll_ 0000000000 S com.tencent.mm:toolsmp
[debug] [AndroidDriver] Returning process name: 'com.tencent.mm:toolsmp'
[debug] [AndroidDriver] Parsed pid: '24682' pkg: 'com.tencent.mm:appbrand0' from
[debug] [AndroidDriver] USER PID PPID VSIZE RSS WCHAN PC NAME
[debug] [AndroidDriver] u0_a813 24682 758 2400204 310464 0000000000 R com.tencent.mm:appbrand0
[debug] [AndroidDriver] Returning process name: 'com.tencent.mm:appbrand0'
[debug] [AndroidDriver] Found webviews: ["WEBVIEW_com.tencent.mm","WEBVIEW_com.tencent.mm:toolsmp","WEBVIEW_com.tencent.mm:appbrand0","WEBVIEW_com.tencent.mm:tools"]
[debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_com.tencent.mm","WEBVIEW_com.tencent.mm:toolsmp","WEBVIEW_com.tencent.mm:appbrand0","WEBVIEW_com.tencent.mm:tools"]
[debug] [AndroidDriver] Connecting to chrome-backed webview context 'WEBVIEW_com.tencent.mm:appbrand0'
[debug] [AndroidDriver] A port was not given, using random free port: 8000
[AndroidDriver] Merging 'goog:chromeOptions' into 'chromeOptions'. This may cause unexpected behavior
[debug] [AndroidDriver] Before starting chromedriver, androidPackage is com.tencent.mm
[debug] [Chromedriver] Changed state to 'starting'
[Chromedriver] Set chromedriver binary as: /Users/boyue/工作/StockCircles/chromedriver
[debug] [Chromedriver] Killing any old chromedrivers, running: pkill -15 -f "/Users/boyue/工作/StockCircles/chromedriver.*--port=8000"
[Chromedriver] No old chromedrivers seem to exist
[debug] [Chromedriver] Cleaning any old adb forwarded port socket connections
[debug] [ADB] List forwarding ports
[debug] [ADB] Running '/Users/boyue/Library/Android/sdk/platform-tools/adb -P 5037 -s edd06f8e forward --list'
[Chromedriver] Spawning chromedriver with: /Users/boyue/工作/StockCircles/chromedriver --url-base=wd/hub --port=8000 --adb-port=5037 --verbose
[debug] [Chromedriver] Chromedriver version: '2.40.565386'
[debug] [WD Proxy] Matched '/status' to command name 'getStatus'
[debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8000/wd/hub/status] with no body
[debug] [WD Proxy] Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Mac OS X\",\"version\":\"10.14.3\"}}}"
[debug] [WD Proxy] Determined that the downstream protocol for proxy is MJSONWP
[debug] [WD Proxy] Matched '/session' to command name 'createSession'
[debug] [WD Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"args":[],"extensions":[],"androidDeviceSerial":"edd06f8e"},"loggingPrefs":{"browser":"ALL"}}}
[debug] [Chromedriver] Webview version: 'Chrome/66.0.3359.126'
[debug] [WD Proxy] Got response with status 200: {"sessionId":"5e88ffa8771365f3057f0d19e515b867","status":13,"value":{"message":"unknown error: unable to discover open pages\n (Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.14.3 x86_64)"}}
[WD Proxy] Got an unexpected response: {"sessionId":"5e88ffa8771365f3057f0d19e515b867","status":13,"value":{"message":"unknown error: unable to discover open pages\n (Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.14.3 x86_64)"}}
[debug] [MJSONWP] Matched JSONWP error code 13 to UnknownError
[Chromedriver] Error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. Original error: unknown error: unable to discover open pages
Chromedriver
[Chromedriver] at Object.wrappedLogger.errorAndThrow (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/logging.js:78:13)
[Chromedriver] at errorAndThrow (/usr/local/lib/node_modules/appium/node_modules/appium-chromedriver/lib/chromedriver.js:493:13)
[Chromedriver] at
[debug] [WD Proxy] Matched '/session' to command name 'createSession'
[debug] [WD Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"args":[],"extensions":[],"androidDeviceSerial":"edd06f8e"},"loggingPrefs":{"browser":"ALL"}}}
[debug] [Chromedriver] Webview version: 'Chrome/66.0.3359.126'


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