脚本如下:
Set contextNames = appiumDriver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName); // 用于返回被测 app 是 NATIVE_APP 还是 WEBVIEW,如果两者都有就是混合型 App
}
appiumDriver.context("WEBVIEW_com.nonobank");
System.out.println(appiumDriver.getContext());
objectRepository.sleep(5000);
logger.info("输入借款金额...");
WebElement element = webObjectRepository.getWebElement("借款金额");
element.sendKeys(borrowMoney);
日志记录:
[debug] [AndroidDriver] Parsed pid: 8273 pkg: com.nonobank!
[debug] [AndroidDriver] from: u0_a360,8273,755,1994200,221004,SyS_epoll_,0000000000,S,com.nonobank
[debug] [AndroidDriver] returning process name: com.nonobank
[debug] [AndroidDriver] Found webviews: ["WEBVIEW_com.nonobank"]
[debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_com.nonobank"]
[MJSONWP] Responding to client with driver.getContexts() result: ["NATIVE_APP","WEBVIEW_com....
[HTTP] <-- GET /wd/hub/session/4b9e9774-f4ea-44d5-b559-02f02420d2b5/contexts 200 212 ms - 109
[HTTP] --> POST /wd/hub/session/4b9e9774-f4ea-44d5-b559-02f02420d2b5/context {"name":"WEBVIEW_com.nonobank"}
[MJSONWP] Calling AppiumDriver.setContext() with args: ["WEBVIEW_com.nonobank","4b...
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/user/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s","3ad67487","shell","cat","/proc/net/unix"]
[debug] [AndroidDriver] WEBVIEW_8273 mapped to pid 8273
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /Users/user/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s","3ad67487","shell","ps"]
[debug] [AndroidDriver] Parsed pid: 8273 pkg: com.nonobank!
[debug] [AndroidDriver] from: u0_a360,8273,755,1969232,211344,0000000000,R,com.nonobank
[debug] [AndroidDriver] returning process name: com.nonobank
[debug] [AndroidDriver] Found webviews: ["WEBVIEW_com.nonobank"]
[debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_com.nonobank"]
[debug] [AndroidDriver] Connecting to chrome-backed webview context 'WEBVIEW_com.nonobank'
[debug] [Chromedriver] Changed state to 'starting'
[Chromedriver] Set chromedriver binary as: /Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver
[Chromedriver] Killing any old chromedrivers, running: pkill -15 -f "/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver.*--port=9515"
[Chromedriver] No old chromedrivers seemed to exist
[Chromedriver] Spawning chromedriver with: /Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver --url-base=wd/hub --port=9515 --adb-port=5037
[Chromedriver] [STDOUT] Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 9515
Only local connections are allowed.
[JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
[JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"\",\"stat...
[JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"ch...
[JSONWP Proxy] Got response with status 200: {"sessionId":"e8e9465f21109...
[debug] [Chromedriver] Changed state to 'online'
[MJSONWP] Responding to client with driver.setContext() result: null
[HTTP] <-- POST /wd/hub/session/3de788bf-e804-4dab-9da5-c435e4f616dd/context 200 60728 ms - 76
[HTTP] --> GET /wd/hub/session/3de788bf-e804-4dab-9da5-c435e4f616dd/context {}
[MJSONWP] Calling AppiumDriver.getCurrentContext() with args: ["3de788bf-e804-4dab-9da5-c...
[MJSONWP] Responding to client with driver.getCurrentContext() result: "WEBVIEW_com.nonobank"
[HTTP] <-- GET /wd/hub/session/3de788bf-e804-4dab-9da5-c435e4f616dd/context 200 2 ms - 94
WEBVIEW_com.nonobank
2017-04-19 14:33:52 [main] INFO com.nonobank.apps.page.webView.Page_webView_emergencyBorrow - 输入借款金额...
问题:
1:System.out.println(appiumDriver.getContext()); 打印 WEBVIEW_com.nonobank 说明已经切换成功了
但是 Responding to client with driver.setContext() result: null 为什么 null 啊?