安卓手机:真机,安卓 8.0
appium 版本:1.4
框架:python+appium+unittest
问题描述:成功连接手机,开启 appium server 后,执行 testsuit.py 主程序时,不执行用例程序。
pycharm 控制台信息如下:
<_io.TextIOWrapper name='' mode='w' encoding='UTF-8'>
Time Elapsed: 0:00:00
appium 中无日志信息。
脚本如下:
测试用例文件 loginIn_testcase.py
# -*- coding:utf-8 -*-
import unittest
import os
import time
from appium import webdriver # 导入appium库
from selenium.common.exceptions import NoSuchElementException
# 测试用例1:
# 安卓应用名:守护宝
# 包名和activity名:com.njzx.care/com.njzx.care.activity.WelcomeActivity2
# 测试步骤:
# (1)打开apk
# (2)关爱界面点击“您好,请登录”,界面跳转至登录/注册界面
# (3)直接输入号码:15152660926 密码:111111
# (4)点击登录---成功
class Test_Login(unittest.TestCase):
def setUp(self):
# 准备环境
# PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p)) # 不写这一句也OK
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.0'
desired_caps['deviceName'] = 'NPE4C17302001740:5037'
# desired_caps['app'] = PATH('D:/workspace/testUI_new/app/test.apk')
desired_caps['appPackage'] = 'com.njzx.care'
desired_caps['appActivity'] = 'com.njzx.care.activity.WelcomeActivity2'
desired_caps["unicodeKeyboard", "True"] # 支持中文输入
desired_caps["resetKeyboard", "True"] # 支持中文输入,必须两条都配置
desired_caps['newCommandTimeout'] = '100' # 超时时间
desired_caps['automationName'] = 'uiautomator2'
# desired_caps['app'] = 'apk包存放地址'
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 打开app 127.0.0.1是环回(loopback)地址
time.sleep(5)
print("环境准备完成!")
self.verificationErrors = "----------------------------------------!"
def tearDown(self):
self.drvier.quit()
print("环境还原完成!")
# 测试用例
def login_in(self):
time.sleep(5)
try:
title = self.driver.find_element_by_text("您好,请登录")
except NoSuchElementException as e:
list_care = self.driver.find_element_by_id('com.njzx.care:id/ll')
list_care.click()
time.sleep(2)
else:
title.click()
time.sleep(2)
user = self.driver.find_element_by_text("请输入手机号码")
if user.exists():
user.click()
time.sleep(2)
user_tel = input("请输入手机账号")
user.send_keys(user_tel)
else:
print('user元素未获取得到')
pwd = self.driver.find_element_by_text("请输入登录密码")
if pwd.exists():
pwd.click()
time.sleep(2)
pwd_tel = input("请输入密码")
pwd.send_keys(pwd_tel)
else:
print('pwd元素未获取得到')
# 点击登录
try:
submit_login = self.driver.find_element_by_id("com.njzx.care:id/iv_login")
except (NoSuchElementException,Exception) as d:
print("登录失败!", format(d))
else:
submit_login.click()
time.sleep(2)
测试集(主程序)文件 testsuit.py:
# encoding:utf-8
import unittest
from loginIn_testcase import Test_Login
from HTMLTestRunner import HTMLTestRunner
# 用例集合
if __name__ == '__main__':
suite = unittest.TestSuite() # unittest模块中的TestSuite()创建一个对象接收testcase
suite.addTests(unittest.TestLoader().loadTestsFromNames(['loginIn_testcase.Test_Login']))# 传入测试用例(文件名 + 类名)
with open('E:/pycharm/run/SHB_Test/result_report/HTMLReport.html', 'wb') as f:# 模式wb
runner = HTMLTestRunner(stream=f,
title=' Test Report',
description='generated by HTMLTestRunner----Bling',
verbosity=2
)
runner.run(suite)