Python pycharm selenium hyrobot 学习中遇到的问题汇总
1.发送邮件
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from cfg import cfg
# 第三方 SMTP 服务
def send_mail(subject, context):
dir_tmp = cfg.dir_tmp_str
mail_host = "smtp.chainz.cn" # 设置服务器
mail_user = "XXX" # 用户名
mail_pass = "xxx" # 口令
sender ="XXX"
receivers = ['xxx'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText(context+'\n文件保存路径:'+dir_tmp, 'plain', 'utf-8')
m = MIMEMultipart()
m['Subject'] = Header(subject, 'utf-8')
# 构造附件1,传送 文件
att1 = MIMEApplication(open(dir_tmp+'/log.html', 'rb').read())
att1.add_header('Content-Disposition', 'attachment', filename='log.html')
m.attach(att1)
# 构造附件2,传送 文件
att2 = MIMEApplication(open(dir_tmp++'/report.html', 'rb').read())
att2.add_header('Content-Disposition', 'attachment', filename='report.html')
m.attach(att2)
m.attach(message)
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, m.as_string())
print("邮件发送成功")
smtpObj.quit()
except smtplib.SMTPException:
print("Error: 无法发送邮件")
注意要点:传送各种非图片和文本的附件使用MIMEApplication
注意要退出
文本内容是用MIMEText
2.运行不同测试名称的用例
- --test testA # 执行名为 testA 的用例
- --test testA --test testB # 执行名为 testA 和 testB 的用例
- --test test* # 执行名字以 test 开头的用例
- --suite 订单管理 # 执行 名为 订单管理 的套件
完整是 run --test testA
注意是双-
3.根据标签运行
# 执行包含 标签 '冒烟测试' 的用例.
--include 冒烟测试
# 执行不包含标签 '冒烟测试' 的用例.
--exclude 冒烟测试
# 执行 有冒烟测试、UITest 两个标签的用例
--include 冒烟测试ANDUITest
# 执行 有 冒烟测试 或者 UITest 标签 的用例
--include 冒烟测试 --include UITest
# 执行标签格式为 A*B 的用例,比如 A5B, AB, A444B
--include A*B
注意:关键测试用例
run --critical first #first 为标签名
4.截图
使用driver自带的截图
#先要获取driver
#图片的保存地址
dir_pic = dir_tmp_str + "/" + getTime_strftime() + '.png' logger.info(dir_pic) try: picture_url = dr.get_screenshot_as_file(dir_pic) logger.info("%s:截图成功!!!" % picture_url) except BaseException as msg: logger.info(msg)
注意路径地址在不用系统的兼容性
5.调用的方法多数时有默认值时 可以先使用默认值 特殊情况使用*args
例如:
1 # 登录函数 2 # 当传入用户名和密码是根据用户名和密码登录 3 # 当不传入用户名和密码时,根据默认的用户名和密码登录 4 def login(*args): 5 if len(args) < 1: 6 username = cfg.user01 7 password = cfg.password01 8 9 else: 10 username = args[0] 11 password = args[1] 12 13 get_element(['n', 'username']).send_keys(username) 14 get_element(['n', 'password']).send_keys(password) 15 get_element(['c', '[aria-label="创建"]']).click()
注意:args使用数组
6.测试套件
如果一整个套件中需要添加标签或者启动啥的
可以使用__st__.py
注意:是双_
7.注意不要同名
包括大小写
8.注意不要在声明中的方法 调用还没解释到的方法
可以调整引用的顺序
浙公网安备 33010602011771号