30、Selenium + Python 实现 UI 自动化测试-正片2
来源:互联网 发布:单片机控制电机电路图 编辑:程序博客网 时间:2024/05/17 05:57
问题:
1、当用例越来越多,代码势必越来越大,这样太不易读和维护了。并且从用例角度来说,登录(各种情况输入、输出)放在一个文件比较合适,新建项目(成功 or 失败等各种情况)用例放在一个文件比较合适。
本节我们引入Python unittest 单元测试框架。将之前的脚本拆分成两个文件,并且使用unittest 所提供的断言。
一、拆分代码,将测试登录和测试新建项目的代码放到不同的文件,这样代码就不会无限大。并且引入 unittest ,可以方便统计测试结果。
文件1:test_login.py,用于测试登录的逻辑
import unittestfrom selenium import webdriverimport timefrom time import sleepclass TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(20) self.driver.maximize_window() self.driver.get("http://localhost:81/redmine/login") def test_login(self): self.driver.find_element_by_id('username').clear() self.driver.find_element_by_id('username').send_keys('admin') self.driver.find_element_by_id('password').clear() self.driver.find_element_by_id('password').send_keys('test1234') self.driver.find_element_by_id('login-submit').click() # 如何判断是否登录成功呢?登录成功后右上角有用户名 ele = self.driver.find_element_by_xpath('//*[@id="loggedas"]/a').text self.assertEqual(ele,"admin") def tearDown(self): self.driver.quit()if __name__ == '__main__': TestLogin()
文件2:test_new_pj.py,用于测试新建项目的逻辑
import unittestfrom selenium import webdriverimport timefrom time import sleepclass TestNewProject(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(20) self.driver.maximize_window() self.driver.get("http://localhost:81/redmine/login") self.driver.find_element_by_id('username').clear() self.driver.find_element_by_id('username').send_keys('admin') self.driver.find_element_by_id('password').clear() self.driver.find_element_by_id('password').send_keys('test1234') self.driver.find_element_by_id('login-submit').click() def test_new_pj(self): project_name = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) project_id = "bs" + project_name # 标识要求以字母开头 self.driver.find_element_by_xpath('//*[@id="top-menu"]/ul/li[3]/a').click() # 点击左上角“项目” self.driver.find_element_by_xpath('//*[@id="content"]/div[1]/a').click() # 点击新建项目 self.driver.find_element_by_id('project_name').send_keys(project_name) # 输入项目名称 self.driver.find_element_by_id('project_identifier').send_keys(project_id) # 输入项目标识 js1 = "window.scrollTo(0,document.body.scrollHeight)" self.driver.execute_script(js1) # 创建按钮不在当前屏幕内,需要先滑动滚动条 self.driver.find_element_by_name('commit').click() # 点击创建按钮 sleep(2) ele1 = self.driver.find_element_by_id('flash_notice') self.assertEqual(ele1.text,'创建成功') def tearDown(self): self.driver.quit()if __name__ == '__main__': TestNewProject()
单独运行文件1和文件2,执行成功。
二、引入测试套件
上面的测试用例是分开存储了,但是我们期望的是执行全部的用例不能一条条执行吧。所以引入testsuit概念。
新创建一个文件run.py,内容如下:
import unittestif __name__ == '__main__': testdir = "./" discover = unittest.defaultTestLoader.discover(start_dir=testdir,pattern='test*.py') runner = unittest.TextTestRunner() runner.run(discover)运行结果:
C:\Python36\python.exe E:/python/test1/first/run.py
..
----------------------------------------------------------------------
Ran 2 tests in 31.894s
OK
Process finished with exit code 0
三、简单总结一下
1、测试用例名必须以test开头,因为我们通过discover(start_dir=testdir,pattern='test*.py'),pattern去找的测试用例。
2、测试用例中,class TestLogin(unittest.TestCase),class必须继承unittest.TestCases,注意这个类名以Test开头后面每个单词字母大写;
3、def test_login(self),定义的测试用例以小写字母test开头,用下划线分隔单词
更多单元测试的知识,请看unittest 文章
- 30、Selenium + Python 实现 UI 自动化测试-正片2
- 29、Selenium + Python 实现 UI 自动化测试-正片1
- 31、Selenium + Python 实现 UI 自动化测试-正片3
- 32、Selenium + Python 实现 UI 自动化测试-正片4
- 33、Selenium + Python 实现 UI 自动化测试-正片5
- 34、Selenium + Python 实现 UI 自动化测试-正片6
- 35、Selenium + Python 实现 UI 自动化测试-正片7
- 2、Selenium + Python 实现 UI 自动化测试-第一个自动化测试脚本
- 1、Selenium + Python 实现 UI 自动化测试-环境搭建
- 3、Selenium + Python 实现 UI 自动化测试-元素定位
- 4、Selenium + Python 实现 UI 自动化测试-定位元素吗?
- 6、Selenium + Python 实现 UI 自动化测试-鼠标操作
- 7、Selenium + Python 实现 UI 自动化测试-键盘操作
- 8、Selenium + Python 实现 UI 自动化测试-操作浏览器标签
- 9、Selenium + Python 实现 UI 自动化测试-操作浏览器窗口
- 10、Selenium + Python 实现 UI 自动化测试-操作输入框
- 12、Selenium + Python 实现 UI 自动化测试-操作下拉列表
- 13、Selenium + Python 实现 UI 自动化测试-操作表格
- 利用vnc在ubuntu14.04搭建服务器方法步骤简述——服务器端
- LeetCode.216 Combination Sum III
- limit的用法
- 贝塞尔曲线简介
- JAVA课程学习三:查询日历表
- 30、Selenium + Python 实现 UI 自动化测试-正片2
- Python的Profile概述
- xxl-job源码解析
- C/C++ 预处理器
- Java-反射(三) 获取类的常用信息
- TreeSet实现原理
- sass for循环中变量的计算
- WEB项目禁止外部访问某一文件
- 浅析比特币的找零机制