多看 app 做测试。切换到 webview 后,无论是获取 element,还是 pagesource 都无响应。
手机 4.4.4 的系统。不知道是什么原因。。日志是说,Didn't get a new command in 60 secs, shutting down...

private AppiumDriver driver;

 public void setUp() throws Exception {
     // set up appium
     DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability("deviceName","af68ec7");
     capabilities.setCapability("platformVersion", "4.4.4");
     capabilities.setCapability("appPackage", "com.duokan.reader");
     capabilities.setCapability("appActivity", ".DkMainActivity");
     driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
 }

 public void test() {
     System.out.println(driver.getContextHandles());
     driver.context("WEBVIEW_com.duokan.reader");
     driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
     System.out.println(driver.getPageSource());
}

info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.eg.android.AlipayGphone,WEBVIEW_com.sankuai.meituan,WEBVIEW_com.duokan.reader
info: [debug] ["WEBVIEW_com.duokan.reader","WEBVIEW_com.sankuai.meituan","WEBVIEW_com.eg.android.AlipayGphone"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.duokan.reader,WEBVIEW_com.sankuai.meituan,WEBVIEW_com.eg.android.AlipayGphone
info: [debug] Connecting to chrome-backed webview
info: [debug] Creating Chrome session
info: [debug] Set chromedriver binary as: C:\Program Files (x86)\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
info: [debug] Ensuring Chromedriver exists
info: [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))
info: [debug] No old chromedrivers seemed to exist
info: [debug] Spawning chromedriver with: C:\Program Files (x86)\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
info: [debug] [CHROMEDRIVER] Starting ChromeDriver 2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3) on port 9515
Only local connections are allowed.
info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.duokan.reader","androidUseRunningApp":true,"androidDeviceSerial":"af68ec7"}}}}
info: [debug] Didn't get a new command in 60 secs, shutting down...
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: C:\Users\liming\AppData\Local\Android\sdk\platform-tools\adb.exe -s af68ec7 shell "input keyevent 3"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"OK, shutting down","status":0}
info: [debug] [BOOTSTRAP] [debug] Closed client connection
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
info: [debug] [UIAUTOMATOR STDOUT] Time: 81.623
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
info: [debug] UiAutomator shut down normally
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
info: [debug] We shut down because no new commands came in


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