环境:
appium 1.4.13 chromedriver 2.18.343837 Mac 系统
问题
切换到 webview 后,查找元素失败后,会执行截图操作。但实际上,截图失败,并且测试脚本会 hang up, 要强制停止
测试用例脚本:
@user1hod_screen_args()
def test_AA(self):
invest_list =accountPage.Account(self.driver)
username = login_cls[0][2]
password = login_cls[0][3]
logger.info('begin login app')
invest_list.click_tab_me()
loginButton = invest_list.is_element_exists(By.ID,u'com.yingzt.invest:id/summit_btn')
if loginButton:
login(self,username, password)
else:
pass
invest_list.click_my_invest()
sleep(2)
context = self.driver.contexts
self.driver.switch_to.context(u'WEBVIEW_com.yingzt.invest')
invest_list.click_invest_list()
sleep(2)
hh = self.driver.window_handles
sleep(2)
self.driver.switch_to_window(hh[1])
invest_list.click_invest_contract() 这一步查找失败,触发截图
截屏装饰器
@staticmethod
def staticmethod_screen_args(*wargs, **wkargs):
def screen(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
saveScreen(MyDriver().get_driver(), func.__name__)
import sys
exc_info = sys.exc_info()
raise exc_info[0], None, exc_info[2]
return wrapper
return screen
def saveScreen(driver, name):
time.sleep(1)
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
pic_path = filepath+'/image/%s--%s.png' % (name, now)
print u'截图保存路径为:\n%s' % os.path.abspath(pic_path)
driver.get_screenshot_as_file(pic_path)
Appium 日志:
markdown 改下。。你这贴要被关小黑屋了
其实打开就想关了。。
@user1hod_screen_args()
def test_AA(self):
invest_list =accountPage.Account(self.driver)
username = login_cls[0][2]
password = login_cls[0][3]
logger.info('begin login app')
invest_list.click_tab_me()
loginButton = invest_list.is_element_exists(By.ID,u'com.yingzt.invest:id/summit_btn')
if loginButton:
login(self,username, password)
else:
pass
invest_list.click_my_invest()
sleep(2)
context = self.driver.contexts
self.driver.switch_to.context(u'WEBVIEW_com.yingzt.invest')
invest_list.click_invest_list()
sleep(2)
hh = self.driver.window_handles
sleep(2)
self.driver.switch_to_window(hh[1])
invest_list.click_invest_contract()
#4 楼 @a3096556718 代码带上 markdown 格式。。编辑框右边自己点点看
mark.
执行脚本报的啥错,这种问题多数是保存路径有问题。
你发的 Appium 日志开头就已经开始关闭 Appium 了,不知道前面的执行结果是什么
#11 楼 @sanlengjingvv 前面的截图是报截图失败的日志
代码 markdown 还是有些问题。这次帮你改了,建议你详细看下回帖框右下角的【排版说明】。
另:麻烦补充一些信息:
invest_list.click_invest_contract()
前后大约 20 行的 appium server log 。你截图给的那个 sessionid 为空的是心跳包,和脚本执行关系不大。日志的截图不要使用截图,直接贴过来,也方便别人查看,且贴过来要把从开始到结束所有的 log 贴上,截图一般很多重要信息被你遗漏了
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.6807265898678452-1/displayed[39m [90m{}[39m
[36minfo[39m: JSONWP Proxy: Proxying [GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.6807265898678452-1/displayed] to [GET http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/element/0.6807265898678452-1/displayed] with body: {}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":true}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.6807265898678452-1/displayed [39m[32m200[39m[90m 37.142 ms - 76[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mPOST[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element[39m [90m{"using":"xpath","sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","value":"//[@id='return_ing_list']/li[1]/a/div[2]/h3"}[39m
[36minfo[39m: JSONWP Proxy: Proxying [POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element] to [POST http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/element] with body: {"using":"xpath","sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","value":"//[@id='return_ing_list']/li[1]/a/div[2]/h3"}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":{"ELEMENT":"0.6807265898678452-1"}}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element [39m[32m200[39m[90m 35.615 ms - 106[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mPOST[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.6807265898678452-1/click[39m [90m{"sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","id":"0.6807265898678452-1"}[39m
[36minfo[39m: JSONWP Proxy: Proxying [POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.6807265898678452-1/click] to [POST http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/element/0.6807265898678452-1/click] with body: {"sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","id":"0.6807265898678452-1"}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":null}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.6807265898678452-1/click [39m[32m200[39m[90m 105.626 ms - 76[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/window_handles[39m [90m{}[39m
[36minfo[39m: JSONWP Proxy: Proxying [GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/window_handles] to [GET http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/window_handles] with body: {}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":["CDwindow-A76D005F-D808-467F-8CE8-73EF5F26F61C","CDwindow-5CB2CB66-6383-495D-88B8-A3879796FD26"]}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/window_handles [39m[32m200[39m[90m 12.429 ms - 169[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mPOST[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/window[39m [90m{"sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","name":"CDwindow-5CB2CB66-6383-495D-88B8-A3879796FD26"}[39m
[36minfo[39m: JSONWP Proxy: Proxying [POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/window] to [POST http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/window] with body: {"sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","name":"CDwindow-5CB2CB66-6383-495D-88B8-A3879796FD26"}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":null}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/window [39m[32m200[39m[90m 11.931 ms - 76[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mPOST[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element[39m [90m{"using":"xpath","sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","value":"/html/body/div/div[2]/a/p[1]"}[39m
[36minfo[39m: JSONWP Proxy: Proxying [POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element] to [POST http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/element] with body: {"using":"xpath","sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","value":"/html/body/div/div[2]/a/p[1]"}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":{"ELEMENT":"0.4724039069842547-1"}}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element [39m[32m200[39m[90m 158.674 ms - 106[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.4724039069842547-1/displayed[39m [90m{}[39m
[36minfo[39m: JSONWP Proxy: Proxying [GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.4724039069842547-1/displayed] to [GET http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/element/0.4724039069842547-1/displayed] with body: {}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":true}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.4724039069842547-1/displayed [39m[32m200[39m[90m 70.954 ms - 76[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mPOST[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element[39m [90m{"using":"xpath","sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","value":"/html/body/div/div[2]/a/p[1]"}[39m
[36minfo[39m: JSONWP Proxy: Proxying [POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element] to [POST http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/element] with body: {"using":"xpath","sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","value":"/html/body/div/div[2]/a/p[1]"}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":{"ELEMENT":"0.4724039069842547-1"}}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element [39m[32m200[39m[90m 70.501 ms - 106[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.4724039069842547-1/text[39m [90m{}[39m
[36minfo[39m: JSONWP Proxy: Proxying [GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.4724039069842547-1/text] to [GET http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/element/0.4724039069842547-1/text] with body: {}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":"投资成功,开始计息,已使用 0.33% 加息券"}
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: [37m<-- GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element/0.4724039069842547-1/text [39m[32m200[39m[90m 97.603 ms - 127[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot[39m [90m{}[39m
[36minfo[39m: JSONWP Proxy: Proxying [GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot] to [GET http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/screenshot] with body: {}
[36minfo[39m: [debug] Didn't get a new command in 60 secs, shutting down...
[36minfo[39m: Shutting down appium session
[36minfo[39m: [debug] Pressing the HOME button
[36minfo[39m: [debug] executing cmd: /Users/yzt_test/Documents/adt-bundle-mac-x86_64/sdk/platform-tools/adb -s 03157df3a0531722 shell "input keyevent 3"
[36minfo[39m: [debug] Stopping logcat capture
[36minfo[39m: [debug] Logcat terminated with code null, signal SIGTERM
[36minfo[39m: [debug] Stopping chromedriver for context WEBVIEW_com.yingzt.invest
[36minfo[39m: Chromedriver: Changed state to 'stopping'
[36minfo[39m: JSONWP Proxy: Proxying [DELETE /] to [DELETE http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c] with no body
[36minfo[39m: [37m<-- GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot [39m[32m-[39m[90m - ms - -[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot[39m [90m{}[39m
[36minfo[39m: JSONWP Proxy: Proxying [GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot] to [GET http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/screenshot] with body: {}
[36minfo[39m: Chromedriver: [STDERR] [605.183][SEVERE]: Timed out receiving message from renderer: 600.000
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":21,"value":{"message":"timeout: Timed out receiving message from renderer: 600.000\n (Session info: chrome=43.0.2357.121)\n (Driver info: chr...
[36minfo[39m: JSONWP Proxy: Replacing sessionId e89b5704975b3a112dc7bd1184900a6c with 15c97430-8a97-4dce-932a-347f4ed20ba7
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":0,"value":null}
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"","status":6,"value":{"message":"no such session\n (Driver info: chromedriver=2.18.343837 (52eb4041461e46a6b73308ebb19e85787ced4281),platform=Mac OS X 10.11.2 x86_64)"}}
[36minfo[39m: [37m<-- GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot [39m[32m200[39m[90m 10.175 ms - 184[39m [90m[39m
[36minfo[39m: Chromedriver: Changed state to 'stopped'
[36minfo[39m: [37m-->[39m [37mDELETE[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7[39m [90m{}[39m
[36minfo[39m: [37m<-- DELETE /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7 [39m[33m404[39m[90m 1.310 ms - 40[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mPOST[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element[39m [90m{"using":"id","sessionId":"15c97430-8a97-4dce-932a-347f4ed20ba7","value":"com.yingzt.invest:id/tab_me"}[39m
[36minfo[39m: [37m<-- POST /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/element [39m[33m404[39m[90m 1.297 ms - 40[39m [90m[39m
[36minfo[39m: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
[36minfo[39m: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
[36minfo[39m: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"OK, shutting down"}
[36minfo[39m: [debug] Sent shutdown command, waiting for UiAutomator to stop...
[36minfo[39m: [debug] [BOOTSTRAP] [debug] Closed client connection
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] Time: 614.002[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] OK (1 test)[39m
[36minfo[39m: [debug] [90m[UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1[39m
[36minfo[39m: [debug] UiAutomator shut down normally
[36minfo[39m: [debug] Cleaning up android objects
[36minfo[39m: [debug] Cleaning up appium session
[36minfo[39m: [debug] We shut down because no new commands came in
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot[39m [90m{}[39m
[36minfo[39m: [37m<-- GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot [39m[33m404[39m[90m 1.068 ms - 40[39m [90m[39m
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot[39m [90m{}[39m
[36minfo[39m: [37m<-- GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot [39m[33m404[39m[90m 0.682 ms - 40[39m [90m[39m
ERROR
查了下,应该是 chromedriver 的 bug 。而且从 appium server log 上看应该是 chromedriver 太长时间不返回导致自动超时了:
[36minfo[39m: JSONWP Proxy: Proxying [GET /wd/hub/session/15c97430-8a97-4dce-932a-347f4ed20ba7/screenshot] to [GET http://127.0.0.1:9515/wd/hub/session/e89b5704975b3a112dc7bd1184900a6c/screenshot] with body: {}
[36minfo[39m: [debug] Didn't get a new command in 60 secs, shutting down...
...
[36minfo[39m: JSONWP Proxy: Got response with status 200: {"sessionId":"e89b5704975b3a112dc7bd1184900a6c","status":21,"value":{"message":"timeout: Timed out receiving message from renderer: 600.000\n (Session info: chrome=43.0.2357.121)\n (Driver info: chr...
...
你用这个最新的 chromedriver 替换掉 appium 自带的试试?
https://pan.baidu.com/s/1bnUceaN
appium 自带的 chromedriver 位置(假设你的 Appium.app 放在 /Applications
):
/Applications/Appium.app/Contents/Resources/node_modules/appium/build/chromedriver/mac/chromedriver
直接用链接里的文件解压后覆盖旧的就好。
PS: 你用的是啥编码。。。为何大括号这些字符在你这里都变成了 [36
这种显示。。。
可以试试在esired_caps
变量中修改下下面参数。
'newCommandTimeout': 90,
'autoWebviewTimeout': 3000,
我测试中也发现类似的问题,切到 webview 后截图就不行,切到 native 模式就可以,我的做法是把截图封装一下,先判断当前 context 是不是 native,不是就切到 native,然后截图,截完图再还原 context
—— 来自 TesterHome 官方 安卓客户端