import unittest
from appium import webdriver
from time import sleep
from HTMLTestRunner import HTMLTestRunner
import time
import yagmail
from appium.webdriver.common.touch_action import TouchAction
# 写在前面,将手机USB调试权限打开,将充电不熄屏打开,已经绑定手环,更好AGPS
class Dttest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("start setup")
desired_caps = {
'platformName': 'Android', # 被测手机是安卓
'platformVersion': '10', # 手机安卓版本
'deviceName': 'xxx', # 设备名,安卓手机可以随意填写
'appPackage': 'com.zjw.apps3pluspro', # 启动APP Package名称
'appActivity': '.SplashActivity', # 启动Activity名称
'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True
'resetKeyboard': True, # 执行完程序恢复原来输入法
'noReset': True, # 不要重置App
'newCommandTimeout': 6000,
'automationName': 'UiAutomator2'
}
# 连接Appium Server,初始化自动化环境
cls.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置缺省等待时间
cls.driver.implicitly_wait(10)
cls.driver.find_element_by_class_name("android.widget.Button").click() # 测试版本加本行代码,非测试版本不用
@classmethod
def tearDownClass(cls):
print("over")
def test_cq(self):
self.driver.close_app()
s = 1
c = 1
i = 1
while i <= 5:
self.driver.launch_app()
self.driver.find_element_by_class_name("android.widget.Button").click()
sleep(5)
try:
self.driver.find_element_by_id('tvConnectState')
except:
print('连接成功', s)
s += 1
else:
print('连接失败', c)
c += 1
self.driver.close_app()
i += 1
print('连接成功次数为', s-1)
print('连接失败次数为', c-1)
# 密码是授权码
def send_mail(report):
yag = yagmail.SMTP(user="54581771@qq.com",
password="4560913qqqqq",
host="smtp.qq.com")
subject = "主题,自动化测试报告"
contents = "正文,请查看附件。"
yag.send('ycc0022@163.com', subject, contents, report)
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(Dttest("test_cq"))
now_time = time.strftime("%Y-%m-%d %H_%M_%S")
fp = open('./report/' + now_time + 'result.html', 'wb')
runner = HTMLTestRunner(stream=fp,
title="3+PRO",
description="运行环境:Windows 10, redmi10X"
)
runner.run(suite)
fp.close()
send_mail(fp) # 发送报告