说明
继实第一个实战项目的基础上添加自动发送报告邮件接口,代码有部分调整
变动
1.增加文本文档SendToUserinfo.txt 用来保存邮件接收者的信息
2.修改测试报告文件名称格式并添加reprot目录保存测试报告
3.添加代码注释,更方便阅读
项目目录
代码
修改过的代码直接贴上,没修改的代码就不贴了,可以去实战一直接拿过来用就ok
1 #! user/bin/python 2 ''' 3 代码说明:麦子学院登录模块自动化测试用例脚本 4 编写日期: 5 设置者:linux超 6 ''' 7 8 import time 9 from selenium import webdriver 10 from webinfo import webinfo 11 from userinfo import userinfo 12 from log_fiile import login_log 13 from send_email import send_mail_init_info,read_SendToUserinfo_file 14 15 #account = '281754043@qq.com' 16 def open_web(): 17 ''' 18 :return:web句柄 19 ''' 20 driver = webdriver.Firefox() 21 driver.maximize_window() 22 return driver 23 24 def load_url(driver,ele_dict): 25 ''' 26 :param driver: web主页面句柄 27 :param ele_dict: web信息字典(从文件中读取) 28 :return: 无 29 ''' 30 driver.get(ele_dict['Turl']) 31 time.sleep(5) 32 33 def find_element(driver,ele_dict): 34 ''' 35 :param driver: web句柄 36 :param ele_dict: web信息字典(从文件中读取) 37 :return: 返回找到的元素 38 ''' 39 driver.find_element_by_class_name(ele_dict['image_id']).click() 40 if 'text_id' in ele_dict: 41 driver.find_element_by_link_text('登录').click() 42 43 user_id = driver.find_element_by_id(ele_dict['userid']) 44 pwd_id = driver.find_element_by_id(ele_dict['pwdid']) 45 login_id = driver.find_element_by_id(ele_dict['loginid']) 46 return user_id,pwd_id,login_id 47 48 def send_val(ele_tuple,arg): 49 ''' 50 :param ele_tuple: web元素 51 :param arg: 可变长参数,接收用户名和密码 52 :return: 无 53 ''' 54 # input userinfo 55 listkey = ['uname','pwd'] 56 i = 0 57 for key in listkey: 58 ele_tuple[i].send_keys('') 59 ele_tuple[i].clear() 60 ele_tuple[i].send_keys(arg[key]) 61 i+=1 62 ele_tuple[2].click() 63 # 判断登录是否成功,并写入测试报告 64 def check_login(driver,ele_dict,log,userlist): 65 ''' 66 :param driver: web句柄 67 :param ele_dict: 读取到的元素信息(文件中读到的) 68 :param log: login_log对象 69 :param userlist: 用户信息(文件中读取) 70 :return: False or Ture 71 ''' 72 result = False 73 time.sleep(3) 74 try: 75 err = driver.find_element_by_id(ele_dict['error']) 76 driver.save_screenshot(err.text+'.png') 77 log.log_write('账号:%s 密码:%s 提示信息:%s:failed ' %(userlist['uname'],userlist['pwd'],err.text)) 78 79 print('username or password error') 80 except: 81 print('login success!') 82 log.log_write('账号:%s 密码:%s :passed '%(userlist['uname'],userlist['pwd'])) 83 #login_out(driver,ele_dict) 84 return True 85 return result 86 # 登出 87 def login_out(driver,ele_dict): 88 time.sleep(5) 89 driver.find_element_by_class_name(ele_dict['logout']).click() 90 ''' 91 def screen_shot(err): 92 i = 0 93 save_path = r'D:pythondcodecapture' 94 capturename = '\'+str(i)+'.png' 95 wholepath = save_path+capturename 96 if Path(save_path).is_dir(): 97 pass 98 else: 99 Path(save_path).mkdir() 100 while Path(save_path).exists(): 101 i+=1 102 capturename = '\'+str(i)+'.png' 103 wholepath = save_path + capturename 104 err.screenshot(wholepath) 105 ''' 106 107 def login_test(): 108 log = login_log() 109 msg = read_SendToUserinfo_file(r'D:pythoncodelogin_testSendToUserinfo.txt') 110 sendmail = send_mail_init_info('smtp.qq.com','281754043@qq.com','gifhhsbgqyovbhhc','281754043@qq.com',msg) 111 #ele_dict = {'url': 'http://www.maiziedu.com/', 'text_id': '登录', 'user_id': 'id_account_l', 'pwd_id': 'id_password_l' 112 #, 'login_id': 'login_btn','image_id':'close-windows-btn7','error_id':'login-form-tips'} 113 ele_dict = webinfo(r'D:pythoncodewebinfo.txt') 114 #user_list=[{'uname':account,'pwd':pwd}] 115 user_list = userinfo(r'D:pythoncodeuserinfo.txt') 116 driver = open_web() 117 # load url 118 load_url(driver,ele_dict) 119 #find element 120 ele_tuple = find_element(driver,ele_dict) 121 # send values 122 ftitle = time.strftime('%Y-%m-%d', time.gmtime()) 123 log.log_write(' %s登录系统测试报告 ' % (ftitle)) 124 # 循环用户测试数据 125 for userlist in user_list: 126 send_val(ele_tuple,userlist) 127 # check login success or failed 128 result = check_login(driver,ele_dict,log,userlist) 129 if result: 130 login_out(driver,ele_dict) 131 time.sleep(3) 132 ele_tuple = find_element(driver,ele_dict) 133 134 time.sleep(3) 135 log.log_close() # 关闭log文件 136 # 发送报告到指定的邮箱 137 sendmail.send_email(log.filename) 138 driver.quit() 139 if __name__ == '__main__': 140 login_test()
1 #! user/bin/python 2 ''' 3 代码说明:测试输出报告 4 编写日期: 5 设置者:linux超 6 ''' 7 8 import time 9 # 测试报告输出接口 10 class login_log(object): 11 filename = './report/' + time.strftime('%Y-%m-%d %H_%M_%S') + '.txt' 12 def __init__(self,path='./report/',mode='w'): 13 #filename = path + time.strftime('%Y-%m-%d %H_%M_%S') +'.txt' 14 self.log = open(self.filename,mode) 15 def log_write(self,msg): 16 self.log.write(msg) 17 def log_close(self): 18 self.log.close() 19 if __name__ == '__main__': 20 log=login_log() 21 log.log_write('xiaochao11520') 22 mas = log.filename 23 print(mas) 24 log.log_close()
1 #! user/bin/python 2 ''' 3 代码说明:通过邮件发送测试报告 4 编写日期:2018.07.17 5 设置者:linux超 6 ''' 7 8 import smtplib 9 from email.mime.text import MIMEText 10 from email.header import Header 11 # 邮件发送接口 12 class send_mail_init_info(object): 13 ''' 14 邮件配置信息 15 ''' 16 def __init__(self,server,fromuser,frompassword,sender,receiver): 17 self.server = server 18 self.fromuser = fromuser 19 self.frompassword = frompassword 20 self.sender = sender 21 self.receiver = receiver 22 ''' 23 配置邮件发送者等信息 24 ''' 25 def send_email(self,filename): 26 # 打开报告文件读取文件内容 27 #filename = './report/'+ time.strftime('%Y-%m-%d %H_%M_%S') 28 f = open(filename,'r') 29 file_msg = f.read() 30 print(filename) 31 print(file_msg) 32 f.close() 33 # 邮件服务器 34 smtpserver = self.server 35 # 发件人用户名和密码 36 user = self.fromuser 37 password = self.frompassword 38 # 发件人 39 sender = self.sender 40 # 收件人 41 receiver = self.receiver 42 # 邮件主题 43 subject = 'Python test report' #+ filename 44 # 邮件设置 45 msg = MIMEText(file_msg,'html','utf-8') 46 msg['subject'] = Header(subject,'utf-8') 47 msg['from'] = sender 48 # 连接服务器,登录服务器,发送邮件 49 self.smtp = smtplib.SMTP() 50 self.smtp.connect(smtpserver) 51 self.smtp.login(user,password) 52 try: 53 self.smtp.sendmail(sender, receiver, msg.as_string()) 54 except Exception as e: 55 print('send failed', e) 56 else: 57 print('send success!') 58 def __del__(self): 59 self.smtp.quit() 60 # 从文件中读取邮件接收人信息 61 def read_SendToUserinfo_file(filename): 62 ''' 63 :param filename: 读取接收邮件人信息 64 :return: 接手邮件人信息 65 ''' 66 open_file = open(filename) 67 read_msg = open_file.read() 68 return read_msg 69 if __name__ == '__main__': 70 #send_email('./report/2018-07-16 13_09_21.txt') 71 read_msg=read_SendToUserinfo_file(r'D:pythoncodelogin_testSendToUserinfo.txt') 72 sendmail = send_mail_init_info('smtp.qq.com','281754043@qq.com','gifhhsbgqyovbhhc','281754043@qq.com',read_msg) 73 sendmail.send_email('./report/2018-07-16 13_09_21.txt')
刚测试这里面的def read_SendToUserinfo_file(filename) 方法有点问题,现在修改一下,如下:
1 def read_SendToUserinfo_file(filename): 2 ''' 3 :param filename: 读取接收邮件人信息 4 :return: 接手邮件人信息 5 ''' 6 open_file = open(filename) 7 #read_msg = open_file.read() 8 #return read_msg 9 for line in open_file: 10 msg = [i.strip() for i in line.split(',')] 11 print(msg) 12 return msg
1 281754043@qq.com,486668776@qq.com
总结
目前这个小项目所有最基本的功能都有了 , 我会慢慢的再完善,毕竟是学习,希望把它完善的越来越好,自己动手知识记得的牢固一点