python 代码
#!/usr/bin/env python
#coding=utf-8
import unittest
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
import time

class browserAPPtest(unittest.TestCase):
def setUp(self):
self.desired_caps = {}
self.desired_caps['platformName'] = 'Android'
self.desired_caps['platformVersion'] = '6.0'
self.desired_caps['deviceName'] = 'Android Emulator'
self.desired_caps['appPackage'] = 'com.example.web'
self.desired_caps['appActivity'] = '.MainActivity'
self.desired_caps['fullReset'] = 'false'
self.desired_caps['unicodeKeyboard'] = 'True'
self.desired_caps['resetKeyboard'] = 'True'
self.desired_caps['fastReset'] = 'false'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps)

def test_APP(self):
self.url="m.baidu.com/"
self.search_words="Software Testing"
self.driver.switch_to.context("NATIVE_APP")
self.driver.find_element_by_id("com.example.web:id/url").send_keys(self.url)
time.sleep(5)
self.driver.find_element_by_id("com.example.web:id/button").click()
time.sleep(10)
self.driver.switch_to.context("WEBVIEW_com.example.web")
self.driver.find_element_by_id("index-kw").send_keys(self.search_words)
self.driver.find_element_by_id("index-bn").click()
self.assertEqual(self.driver.find_element_by_id("kw").text,self.search_words)

def tearDown(self):
self.driver.switch_to.context("NATIVE_APP")
self.driver.quit()

if name=='main':
# 构造测试集
suite=unittest.TestSuite()
suite.addTest(browserAPPtest("test_APP"))
# 运行测试集合
runner=unittest.TextTestRunner()
runner.run(suite)

当执行到:self.driver.switch_to.context("WEBVIEW_com.example.web"),报告错误信息

