新手区 python+appium 执行完第一条用例就报错

测试老菜 · 2016年04月20日 · 最后由 测试zzZ 回复于 2019年07月29日 · 3648 次阅读

环境:

  • win7
  • python 2.7
  • appium 1.4.13.1
  • 模拟器 Genymotion 2.5.2
  • IDE:PyChram

问题:test_a 用例执行完之后,在执行 test_b 之前就报错

用例代码如下:

import time
import unittest

from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
from waiqing_testcase import DRIVER
from waiqing_testcase.screenshot import take_screenshot
from selenium.webdriver.support import expected_conditions as EC
from common import *


class MyAndroidTest(unittest.TestCase):

    def setUp(self):
        global driver
        driver = DRIVER.MyDriver.get_driver()

    def tearDown(self):
        driver.quit()

    def test_a(self):
          ...
    def test_b(self):
          ...

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(MyAndroidTest) 
    unittest.TextTestRunner(verbosity=2).run(suite)

appium driver 代码:

# -*- coding:utf-8 -*-
from appium import webdriver
import threading


class MyDriver:
    driver = None
    mutex = threading.Lock()
    desired_caps = {}
    desired_caps['deviceName'] = 'Android Emulator'
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '4.2'
    desired_caps['appPackage'] = 'com.xbcx.waiqing'
    desired_caps['appActivity'] = 'com.xbcx.waiqing.activity.StartActivity'

    def __init__(self):
        pass

    @staticmethod
    def get_driver():
        if MyDriver.driver is None:
            MyDriver.mutex.acquire()
            if MyDriver.driver is None:
                MyDriver.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
            else:
                return MyDriver.driver
            MyDriver.mutex.release()
        else:
             print "you have been created a session"
        return MyDriver.driver

pychram 中的错误日志:问题好像出在 driver.quit() 这里,但给出的错误 WebDriverException: Message: ,也没有显示到底是因为什么导致的报错

ERROR: test_sales (waiqing_testcase.testcase.MyAndroidTest) Traceback
(most recent call last): File
"C:\Users\Administrator\PycharmProjects\waiqing_testcase\waiqing_testcase\testcase.py",
line 22, in tearDown
driver.quit() File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 483, in quit
self.execute(Command.QUIT) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 201, in execute
self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\appium\webdriver\errorhandler.py", line
29, in check_response
raise wde WebDriverException: Message:

-------------------- >> begin captured logging << -------------------- selenium.webdriver.remote.remote_connection: DEBUG: GET
http://127.0.0.1:4723/wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/window/current/size
{"windowHandle": "current", "sessionId":
"543bc504-4624-4319-a611-df115d55bfad"}
selenium.webdriver.remote.remote_connection: DEBUG: Finished Request
selenium.webdriver.remote.remote_connection: DEBUG: POST
http://127.0.0.1:4723/wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/element {"using": "name", "sessionId": "543bc504-4624-4319-a611-df115d55bfad",
"value": "\u7ec8\u7aef\u9500\u91cf"}
selenium.webdriver.remote.remote_connection: DEBUG: Finished Request
selenium.webdriver.remote.remote_connection: DEBUG: DELETE
http://127.0.0.1:4723/wd/hub/session/543bc504-4624-4319-a611-df115d55bfad
{"sessionId": "543bc504-4624-4319-a611-df115d55bfad"}
selenium.webdriver.remote.remote_connection: DEBUG: Finished Request
--------------------- >> end captured logging << ---------------------

appium 中的 log:

