【功能自动化】使用测试套件运行测试函数
1.创建registers.py
将registers.py放在文件夹下
registers.py
代码实现
# 导入包
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import unittest
import parameterized
driver = None
file = open(r"user.txt")
data = []
for row in file:
t = row.split()
data.append(t)
class Reg(unittest.TestCase):
@classmethod
def setUpClass(cls):
global driver
driver = webdriver.Chrome()
@classmethod
def tearDownClass(cls):
driver.quit()
# def setUp(self):
# driver.get("http://127.0.0.1:1080/WebTours/")
# sleep(3)
def testOpenWeb(self):
u'''打开网站'''
driver.get("http://127.0.0.1:1080/WebTours/")
# 等待3秒
sleep(3)
driver.switch_to.default_content()
driver.switch_to_frame("body")
driver.switch_to_frame("info")
actual = driver.find_element_by_tag_name("body").text
expect = 'Welcome to the Web Tours site.'
assert expect in actual,"实际结果:"+actual
@parameterized.parameterized.expand(data)
def testRegister(self,yhm,mm):
u'''注册账号'''
driver.get("http://127.0.0.1:1080/WebTours/")
sleep(1)
driver.switch_to.default_content()
driver.switch_to.frame("body")
driver.switch_to.frame("info")
driver.find_element_by_link_text("sign up now").click()
sleep(3)
# 跳转页面,还是需要切换框架
driver.switch_to.default_content()
driver.switch_to.frame("body")
driver.switch_to.frame("info")
driver.find_element_by_name("username").send_keys(yhm)
driver.find_element_by_name("password").send_keys(mm)
driver.find_element_by_name("passwordConfirm").send_keys(mm)
driver.find_element_by_name("register").click()
sleep(3)
driver.switch_to.default_content()
driver.switch_to.frame("body")
driver.switch_to.frame("info")
expect = "Thank you, "+yhm
actual = driver.find_element_by_tag_name("body").text
assert expect in actual,"实际结果:"+actual
if __name__=="__main__":
# verbosity=2 显示详细信息,可以省略,测试信息不详细
unittest.main(verbosity=2)
2.使用测试套件运行测试函数
代码实现
import unittest
from test.registers import Reg
suite = unittest.TestSuite()
suite.addTest(Reg("testOpenWeb"))
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
3.运行结果