info: [debug] Appium session started with sessionId 392dbcf4-476a-4851-802e-8854cf8ad396
info: <-- POST /wd/hub/session 303 10952.557 ms - 74
info: --> GET /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396 {}
info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"4.4.2","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"deviceName":"Android Emulator","fastReset":false,"fullReset":false,"unicodeKeyboard":true,"chromeOptions":{"androidProcess":"com.baidu.hi"},"resetKeyboard":true,"platformVersion":"6.0","appPackage":"com.example.web","platformName":"Android","appActivity":".MainActivity"},"deviceName":"CF1365A8","fastReset":false,"fullReset":false,"unicodeKeyboard":true,"chromeOptions":{"androidProcess":"com.baidu.hi"},"resetKeyboard":true,"appPackage":"com.example.web","platformName":"Android","appActivity":".MainActivity"},"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: <-- GET /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396 200 1.536 ms - 841 {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"4.4.2","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"deviceName":"Android Emulator","fastReset":false,"fullReset":false,"unicodeKeyboard":true,"chromeOptions":{"androidProcess":"com.baidu.hi"},"resetKeyboard":true,"platformVersion":"6.0","appPackage":"com.example.web","platformName":"Android","appActivity":".MainActivity"},"deviceName":"CF1365A8","fastReset":false,"fullReset":false,"unicodeKeyboard":true,"chromeOptions":{"androidProcess":"com.baidu.hi"},"resetKeyboard":true,"appPackage":"com.example.web","platformName":"Android","appActivity":".MainActivity"},"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: --> POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/context {"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396","name":"NATIVE_APP"}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: C:\ADT\sdk\platform-tools\adb.exe -s CF1365A8 shell "cat /proc/net/unix"
info: [debug] WEBVIEW_3873 mapped to pid 3873
info: [debug] Getting process name for webview
info: [debug] executing cmd: C:\ADT\sdk\platform-tools\adb.exe -s CF1365A8 shell "ps"
info: [debug] Parsed pid: 3873 pkg: com.example.web
info: [debug] from: u0_a70,3873,153,947408,53428,ffffffff,00000000,R,com.example.web
info: [debug] returning process name: com.example.web
info: [debug] Available contexts:
info: [debug] ["WEBVIEW_com.example.web"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.example.web
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: <-- POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/context 200 617.186 ms - 76 {"status":0,"value":null,"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: --> POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/element {"using":"id","sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396","value":"com.example.web:id/url"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.example.web:id/url","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.example.web:id/url","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding com.example.web:id/url using ID with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.example.web:id/url]
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"ELEMENT":"1"},"status":0}
info: [debug] Responding to client with success: {"status":0,"value":{"ELEMENT":"1"},"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: <-- POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/element 200 60.047 ms - 87 {"status":0,"value":{"ELEMENT":"1"},"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: --> POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/element/1/value {"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396","id":"1","value":["m",".","b","a","i","d","u",".","c","o","m","/"]}
info: [debug] Pushing command to appium work queue: ["element:setText",{"elementId":"1","text":"m.baidu.com/","replace":false,"unicodeKeyboard":true}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:setText","params":{"elementId":"1","text":"m.baidu.com/","replace":false,"unicodeKeyboard":true}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: setText
info: [debug] [BOOTSTRAP] [debug] Using element passed in.
info: [debug] [BOOTSTRAP] [debug] Attempting to clear using UiObject.clearText().
info: [debug] [BOOTSTRAP] [debug] Sending plain text to element: m.baidu.com/
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: <-- POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/element/1/value 200 4795.827 ms - 76 {"status":0,"value":true,"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: --> POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/element {"using":"id","sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396","value":"com.example.web:id/button"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.example.web:id/button","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.example.web:id/button","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding com.example.web:id/button using ID with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.example.web:id/button]
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"ELEMENT":"2"},"status":0}
info: [debug] Responding to client with success: {"status":0,"value":{"ELEMENT":"2"},"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: <-- POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/element 200 43.805 ms - 87 {"status":0,"value":{"ELEMENT":"2"},"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: --> POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/element/2/click {"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396","id":"2"}
info: [debug] Pushing command to appium work queue: ["element:click",{"elementId":"2"}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"2"}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: click
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: <-- POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/element/2/click 200 3146.720 ms - 76 {"status":0,"value":true,"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396"}
info: --> POST /wd/hub/session/392dbcf4-476a-4851-802e-8854cf8ad396/context {"sessionId":"392dbcf4-476a-4851-802e-8854cf8ad396","name":"WEBVIEW_com.example.web"}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: C:\ADT\sdk\platform-tools\adb.exe -s CF1365A8 shell "cat /proc/net/unix"
info: [debug] WEBVIEW_3873 mapped to pid 3873
info: [debug] Getting process name for webview
info: [debug] executing cmd: C:\ADT\sdk\platform-tools\adb.exe -s CF1365A8 shell "ps"
info: [debug] Parsed pid: 3873 pkg: com.example.web
info: [debug] from: u0_a70,3873,153,1075332,146144,ffffffff,00000000,S,com.example.web
info: [debug] returning process name: com.example.web
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.example.web
info: [debug] ["WEBVIEW_com.example.web"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.example.web
info: [debug] Connecting to chrome-backed webview
info: Chromedriver: Changed state to 'starting'
info: Chromedriver: Set chromedriver binary as: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
info: Chromedriver: 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 %a))
info: Chromedriver: No old chromedrivers seemed to exist
info: Chromedriver: Spawning chromedriver with: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=9515
info: Chromedriver: [STDOUT] Starting ChromeDriver 2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a) on port 9515
Only local connections are allowed.
info: JSONWP Proxy: Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
info: JSONWP Proxy: Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Windows NT\",\"version\":\"10.0\"}}}"
info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.example.web","androidUseRunningApp":true,"androidProcess":"com.baidu.hi","androidDeviceSerial":"CF1365A8"}}}
info: JSONWP Proxy: Got response with status 200: {"sessionId":"d1e91e9e06bf1c2e60234e6e39295b7a","status":13,"value":{"message":"unknown error: Device CF1365A8 is not online\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d...
info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.example.web","androidUseRunningApp":true,"androidProcess":"com.baidu.hi","androidDeviceSerial":"CF1365A8"}}}
info: JSONWP Proxy: Got response with status 200: {"sessionId":"9ce2f2b9b296bc48a78c245909cffd41","status":13,"value":{"message":"unknown error: Device CF1365A8 is not online\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d...
info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.example.web","androidUseRunningApp":true,"androidProcess":"com.baidu.hi","androidDeviceSerial":"CF1365A8"}}}
info: JSONWP Proxy: Got response with status 200: {"sessionId":"57ef7594c4976833ef8cbc63b17b8fed","status":13,"value":{"message":"unknown error: Device CF1365A8 is not online\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d...
info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.example.web","androidUseRunningApp":true,"androidProcess":"com.baidu.hi","androidDeviceSerial":"CF1365A8"}}}
info: JSONWP Proxy: Got response with status 200: {"sessionId":"f2b0e40215946a728e058a19d0de8073","status":13,"value":{"message":"unknown error: Device CF1365A8 is not online\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d...
error: Chromedriver: Chromedriver exited unexpectedly with code null, signal SIGTERM
info: Chromedriver: Changed state to 'stopped'
warn: Chromedriver for context WEBVIEW_com.example.web stopped unexpectedly
error: Chromedriver: Error: An unknown server-side error occurred while processing the command. (Original error: unknown error: Device CF1365A8 is not online
(Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 10.0 x86_64))
at JWProxy.command$ (lib/proxy.js:133:15)
at tryCatch (C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as next
at GeneratorFunctionPrototype.invoke (C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at bound (domain.js:284:14)
at GeneratorFunctionPrototype.runBound (domain.js:297:12)
at run (C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\es6.promise.js:89:39)
at C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\es6.promise.js💯28
at flush (C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\$.microtask.js:17:13)
at process._tickDomainCallback (node.js:381:11)
{ [Error: An unknown server-side error occurred while processing the command. (Original error: unknown error: Device CF1365A8 is not online
(Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 10.0 x86_64))]
status: 13,
value: { message: 'unknown error: Device CF1365A8 is not online\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 10.0 x86_64)' },
httpCode: 200 }
info: [debug] Didn't get a new command in 60 secs, shutting down...
info: Shutting down appium session
info: [debug] Pressing the HOME button


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