使用数据分离自动化测试,测试执行完成自动邮件发送
配置文件 配置文件中的内容为工程所有配置信息,各个模块的元素定位的方法以及值,服务器(应用服务器和数据库服务器)配置信息,用户账号密码等信息
实例,cashier.ini
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [login] storeNo = id > com.anmav.cashierdesk:id/etStoreNo username = id > com.anmav.cashierdesk:id/login_accountEdt pwd = id > com.anmav.cashierdesk:id/login_paswEdt login = id > com.anmav.cashierdesk:id/tvLogin login_store = *** login_user = *** login_pwd = *** [mailmsg] mail_user = *** mail_pwd = *** mail_to = *** mail_host = ***
该配置文件包含了login
模块进行元素定位需要的方法以及值,在工程中进行元素定位值,只需要使用configparser
模块完成配置文件读取即可。
邮件发送数据分离 封装数据分离之后的邮件发送类(MailSend.py):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #!/usr/bin/env python3 # -*- coding: utf-8 -*- from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib import configparser import os class MailSend(): """邮件发送""" def __init__(self, mail_subject, report_file): self.mail_subject = mail_subject self.report_file = report_file self.settings = os.path.abspath('..') + "\cashier_conf\cashier_setting.ini" def send_mail(self): """读取测试报告""" cf = configparser.ConfigParser() cf.read(self.settings) cf.sections() # 获取邮箱发件人,密码,收件人,邮件服务器 mail_user = cf.get('mailmsg', 'mail_user') mail_pwd = cf.get('mailmsg', 'mail_pwd') mail_to = cf.get('mailmsg', 'mail_to') mail_host = cf.get('mailmsg', 'mail_host') # 发送邮件配置 with open(self.report_file, 'r', encoding='utf-8') as f_obj: content = f_obj.read() msg = MIMEMultipart('mixed') # 添加邮件内容 msg_html = MIMEText(content, 'html', 'utf-8') msg.attach(msg_html) # 添加附件 msg_attachment = MIMEText(content, 'html', 'utf-8') msg_attachment["Content-Disposition"] = 'attachment; filename={0}'.format(self.report_file) msg.attach(msg_attachment) msg['Subject'] = self.mail_subject msg['Form'] = mail_user msg['To'] = mail_to try: # 连接邮箱服务器 s = smtplib.SMTP() s.connect(mail_host) # 登录 s.login(mail_user, mail_pwd) # 发送邮件 s.sendmail(mail_user, mail_to, msg.as_string()) s.quit() except Exception as e: print("发送邮件异常:", e)
元素定位数据分离 封装了元素定位的方法(find_element_by…),实现在工程中进行元素定位时,直接调用方法(GetElement.py):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #!/usr/bin/env python3 # -*- coding: utf-8 -*- from selenium.webdriver.support.ui import WebDriverWait import os import configparser import traceback class GetElement(): """获取元素定位方法和值""" def __init__(self): # 读取配置文件 self.settings = os.path.abspath('..') + "/cashier_conf/cashier_setting.ini" self.cf = configparser.ConfigParser() self.cf.read(self.settings) self.cf.sections() def get_elementId(self, driver, webSiteName, webelement): try: # 获取配置文件中的定位方法以及定位元素 webElemnt = self.cf.get(webSiteName, webelement).split('>') webelement_method = webElemnt[0].strip() webelement_expression = webElemnt[1].strip() element = WebDriverWait(driver, 10).until\ (lambda x: x.find_element(webelement_method, webelement_expression)) except Exception as e: print(traceback.print_exc(), e) else: return element
自动化测试实现数据分离 使用数据分离的方式完成系统登录 的自动化测试,方便维护,后期只需要维护cashier_setting.ini
文件即可(testlogin.py)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import configparser, os import traceback from datetime import datetime as dt from appium import webdriver from nose.tools import assert_true from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import NoSuchElementException,TimeoutException from cashier_tools.GetElement import GetElement from cashier_tools.MailSend import MailSend class Login(): """登录""" def __init__(self): # 初始化,配置环境 self.desired_caps = {} self.desired_caps['platformName'] = 'Android' self.desired_caps['platformVersion'] = '5.1' self.desired_caps['deviceName'] = 'Android Emulator' self.desired_caps['noReset'] = True self.desired_caps['appPackage'] = 'com.anmav.cashierdesk' self.desired_caps['appActivity'] = 'com.anmav.cashierdesk.login.activity.LoginActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps) self.elementid = GetElement() def test_login(self): settings = os.path.abspath('..') + "/cashier_conf/cashier_setting.ini" cf = configparser.ConfigParser() cf.read(settings) cf.sections() # 定位门店编号输入框 storeno = self.elementid.get_elementId(self.driver, 'login', 'storeNo') storeno.click() storeno.clear() store_no = cf.get('login', 'login_store') storeno.send_keys(store_no) self.driver.hide_keyboard() # 定位用户名 username = self.elementid.get_elementId(self.driver, 'login', 'username') username.click() username.clear() user = cf.get('login', 'login_user') username.send_keys(user) self.driver.hide_keyboard() # 定位密码 pwd = self.elementid.get_elementId(self.driver, 'login', 'pwd') pwd.click() pwd.clear() password = cf.get('login', 'login_pwd') pwd.send_keys(password) self.driver.hide_keyboard() # 登录 self.elementid.get_elementId(self.driver, 'login', 'login').click() try: # 显示等待,门店名称出现 WebDriverWait(self.driver, 10).until(lambda x:x.find_element_by_id(cf.get('order', 'store_name'))) # 断言登录成功 assert_true(u"点餐" in self.driver.page_source) except NoSuchElementException as e: print(traceback.print_exc(), e) except TimeoutException as e: print(traceback.print_exc(), e) if __name__ == '__main__': mail_subject = 'NoseTests_测试报告_{0}'.format(dt.now().strftime('%Y%m%d')) report_file = 'Login.html' mailsend = MailSend(mail_subject, report_file) print('开始执行自动化测试...') os.system('nosetests -v {0} --with-html --html-file={1}'.format(__file__, report_file)) # 发送测试报告邮件 print('开始发送测试报告...') mailsend.send_mail() print('测试报告发送成功')
单独的邮件发送模块可参照之前一篇:python实现自动化测试报告邮件实时发送