python selenium --unittest 框架
转自:http://www.cnblogs.com/fnng/p/3300788.html
学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本。相信不少新手学习selenium 也是从IED 开始的。
IDE学习参考:
菜鸟学自动化测试(一)----selenium IDE
借助IED 录制脚本
将脚本导出,保存为baidu.py ,通过python IDLE编辑器打开。如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re class Baidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com/"
self.verificationErrors = []
self.accept_next_alert = True def test_baidu(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").send_keys("selenium webdriver")
driver.find_element_by_id("su").click()
driver.close() def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors) if __name__ == "__main__":
unittest.main()

加入unittest框架后,看上去比我们之前见的脚本复杂了很多,除了中间操作浏览器的几行,其它都看不懂,不要急,我们来分析一下~!
框架分析
import unittest
相想使用unittest框架,首先要引入unittest 包,这个不多解释。
class Baidu(unittest.TestCase):
Baidu类继承unittest.TestCase 类,从TestCase类继承是告诉unittest模块的方式,这是一个测试案例。
def setUp(self):
self.driver = webdriver.Firefox()
self.base_url = "http://www.baidu.com/"
setUp 用于设置初始化的部分,在测试用例执行前,这个方法中的函数将先被调用。这里将浏览器的调用和URL的访问放到初始化部分。
self.verificationErrors = []
脚本运行时,错误的信息将被打印到这个列表中。
self.accept_next_alert = True
是否继续接受下一下警告(字面意思,没找到解释!)
def test_baidu(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").send_keys("selenium webdriver")
driver.find_element_by_id("su").click()
test_baidu中放置的就是我们的测试脚本了,这部分我们并不陌生;因为我们执行的脚本就在这里。
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
is_element_present函数用来查找页面元素是否存在,在这里用处不大,通常删除。
因为判断页面元素是否存在一般都加在testcase中。
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
对弹窗异常的处理

def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True

关闭警告和对得到文本框的处理,如果不熟悉python的异常处理和if 语句的话,请去补基础知识,这里不多解释。
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
tearDown 方法在每个测试方法执行后调用,这个地方做所有清理工作,如退出浏览器等。
self.assertEqual([], self.verificationErrors) 是个难点,对前面verificationErrors方法获得的列表进行比较;如查verificationErrors的列表不为空,
输出列表中的报错信息。
而且,这个东西,也可以将来被你自己更好的调用和使用,根据自己的需要写入你希望的信息。(rabbit 告诉我的)
if __name__ == "__main__":
unittest.main()
unitest.main()函数用来测试 类中以test开头的测试用例
执行结果
这样一一分析下来,我们对unittest 框架有了初步的了解。运行脚本,因为引入了unittest 框架,所以控制台输出了脚本执行情况的信息。

>>> ========================= RESTART ================================
>>>
.
----------------------------------------------------------------------
Ran 1 test in 10.656s OK
>>>

python selenium --unittest 框架的更多相关文章
- Python+Selenium+Unittest框架使用——Selenium——定位元素(二)
1.定位元素(id.name.class.link.partial link) (1)find_element_by_id() 用百度定位测试,用firebug查看定位元素 ,输入框的id为“kw”, ...
- Python+Selenium+Unittest框架使用——Selenium——模拟操作浏览器(三)
1.浏览器大小的控制 Set_window_size()是控制浏览器大小 Maximize_window()浏览器全屏显示 from selenium import webdriver #导入sele ...
- selenium + python自动化测试unittest框架学习(二)
1.unittest单元测试框架文件结构 unittest是python单元测试框架之一,unittest测试框架的主要文件结构: File >report >all_case.py &g ...
- selenium + python自动化测试unittest框架学习(一)selenium原理及应用
unittest框架的学习得益于虫师的<selenium+python自动化实践>这一书,该书讲得很详细,大家可以去看下,我也只学到一点点用于工作中,闲暇时记录下自己所学才能更加印象深刻. ...
- Python+selenium+unittest+HTMLTestReportCN单元测试框架分享
分享一个比较基础的,系统性的知识点.Python+selenium+unittest+HTMLTestReportCN单元测试框架分享 Unittest简介 unittest是Python语言的单元测 ...
- python+selenium+unnittest框架
python+selenium+unnittest框架,以百度搜索为例,做了一个简单的框架,先看一下整个项目目录结构 我用的是pycharm工具,我觉得这个工具是天使,超好用也超好看! 这些要感谢原作 ...
- Python+Selenium+Unittest+Ddt+HTMLReport分布式数据驱动自动化测试框架结构
1.Business:公共业务模块,如登录模块,可以把登录模块进行封装供调用 ------login_business.py from Page_Object.Common_Page.login_pa ...
- python之unittest框架实现接口测试实例
python之unittest框架实现接口测试实例 接口测试的方法有很多种,具体到工具有postman,jmeter,fiddler等,但是工具的局限性是测试数据的组织较差,接口的返回工具的判断有限, ...
- selenium自动化测试、Python单元测试unittest框架以及测试报告和日志输出
部分内容来自:https://www.cnblogs.com/klb561/p/8858122.html 一.基础介绍 核心概念:test case, testsuite, TestLoder,Tex ...
随机推荐
- 【最小割】BZOJ3438-小M的作物(Rank 2???!!!)(含新款Dinic模板)
一开始被T掉了之后,才害怕地发现之前写的Dinic基本上都是错的??!!!正确的写在注释里了,注意一下(;3<)馬鹿やろ 一个丧心病狂的优化前后效率对比:
- bzoj 3728: PA2014Final Zarowki
3728: PA2014Final Zarowki Description 有n个房间和n盏灯,你需要在每个房间里放入一盏灯.每盏灯都有一定功率,每间房间都需要不少于一定功率的灯泡才可以完全照亮.你可 ...
- python基础之re,sys,suprocess模块
re 正则表达式 1.什么是正则? 正则就是用一系列具有特殊含义的字符组成的规则,该规则用来描述具有某一特征的字符串. 正则就是用来在一个大的字符串匹配出符合规则的子字符串 2.为什么用正则? 正则可 ...
- [Luogu2656]采蘑菇
题目大意: 给你一个有向图,每条边有一个边权w以及恢复系数k, 你从s点出发乱走,经过某条边时会获得相应的收益w,而当第二次经过这条边时相应的收益为w*k下取整. 问你最大能获得的收益为多少? 思路: ...
- 检测是否为n的因子 Exercise07_06
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:检测是否为n的因子 * */ public class Exercise07_06 { public static void ...
- NOIP2014 解题报告·水渣记
Day 1: 第一次参加noip.小激动,小紧张,这些正常的情绪就不用说了.唯一值得一提的是 我早上步行去郑大工学院的时候迷路了,直接转进了隔壁的河南农大,绕了半天找不到机房,还给几个同学打了电话可就 ...
- 用asyncio的异步网络连接来获取sina、sohu和163的网站首页
代码如下: import asyncio async def wget(host): print('wget %s...' % host) connect = asyncio.open_connect ...
- “Warning: Call-time pass-by-reference has been deprecated in”解决方法
刚刚在调试一个PHP木马,显示错误信息为: Warning: Call-time pass-by-reference has been deprecated in E:\New-Hack520org\ ...
- HDU 4665 Unshuffle (2013多校6 1011 )
Unshuffle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- MFC获取纸张大小
BOOL CPrintView::GetPageSize(CSize &nRetVal) // CPrintView 是自己创建的类 { PRINTDLG FA ...