info: <-- POST /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/element/3/click 200 1238.069 ms - 76 {"status":0,"value":true,"sessionId":"543bc504-4624-4319-a611-df115d55bfad"}
info: --> GET /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/screenshot {}
info: [debug] executing cmd: D:\android-sdk\platform-tools\adb.exe -s 192.168.56.101:5555 shell "/system/bin/rm /data/local/tmp/screenshot.png; /system/bin/screencap -p /data/local/tmp/screenshot.png "
info: [debug] executing cmd: D:\android-sdk\platform-tools\adb.exe -s 192.168.56.101:5555 pull /data/local/tmp/screenshot.png "C:\Users\ADMINI~1\AppData\Local\Temp\appium116320-4608-14dbqlt.png"
info: [debug] Responding to client with success: {"status":0,"value":"iVBORw0KGgoAAAANSUhEUgAAAwAAAAUACAYAAADwUTW0AAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzs3Xd8VFX6+PHP9EwyqYQ0IhA6CQgoHWzgKi6oqIiKUlTEXd1d+SnrrqvurmVxXbZIE7+AICqKiIgCigKioCAEpIfQEqkJCWlkksnU+/sjziVDJn1CAjzv1wudcu+5ZyaTyXlOeY4GUBBCCCGEEEJcEbRNXQEhhBBCCCHExSMBgBBCCCGEEFcQCQCEEEIIIYS4gkgAIIQQQgghxBVEAgAhhBBCCCGuIBIACCGEEEIIcQXRN3UFhBBCCCEuRxqNRr2tKJJ1XTQfGprJPgAGg4Ho6GgUReHs2bO4XK6mrpIQQgghRK14G/sajQaPx1PlcVqtVg0GmmtQYDAYcLlcDa6fzhSEzmSushyNRoPbbsNtL2vQdUTdNZsRgJiYGH7zm9+gKAqzZ8/mzJkzTV2lgLCEaYlLNBBk1lJa6iHrmBNbadVfDEIIIYRo3vz17F/4f51Oh15/lcrlwu91+g4PmMFKg0WhQFAW9Xk+XLl04cOBA3TpjNednlWt0WvB4aDXkbq66bQwep8PnNUL569QajJz48gNOfLUEtFoUd4X3RpG2UmNqNgGAVqslIiICKI88L3Ut4/XcMDyUdl1N6LTnP/ROp8K+7TY2flFMqVU+3EIIIcSlwtuIrdhI1+v1WCwW4uLiiIiIIDg4mLCwMIKCgggKClKPKysro6ysjHPnzlFaWkphYSHZ2dlYrVafhra/a1wM3ut17dqVFi1a4Ha761jA+TaN4iq/rdEb0JtDwBxS5WkavQHF44FqRk1E4DWbAKCiC6PES801g0O48fZQdAYNCuCq8Dus0Wvo3j+Y1h2NLJ6ZR8k5+cALIYQQzZm3d9zbSLZYLLRq1YrExESSkpKIiopSG/tabc35VbyjAGVlZeTn55OZmcnJkyc5deoUVqu10nUbm7fd1bFjR1q0aIHL...","sessionId":"543bc504-4624-4319-a611-df115d55bfad"}
info: <-- GET /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/screenshot 200 1337.553 ms - 94842 {"status":0,"value":"iVBORw0KGgoAAAANSUhEUgAAAwAAAAUACAYAAADwUTW0AAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzs3Xd8VFX6+PHP9EwyqYQ0IhA6CQgoHWzgKi6oqIiKUlTEXd1d+SnrrqvurmVxXbZIE7+AICqKiIgCigKioCAEpIfQEqkJCWlkksnU+/sjziVDJn1CAjzv1wudcu+5ZyaTyXlOeY4GUBBCCCGEEEJcEbRNXQEhhBBCCCHExSMBgBBCCCGEEFcQCQCEEEIIIYS4gkgAIIQQQgghxBVEAgAhhBBCCCGuIBIACCGEEEIIcQXRN3UFhBBCCCEuRxqNRr2tKJJ1XTQfGprJPgAGg4Ho6GgUReHs2bO4XK6mrpIQQgghRK14G/sajQaPx1PlcVqtVg0GmmtQYDAYcLlcDa6fzhSEzmSushyNRoPbbsNtL2vQdUTdNZsRgJiYGH7zm9+gKAqzZ8/mzJkzTV2lgLCEaYlLNBBk1lJa6iHrmBNbadVfDEIIIYRo3vz17F/4f51Oh15/
lcrlwu91+g4PmMFKg0WhQFAW9Xk+XLl04cOBA3TpjNednlWt0WvB4aDXkbq66bQwep8PnNUL569QajJz48gNOfLUEtFoUd4X3RpG2UmNqNgGAVqslIiICKI88L3Ut4/XcMDyUdl1N6LTnP/ROp8K+7TY2flFMqVU+3EIIIcSlwtuIrdhI1+v1WCwW4uLiiIiIIDg4mLCwMIKCgggKClKPKysro6ysjHPnzlFaWkphYSHZ2dlYrVafhra/a1wM3ut17dqVFi1a4Ha761jA+TaN4iq/rdEb0JtDwBxS5WkavQHF44FqRk1E4DWbAKCiC6PES801g0O48fZQdAYNCuCq8Dus0Wvo3j+Y1h2NLJ6ZR8k5+cALIYQQzZm3d9zbSLZYLLRq1YrExESSkpKIiopSG/tabc35VbyjAGVlZeTn55OZmcnJkyc5deoUVqu10nUbm7fd1bFjR1q0aIHL...","sessionId":"543bc504-4624-4319-a611-df115d55bfad"}
info: --> DELETE /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad {}
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: D:\android-sdk\platform-tools\adb.exe -s 192.168.56.101:5555 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: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
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: 20.681
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] Responding to client with success: {"status":0,"value":null,"sessionId":"543bc504-4624-4319-a611-df115d55bfad"}
info: <-- DELETE /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad 200 914.966 ms - 76 {"status":0,"value":null,"sessionId":"543bc504-4624-4319-a611-df115d55bfad"}
info: --> GET /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/window/current/size {}
info: <-- GET /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/window/current/size 404 0.662 ms - 40
info: --> POST /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/element {"using":"name","sessionId":"543bc504-4624-4319-a611-df115d55bfad","value":"缁堢閿€閲?}
info: <-- POST /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad/element 404 0.651 ms - 40
info: --> DELETE /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad {}
info: <-- DELETE /wd/hub/session/543bc504-4624-4319-a611-df115d55bfad 404 0.539 ms - 40

日志显示出错的地方
info: [debug] Responding to client with success:
{"status":0,"value":... }返回的给 client 的 value 的值是一串乱码

到这里为止,我不明白到底是哪里出错,为什么会返回乱码?应该怎么改?

第一次在 testerhome 上发帖,表达不清楚的地方,望大家见谅!

共收到 8 条回复 时间 点赞

元素没找到啊。

用 name 是终端销量没找到。

我看用例代码没有 utf-8 标记,是没贴出来吗?报了乱码会不会是因为这个原因?

#2 楼 @lihuazhang 我觉得应该不是元素没找到,我这两条用例单独执行都可以成功(注释另外一条),两条用例一起执行就会报错

#3 楼 @paicong 是没贴出来,用例代码有 utf-8 的标记

#2 楼 @lihuazhang 我又看了下,确实是没找到元素 “终端销量”,原因是第一条用例结束后,没有执行 driver.quit(),执行第二条用例的时候,就没有启动 app...是我 driver 部分的代码有问题吗?

个人觉的,应该是你定义的 tearDown 并没有得到 driver 的属性

楼主,我也遇到了这个报错,请问解决了吗?求告知

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册