环境:
Appium:1.2.0.1
设备 Android 版本: 4.4.2
设备 SDK 版本: 19

Set<String> contextNames = appDriver.getContextHandles();
for (String contextName : contextNames) {
     System.out.println(contextName+"\n");
}

打印的结果:

NATIVE_APP

WEBVIEW_com.android.browser

而接下来执行下面这句时报错了:

appDriver.context("WEBVIEW_com.android.browser");

报错如下:

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (Original error: Did not get session redirect from Chromedriver) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 61.75 seconds
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'jli', ip: '192.168.200.94', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Session ID: f0b6fa51-296f-477d-9b96-09e391ca4484
Driver info: io.appium.java_client.AppiumDriver
Capabilities [{platform=LINUX, javascriptEnabled=true, appActivity=cn.mailtech.app.activity.SplashMailtech, browserName=, desired={newCommandTimeout=600, platformVersion=4.4, deviceName=NX507J, platformName=Android, browserName=, appActivity=cn.mailtech.app.activity.SplashMailtech, appPackage=cn.mailtech.app}, appPackage=cn.mailtech.app, newCommandTimeout=600, platformVersion=4.4, databaseEnabled=false, deviceName=NX507J, platformName=Android, webStorageEnabled=false, warnings={}, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:95)
    at io.appium.java_client.AppiumDriver.context(AppiumDriver.java:567)
    at com.mailtech.selenium2.example.appium.cmappTest.ReadMail2.ReadMailtest(ReadMail2.java:113)
    at com.mailtech.selenium2.example.appium.cmappTest.ReadMail2.ReadMailTest(ReadMail2.java:305)
    at com.mailtech.selenium2.example.appium.cmappTest.ReadMail2.testReadMail(ReadMail2.java:314)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Appium 的日志如下:
有 error: Chromedriver create session did not work.

> debug: Appium request initiated at /wd/hub/session/f0b6fa51-296f-477d-9b96-09e391ca4484/contexts
> info: --> GET /wd/hub/session/f0b6fa51-296f-477d-9b96-09e391ca4484/contexts {}
> debug: Request received with params: {}
> debug: Getting a list of available webviews
> debug: executing: "D:\adt-bundle-windows-x86_64-20140321\sdk\platform-tools\adb.exe" -s NX507J shell "cat /proc/net/unix"
> debug: WEBVIEW_32633 mapped to pid 32633
> debug: Getting process name for webview
> debug: executing: "D:\adt-bundle-windows-x86_64-20140321\sdk\platform-tools\adb.exe" -s NX507J shell "ps"
> debug: Parsed pid: 32633 pkg: com.android.browser
> info: <-- GET /wd/hub/session/f0b6fa51-296f-477d-9b96-09e391ca4484/contexts 200 338.540 ms - 116 {"status":0,"value":["NATIVE_APP","WEBVIEW_com.android.browser"],"sessionId":"f0b6fa51-296f-477d-9b96-09e391ca4484"}
> debug: from: u0_a17,32633,227,966956,65272,ffffffff,00000000,S,com.android.browser
> debug: returning process name: com.android.browser
> debug: Available contexts: 
> debug: ["WEBVIEW_com.android.browser"]
> debug: Available contexts: NATIVE_APP,WEBVIEW_com.android.browser
> debug: Responding to client with success: {"status":0,"value":["NATIVE_APP","WEBVIEW_com.android.browser"],"sessionId":"f0b6fa51-296f-477d-9b96-09e391ca4484"}
> debug: Appium request initiated at /wd/hub/session/f0b6fa51-296f-477d-9b96-09e391ca4484/context
> debug: Request received with params: {"name":"WEBVIEW_com.android.browser"}
> debug: Getting a list of available webviews
> debug: executing: "D:\adt-bundle-windows-x86_64-20140321\sdk\platform-tools\adb.exe" -s NX507J shell "cat /proc/net/unix"
> debug: WEBVIEW_32633 mapped to pid 32633
> debug: Getting process name for webview
> debug: executing: "D:\adt-bundle-windows-x86_64-20140321\sdk\platform-tools\adb.exe" -s NX507J shell "ps"
> debug: Parsed pid: 32633 pkg: com.android.browser
> debug: from: u0_a17,32633,227,966956,65272,ffffffff,00000000,S,com.android.browser
> debug: returning process name: com.android.browser
> debug: Available contexts: NATIVE_APP,WEBVIEW_com.android.browser
> debug: ["WEBVIEW_com.android.browser"]
> debug: Available contexts: NATIVE_APP,WEBVIEW_com.android.browser
> debug: Connecting to chrome-backed webview
> debug: Creating Chrome session
> debug: Ensuring Chromedriver exists
> debug: Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"9515 "`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe`) do (IF NOT %b=="" TASKKILL /F /PID %b))
> debug: No old chromedrivers seemed to exist
> debug: Spawning chromedriver with: D:\AppiumForWindows\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
> debug: [CHROMEDRIVER] Starting ChromeDriver (v2.9.248315) on port 9515
> debug: Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage":"cn.mailtech.app","androidUseRunningApp":true,"androidDeviceSerial":"NX507J"}}}}
> error: Chromedriver create session did not work. Status was 200 and body was {"sessionId":"e0e91a1f48274488dba92953b66ba6b6","status":100,"value":{"message":"chrome not reachable\n  (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64)"}}
> info: <-- POST /wd/hub/session/f0b6fa51-296f-477d-9b96-09e391ca4484/context 500 61738.692 ms - 280 
> debug: Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command. (Original error: Did not get session redirect from Chromedriver)","origValue":"Did not get session redirect from Chromedriver"},"sessionId":"f0b6fa51-296f-477d-9b96-09e391ca4484"}
> info: --> DELETE /wd/hub/session/f0b6fa51-296f-477d-9b96-09e391ca4484 {}
> debug: Appium request initiated at /wd/hub/session/f0b6fa51-296f-477d-9b96-09e391ca4484
> info: Shutting down appium session

尝试:在 android 安装 chrome,但结果仍旧一样。


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