Appium python+appium+unittest 执行脚本时不执行的问题

黄亮 · 2018年11月30日 · 1181 次阅读

python+appium+unittest 执行脚本时候,一直卡在这个地方,是什么原因啊,是脚本问题还是什么原因?
脚本:

test_case1_njzx_login.py 文件中:

实现守护宝 app 登录

包名和 activity 名:com.njzx.care/com.njzx.care.activity.MainActivity

!/usr/bin/python

encoding:utf-8

import time
import unittest
from appium import webdriver

class test_login(unittest.TestCase):
# 1、环境准备与还原:文件配置
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7.0'
desired_caps['deviceName'] = '0123456789ABCDEF:5037'
# desired_caps['browserName'] = ''
# desired_caps['chromeOptions'] = {'androidProcess': 'com.tencent.mm:tools'} # 驱动 html5,必备
desired_caps['appPackage'] = 'com.njzx.care'
desired_caps['appActivity'] = 'com.njzx.care.activity.MainActivity'
# desired_caps["unicodeKeyboard", "True"] # 支持中文输入
# desired_caps["resetKeyboard", "True"] # 支持中文输入,必须两条都配置
# desired_caps['automationName'] = 'Selendroid'# link_text 定位方式是 Selendroid 框架中的定位方式,因此 appium 若想采用,则需在代码加入
desired_caps['newCommandTimeout'] = '100' # 超时时间
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

def tearDown(self):
self.driver.quit()

# 2、登录业务流程:
# (1)启动 app
# (2)点击 text = "您好,请登录"或者 id = "com.njzx.care:id/tv_helloname"进入登录界面
# (3)输入手机号码 id = "com.njzx.care:id/et_username"和密码 id = "com.njzx.care:id/et_password"
# (4)如果手机已经注册,可以直接登录;如果不存在,需要注册 text= "快速注册"
# (5)点击登录 id = "com.njzx.care:id/iv_login"
def test_sendAndLogin(self):
time.sleep(2)# 动态加载等待对象的时间,可以添加个 WebDriverWait
startLogin_text = self.find_element_by_text("您好,请登录")
startLogin_text_id = self.find_element_by_id("com.njzx.care:id/tv_helloname")
startLogin_text.click()
time.sleep(2)
text_num1 = input("请输入注册号码:")
text_psd2 = input("请输入密码:")

# 打开本地的用户名 + 密码存储文件
user_info_file = open('user_info.txt', 'r') # 用户名和密码存储文件
user_info_list = user_info_file.readlines() # 正确的用户名和密码存储队列
correct_username = user_info_list[0].strip() # 正确的用户名
correct_password = user_info_list[1] # 正确的密码
user_info_file.close()

if text_num1 == correct_username and text_psd2 == correct_password:
print('账号存在,可以登录')
time.sleep(2)
self.find_element_by_id("com.njzx.care:id/et_username").send_keys(text_num1)
self.find_element_by_id("com.njzx.care:id/et_password").send_keys(text_psd2)
else:
print('账号不存在,请重新注册!')
# register = self.find_element_by_text("快速注册")
# time.sleep(2)
# # 注册 id = com.njzx.care:id/et_phone
# register_text = self.find_element_by_id("com.njzx.care:id/et_phone")
# register_text.send_keys(number)
# time.sleep(2)
# self.find_element_by_id("com.njzx.care:id/img_submit")
# time.sleep(2)
# code_text = self.find_element_by_id("com.njzx.care:id/et_code")
# code_text.send_keys(input("请输入验证"))
# self.find_element_by_id("com.njzx.care:id/img_submit")
return False

time.sleep(2)
submit = self.find_element_by_id("com.njzx.care:id/iv_login")
submit.click()
time.sleep(2)
# 提交登陆之后,判断是否登陆成功
# 方法 1:可以判断某一个元素是否存在:
# 如:“你好,请登录” 这个元素是否存在 isDisplayed():
# try:
# get_user_info_message = self.find_element_by_id("com.njzx.care:id/tv_helloname").isDisplayed()
# print("登陆成功!")
# except Exception as e:
# print("登录失败!",format(e))
# 或者使用断言 assert:
try:
self.find_element_by_id("com.njzx.care:id/ll_userinfo").click()# 进入个人信息界面
time.sleep(2)
info_message = self.find_element_by_id("com.njzx.care:id/tv_title").text # 获取到元素中的文本信息
correct_message = "个人信息"
assert info_message == correct_message
print("登陆成功")
except Exception as e:
print("登录失败!",format(e))

# 方法 2:可以判断只有登录成功才能出现的界面,如果存在,就表明登陆成功

testsuit_njzx.py 文件中:

!/usr/bin/python

encoding:utf-8

import unittest
import test_case1_njzx_login
from HTMLTestRunner import HTMLTestRunner

测试用例集合:

if name == 'main':
suite = unittest.TestSuite() # unittest 模块中的 TestSuite() 创建一个对象接收 testcase
suite.addTests(unittest.TestLoader().loadTestsFromNames(['test_case1_njzx_login.test_login']))
with open('D:/HTMLReport.html', 'wb') as f:# 模式 wb
runner = HTMLTestRunner(stream=f,
title=' Test Report',
description='generated by HTMLTestRunner----黄亮.',
verbosity=2
)
runner.run(suite)

appium 中:

error: Unhandled error: TypeError: undefined is not a function
at [object Object].ADB.getPIDsByName (D:\Appium\loadspace\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:1037:8)
at [object Object].ADB.killProcessesByName (D:\Appium\loadspace\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:1079:8)
at [object Object].UiAutomator.start (D:\Appium\loadspace\Appium\node_modules\appium\lib\devices\android\uiautomator.js:29:12)
at [object Object]. (D:\Appium\loadspace\Appium\node_modules\appium\lib\devices\android\android.js:115:37)
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:607:21
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:246:17
at iterate (D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:146:13)
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:157:25
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:248:21
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:612:34
at [object Object].androidCommon.initUnicode (D:\Appium\loadspace\Appium\node_modules\appium\lib\devices\android\android-common.js:878:5)
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:607:21
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:246:17
at iterate (D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:146:13)
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:157:25
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:248:21
at D:\Appium\loadspace\Appium\node_modules\appium\node_modules\async\lib\async.js:612:34
at [object Object]. (D:\Appium\loadspace\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:180:9)
at ChildProcess.exithandler (child_process.js:742:7)
at ChildProcess.emit (events.js:110:17)
at maybeClose (child_process.js:1016:16)
at Process.ChildProcess._handle.onexit (child_process.js:1088:5) context: [POST /wd/hub/session {"capabilities":{"firstMatch":[{}],"alwaysMatch":{"platformName":"Android","appium:platformVersion":"7.0","appium:deviceName":"0123456789ABCDEF:5037","appium:appPackage":"com.njzx.care","appium:appAct]

HTMLReport.html 中:
File "D:\python\lib\http\client.py", line 297, in begin version, status, reason = self._read_status() File "D:\python\lib\http\client.py", line 266, in _read_status raise RemoteDisconnected("Remote end closed connection without"urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

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