test_case/public :是登录和退出模块, 注意:_init_.py 是必须要的
test_case/test_addCard test-addPerson 这两个分别是不同的测试用例
all_tests 执行所有用例
本次用例功能点:登录,退出,用例(新增卡片,新增实验室人员培训),分别模块化,总的文件来执行所有的用例,登录和退出是公共模块,所有放在一个文件夹,
后续用例需要就直接调用即可,也引用BSTestRunner 测试报告的生成和自动发送邮件报告给某人
all_tests :引用了discover()方法,可以通过文件的名称来判断是否为测试用例文件,如果为用例文件则自动添加到测试套件中,
测试套件在运行时只识别并运行test开头的.py文件
all_tests.py 文件内容
#coding=utf-8
import unittest,time
import os
from datetime import datetime
import BSTestRunner
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#自动发送邮件
def send_email(new_report):
#读取测试报告中的内容作为邮件的内容
with open(new_report,'r',encoding='utf-8',errors='ignore') as f:
mail_body = f.read()
#发件人地址
from_addr = 'caiting0818@qq.com'
#收件人地址
to_addr = 'caiting0818@qq.com'
#发送邮箱的服务器地址
mail_server = 'smtp.qq.com'
#邮件的标题
subject =u'qq登录测试报告'
#发件人的邮箱地址
username = 'caiting0818@qq.com'
password = 'puiqzotbyjirjdia'
#邮箱的内容和标题
message = MIMEText(mail_body,'html','utf-8')
message['Subject'] = Header(subject,charset='utf-8')
#发送邮件
smtp = smtplib.SMTP()
smtp.connect(mail_server)
smtp.login(username,password)
smtp.sendmail(from_addr,to_addr.split(','),message.as_string())
smtp.quit()
#获取最新报告的地址
def acquire_report_address(reports_address):
#测试报告文件夹中的所有文件加入到列表
test_reports_list = os.listdir(reports_address)
#按照升序排序生成新的列表
new_test_reports_list = sorted(test_reports_list)
#获取最新的测试报告
the_last_report = new_test_reports_list[-1]
#最新的测试报告的地址
the_last_report_address = os.path.join(reports_address,the_last_report)
return the_last_report_address
def creatsuitel():
testunit = unittest.TestSuite()
#discover方法定义
discover = unittest.defaultTestLoader.discover(
listaa,
pattern='test_*.py',
top_level_dir=None
)
#discover方法筛选出来的用例,循环添加到测试套件中
for test_suite in discover:
for test_case in test_suite:
testunit.addTest(test_case)
print(testunit)
return testunit
if __name__ == '__main__':
#测试用例
listaa = 'C:\\Users\\Admin\\PycharmProjects\\ShengQi\\test_case'
alltestnames = creatsuitel()
# 测试报告文件夹地址
test_reports_address = u'C:\\Users\\Admin\\Desktop\\测试报告'
# 获取当前时间
now = datetime.now().strftime('%Y%m%d%H%MM%f')
# 生成以当前时间命名的测试报告文件名
test_report_name = r'{}\report_{}.html'.format(test_reports_address, datetime.now().strftime('%Y%m%d%H%M%f'))
# 生成以当前时间命名的测试报告文件
file_report = open(test_report_name, 'w', encoding='utf-8')
# 生成html形式的报告
runner = BSTestRunner.BSTestRunner(stream=file_report, title=u'正式的测试报告', description=u'新增卡片和实验室新增培训人员:')
# 运行
# 执行测试套件
runner.run(alltestnames)
file_report.close()
# 查找最新生成的测试报告地址
new_report_addr = acquire_report_address(test_reports_address)
# 自动发送邮件
send_email(new_report_addr)
test_addCard.py 文件内容
#导入驱动的包和控制时间的包
from selenium import webdriver
import time
import unittest
from test_case.public.login import test_Login
from test_case.public.quit import quit
#卡片新增自动化脚本设计到的点:放大镜,下拉框,日期框,文本框,页面下拉到底部,上传文件等
class addCard(unittest.TestCase):
# 获取动态的frame0
def iframe0(self):
time.sleep(1)
# 获取动态的frame0
iframe0 = self.driver.find_elements_by_tag_name("iframe")
r0 = iframe0[0].get_attribute('id')
print('动态frame0:%s' % r0)
self.driver.switch_to.frame(r0)
def iframe1(self):
time.sleep(1)
# 获取动态的frame1
iframe1 = self.driver.find_elements_by_tag_name("iframe")
r1 = iframe1[1].get_attribute('id')
print('动态frame1:%s' % r1)
self.driver.switch_to.frame(r1)
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.url = 'http://localhost:8087/t6/'
time.sleep(2)
def test_addCard(self):
u"""新增卡片"""
driver = self.driver
driver.get(self.driver.url)
#调用登录模块
test_Login(self)
time.sleep(5)
#通过frame方式定位
driver.switch_to.frame("id_iframe")
#点击资产账
driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/div[3]/ul/li[2]/div/div[2]/span').click()
time.sleep(1)
#点击固定资产管理
driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/div[3]/ul/li[2]/ul/li[2]/div/div[2]').click()
time.sleep(1)
#点击固定资产登记
driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/div[3]/ul/li[2]/ul/li[2]/ul/li[1]/div/div[2]').click()
time.sleep(1)
#退出frame方式定位
driver.switch_to.default_content()
#通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.find_element_by_xpath('//*[@id="jpwClose"]').click()
time.sleep(5)
driver.switch_to.frame("J_iframe_M160103A")
#点击新增
driver.find_element_by_xpath('/html/body/div[2]/div[1]/button[1]').click()
#退出两层frame方式定位
driver.switch_to.default_content()
#退出两层frame方式定位
driver.switch_to.default_content()
time.sleep(3)
# 获取动态的frame0
self.iframe1()
time.sleep(1)
#点击"其他"类型卡片
driver.find_element_by_xpath('/html/body/div/div[5]').click()
time.sleep(1)
#退出动态frame方式定位
driver.switch_to.default_content()
#通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.switch_to.frame("J_iframe_M160103A")
#获取动态的frame
self.iframe0()
time.sleep(2)
#点击'使用部门'放大镜
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[1]/td[4]/span/span/span').click()
#退出frame方式定位
driver.switch_to.default_content()
driver.switch_to.default_content()
driver.switch_to.default_content()
time.sleep(2)
#选择部门‘教务处’
driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[3]/div[1]/ul/li/ul/li[8]/a/span[2]').click()
time.sleep(2)
#点击:确定 按键
driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[4]/button[1]').click()
#通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.switch_to.frame("J_iframe_M160103A")
#获取动态的frame
self.iframe0()
time.sleep(2)
#点击'教育分类代码'放大镜
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[2]/td[2]/span/span/span').click()
#退出frame方式定位
driver.switch_to.default_content()
driver.switch_to.default_content()
driver.switch_to.default_content()
time.sleep(2)
#选择资产类别“图书”
driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[3]/div[1]/ul/li/ul/li[11]/a/span[2]').click()
time.sleep(1)
#点击:确定 按键
driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[4]/button[1]').click()
time.sleep(1)
#通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.switch_to.frame("J_iframe_M160103A")
#获取动态的frame
self.iframe0()
time.sleep(2)
#点击'资产分类代码'放大镜
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[2]/td[4]/span/span/span').click()
#退出frame方式定位
driver.switch_to.default_content()
driver.switch_to.default_content()
driver.switch_to.default_content()
time.sleep(2)
#点击'其他图书,档案'放大镜
driver.find_element_by_id('tsid_div_tree_12_span').click()
time.sleep(1)
#点击:确定 按键
driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[4]/button[1]').click()
time.sleep(1)
#通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.switch_to.frame("J_iframe_M160103A")
#获取动态的frame(好像区分360浏览器和火狐浏览器)
self.iframe0()
time.sleep(2)
#点击:点击:资产名称 输入框
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[3]/td[2]/input').click()
#输入资产名称:c资产
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[3]/td[2]/input').send_keys(u'c资产')
time.sleep(2)
#*********************************************日期控件**************************************
time.sleep(2)
#点击:取得日期 日期框
driver.find_element_by_css_selector('#EditPanel > div > div > table > tbody > tr:nth-child(4) > td:nth-child(2) > span > span > span').click()
time.sleep(2)
#点击:今天 选项
driver.find_element_by_xpath('/html/body/div[12]/div/div[2]/a[1]').click()
#*********************************************日期控件**************************************
#点击:点击:型号 输入框
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[11]/td[4]/input').click()
#输入型号:88*88
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[11]/td[4]/input').send_keys(u'88*88')
time.sleep(2)
#点击:点击:原值 输入框
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[7]/td[2]/input[1]').clear()
#输入原值:888
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/table/tbody/tr[7]/td[2]/input[1]').send_keys(u'888')
time.sleep(2)
#点击:经费来源 下拉框
driver.find_element_by_css_selector('#EditPanel > div > div > table > tbody > tr:nth-child(9) > td:nth-child(2) > span > span > span').click()
time.sleep(2)
#点击:创新强校 选项
driver.find_element_by_xpath('//div[text()="创新强校"]').click()
time.sleep(2)
#10000足够大,代表拖到底部。值设为0代表顶部
js="var q=document.documentElement.scrollTop=10000"
driver.execute_script(js)
time.sleep(2)
#*********************************************下拉框**************************************
#点击:行业资产用途 下拉框
driver.find_element_by_css_selector('#EditPanel > div > div > table > tbody > tr:nth-child(17) > td:nth-child(2) > span > span > span').click()
time.sleep(2)
#点击:(行业)科研 选项
driver.find_element_by_xpath('//div[text()="(行业)科研"]').click()
#*********************************************下拉框**************************************
#点击:资产附件
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[3]/ul/li[2]/a/span[1]').click()
time.sleep(2)
#点击:新增
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div/div/div/div[1]/div/button[1]').click()
#退出frame方式定位
driver.switch_to.default_content()
driver.switch_to.default_content()
driver.switch_to.default_content()
#获取动态的frame(好像区分360浏览器和火狐浏览器)
self.iframe1()
#点击:选择文件
driver.find_element_by_xpath('/html/body/div[1]/table/tbody/tr[2]/td[2]/form/input').send_keys('C:\\Users\\Admin\\Desktop\\控制台输出.sql')
time.sleep(2)
#点击文件框:保存
driver.find_element_by_xpath('/html/body/div[2]/button[1]').click()
#退出frame方式定位
driver.switch_to.default_content()
#通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.switch_to.frame("J_iframe_M160103A")
#获取动态的frame(好像区分360浏览器和火狐浏览器)
self.iframe0()
time.sleep(2)
#点击:资金来源
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[3]/ul/li[6]/a/span[1]').click()
time.sleep(2)
#点击:添加
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[6]/div/div/div[3]/div[1]/button[1]').click()
time.sleep(2)
#点击:项目编号放大镜
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[6]/div/div/div[3]/div[2]/form/div/div/div/div[2]/div[2]/table/tbody/tr/td[3]/div/table/tbody/tr/td/span/span/span').click()
time.sleep(2)
#退出frame方式定位
driver.switch_to.default_content()
driver.switch_to.default_content()
driver.switch_to.default_content()
#获取动态的frame(好像区分360浏览器和火狐浏览器)
self.iframe1()
#选择行号:1 的经费
driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div').click()
time.sleep(2)
#点击:确定
driver.find_element_by_xpath('/html/body/div[3]/button[1]').click()
time.sleep(2)
#退出frame方式定位
driver.switch_to.default_content()
#通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.switch_to.frame("J_iframe_M160103A")
#获取动态的frame(好像区分360浏览器和火狐浏览器)
self.iframe0()
time.sleep(2)
#使用金额输入值
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[6]/div/div/div[3]/div[2]/form/div/div/div/div[2]/div[2]/table/tbody/tr/td[5]/div/table/tbody/tr/td/input[1]').send_keys('888')
#点击:保存
driver.find_element_by_xpath('/html/body/div[3]/button[2]').click()
time.sleep(2)
#退出frame方式定位
driver.switch_to.default_content()
driver.switch_to.default_content()
driver.switch_to.default_content()
#点击:确定
driver.find_element_by_xpath('/html/body/div[7]/div[3]/a[1]').click()
time.sleep(2)
#通过两层frame方式定位
#获取动态的frame(好像区分360浏览器和火狐浏览器)
self.iframe1()
#点击:返回
driver.find_element_by_xpath('/html/body/div/div[3]/input[3]').click()
#退出frame方式定位
driver.switch_to.default_content()
# 调用退出模块
quit(self)
if __name__ == '__main__':
unittest.main()
test_addPerson.py
#coding=utf-8
from selenium import webdriver
import unittest
import time
#导入登录,退出模块
from test_case.public.login import test_Login
from test_case.public.quit import quit
#测试用例1(新增人员)
class addperson(unittest.TestCase):
# 获取动态的frame0
def iframe0(self):
time.sleep(2)
# 获取动态的frame0
iframe0 = self.driver.find_elements_by_tag_name("iframe")
r0 = iframe0[0].get_attribute('id')
print('动态frame0:%s' % r0)
self.driver.switch_to.frame(r0)
# 获取动态的frame1 这个主要是360浏览器和火狐浏览器所获取到的iframe的值不一致
def iframe1(self):
time.sleep(2)
# 获取动态的frame1
iframe1 = self.driver.find_elements_by_tag_name("iframe")
r1 = iframe1[1].get_attribute('id')
print('动态frame1:%s' % r1)
self.driver.switch_to.frame(r1)
#登录
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.url = 'http://localhost:8087/t6/'
time.sleep(2)
def test_addPerson(self):
time.sleep(2)
u"""新增人员"""
driver = self.driver
self.driver.get(self.driver.url)
#调用登录模块
test_Login(self)
# 通过两层frame方式定位
driver.switch_to.frame("id_iframe")
# 进入实验室管理系统 吸取的教训2:始终定位不成功的时候可以借助火狐浏览器的插件,比如:katalon 定位一遍,复制定位到的元素
driver.find_element_by_xpath("(//div[@id='MENU_SYSGL']/span[2])[2]").click()
time.sleep(2)
#实验室管理
driver.find_element_by_xpath("(//div[@id='M190226A']/div[2]/span)[2]").click()
time.sleep(2)
#实验室人员信息
driver.find_element_by_xpath("(//div[@id='M190226AC']/div[2])[2]").click()
time.sleep(2)
#实验室人员培训管理
driver.find_element_by_xpath("(//div[@id='M190226ACC']/div[2])[2]").click()
time.sleep(2)
#实验室人员培训登记
driver.find_element_by_xpath("(//div[@id='M190226ACCA']/div[2])[2]").click()
time.sleep(2)
#操作指引
driver.find_element_by_xpath('//*[@id="jpwClose"]').click()
# 通过两层frame方式定位
self.driver.switch_to.frame("J_iframe_M190226ACCA")
time.sleep(2)
#点击:新增
driver.find_element_by_id("addNew").click()
time.sleep(2)
# 获取动态的frame0
self.iframe0()
#点击:姓名放大镜
driver.find_element_by_xpath("//form[@id='baseInfoForm']/table/tbody/tr/td[2]/span/span/span").click()
time.sleep(2)
# 退出两层frame方式定位
self.driver.switch_to.default_content()
self.driver.switch_to.default_content()
self.driver.switch_to.default_content()
# 获取动态的frame1
self.iframe1()
#人员选择 第一行数据
driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div').click()
time.sleep(2)
driver.find_element_by_xpath("//button[@id='id_btn_sure']/i").click()
time.sleep(2)
# 退出两层frame方式定位
self.driver.switch_to.default_content()
# 通过两层frame方式定位
self.driver.switch_to.frame("id_iframe")
self.driver.switch_to.frame("J_iframe_M190226ACCA")
# 获取动态的frame1
self.iframe0()
driver.find_element_by_xpath("//form[@id='baseInfoForm']/table/tbody/tr[5]/td[2]/span/span/span").click()
time.sleep(2)
driver.find_element_by_xpath("//div[2]/table/tbody/tr[4]/td[4]").click()
time.sleep(2)
driver.find_element_by_xpath("//form[@id='baseInfoForm']/table/tbody/tr[5]/td[4]/span/span/span").click()
time.sleep(2)
driver.find_element_by_xpath("//div[5]/div/div/div/div[2]/table/tbody/tr[4]/td[5]").click()
time.sleep(2)
driver.find_element_by_id("id_mobilePhone").click()
time.sleep(2)
driver.find_element_by_id("id_mobilePhone").clear()
time.sleep(2)
driver.find_element_by_id("id_mobilePhone").send_keys("13886453547")
time.sleep(2)
driver.find_element_by_id("id_projectName").click()
time.sleep(2)
driver.find_element_by_id("id_projectName").clear()
time.sleep(2)
driver.find_element_by_id("id_projectName").send_keys(u"自动化项目名称1")
time.sleep(2)
#iframe 网页上没有id值的情况下,通过下面方式获取
iframe = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[2]/iframe')
driver.switch_to.frame(iframe)
driver.find_element_by_xpath('/html/body').click()
driver.find_element_by_xpath('/html/body').send_keys('自动化项目内容1')
time.sleep(2)
# 退出两层frame方式定位
driver.switch_to.default_content()
# 退出两层frame方式定位
self.driver.switch_to.default_content()
self.driver.switch_to.default_content()
self.driver.switch_to.default_content()
# 通过两层frame方式定位
self.driver.switch_to.frame("id_iframe")
self.driver.switch_to.frame("J_iframe_M190226ACCA")
# 获取动态的frame1
self.iframe0()
driver.find_element_by_xpath("/html/body/div[3]/button[1]").click()
time.sleep(2)
# 退出两层frame方式定位
self.driver.switch_to.default_content()
self.driver.switch_to.default_content()
self.driver.switch_to.default_content()
driver.find_element_by_link_text(u"确定").click()
time.sleep(2)
driver.find_element_by_link_text(u"确定").click()
time.sleep(2)
#调用退出模块
quit(self)
if __name__ == '__main__':
unittest.main()
login.py
#coding=utf-8
from selenium import webdriver
import time
#输入账号和密码,点击登录
def test_Login(self):
print('输入账号和密码,点击登录')
driver = self.driver
# 窗口最大化
driver.maximize_window()
# 设置隐藏等待时间
driver.implicitly_wait(10)
driver.find_element_by_id('showAccount').click()
driver.find_element_by_id('userAccount').send_keys(u'ctt')
time.sleep(2)
driver.find_element_by_id('showPassword').click()
driver.find_element_by_id('userPassword').send_keys(u'123456')
time.sleep(2)
driver.find_element_by_id('dengluBtn').click()
time.sleep(5)
#通过两层frame方式定位
driver.switch_to.frame("id_iframe")
#最新出来的一个快捷功能,操作指引,很烦这玩意
driver.find_element_by_xpath('//*[@id="jpwClose"]').click()
time.sleep(5)
# 退出两层frame方式定位
driver.switch_to.default_content()
quit.py
#coding=utf-8
from selenium import webdriver
def quit(self):
self.driver.quit()
目录结构: