Python网络数据采集7-单元测试与Selenium自动化测试
Python网络数据采集7-单元测试与Selenium自动化测试
单元测试
Python中使用内置库unittest可完成单元测试。只要继承unittest.TestCase类,就可以实现下面的功能。
- 为每个单元测试的开始和结束提供
setUp和tearDown函数。 - 提供不同类型的断言让测试成功或者失败
- 所有以
test_打头的函数,都会当成单元测试来运行,他们彼此独立,互不影响。
下面来看一个简单的例子
import unittest
class TestSimple(unittest.TestCase):
def setUp(self):
print('set up')
def test_simple(self):
a = 2
l = [2, 3, 43]
self.assertIn(a, l)
def tearDown(self):
print('teardown')
if __name__ == '__main__':
unittest.main(argv=['ignored', '-v'], exit=False)
test_simple (__main__.TestSimple) ...
set up
teardown
ok
----------------------------------------------------------------------
Ran 1 test in 0.006s
OK
在Jupyter中,main()需要填入以上参数才能运行,参数列表中第一个参数会被忽略,而exit=False则不会kill掉kernel。详见stackoverflow
但是在Pycharm中运行则不会任何参数。
测试维基百科
将Python的单元测试和网络爬虫结合起来,就可以实现简单的网站前端功能测试。
import requests
from bs4 import BeautifulSoup
import unittest
class TestWiki(unittest.TestCase):
soup = None
def setUp(self):
global soup
r = requests.get('https://en.wikipedia.org/wiki/Monty_Python')
soup = BeautifulSoup(r.text, 'lxml')
def test_title(self):
global soup
title = soup.h1.string
self.assertEqual(title, 'Monty Python')
def test_content_exists(self):
global soup
content = soup.find('div', id='mw-content-text')
self.assertIsNotNone(content)
if __name__ == '__main__':
unittest.main(argv=['ignored', '-v'], exit=False)
test_simple (__main__.TestSimple) ... ok
test_content_exists (__main__.TestWiki) ...
set up
teardown
D:\Anaconda3\lib\site-packages\bs4\builder\_lxml.py:250: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() or inspect.getfullargspec()
self.parser.feed(markup)
ok
test_title (__main__.TestWiki) ... ok
----------------------------------------------------------------------
Ran 3 tests in 3.651s
OK
Selenium单元测试
如果使用selenium进行网站测试呢?(它的初衷就是用来干这个的)
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path=r'C:\Program Files (x86)\phantomjs\bin\phantomjs.exe')
driver.get('https://en.wikipedia.org/wiki/Monty_Python')
print(driver.title)
assert 'Monty Python' in driver.title
Monty Python - Wikipedia
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path=r'C:\Program Files (x86)\phantomjs\bin\phantomjs.exe')
driver.get('http://pythonscraping.com/pages/files/form.html')
# 找到输入框和提交按钮
first_name = driver.find_element_by_name('firstname')
last_name = driver.find_element_by_name('lastname')
submit = driver.find_element_by_id('submit')
# 输入
first_name.send_keys('admin')
last_name.send_keys('Sun')
# 提交
submit.click()
print(driver.find_element_by_tag_name('body').text)
driver.close()
Hello there, admin Sun!
或者使用动作链。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
driver = webdriver.PhantomJS(executable_path=r'C:\Program Files (x86)\phantomjs\bin\phantomjs.exe')
driver.get('http://pythonscraping.com/pages/files/form.html')
# 找到输入框和提交按钮
first_name = driver.find_element_by_name('firstname')
last_name = driver.find_element_by_name('lastname')
submit = driver.find_element_by_id('submit')
actions = ActionChains(driver).send_keys_to_element(first_name, 'admin') \
.send_keys_to_element(last_name, 'Sun') \
.send_keys(Keys.ENTER)
# 执行动作链
actions.perform()
print(driver.find_element_by_tag_name('body').text)
driver.close()
Hello there, admin Sun!
除了简单的单击双击,发送文本到输入框,还能实现复杂的动作。比如拖放。
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Firefox()
driver.get('http://pythonscraping.com/pages/javascript/draggableDemo.html')
print(driver.find_element_by_id('message').text)
element = driver.find_element_by_id('draggable')
target = driver.find_element_by_id('div2')
actions = ActionChains(driver).drag_and_drop(element, target)
actions.perform()
print(driver.find_element_by_id('message').text)
driver.close()
Prove you are not a bot, by dragging the square from the blue area to the red area!
You are definitely not a bot!
上面的代码用FireFox可以成功,Chrome和PhantomJs不成功,不知道为什么。先是打印了未拖拽方块时候显示的文字,当将方块拖到下面的div区域之后,页面上的文字变化了。
Selenium还有个有意思的功能--截屏。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
driver = webdriver.PhantomJS(executable_path=r'C:\Program Files (x86)\phantomjs\bin\phantomjs.exe')
driver.get('https://www.pythonscraping.com/')
driver.get_screenshot_as_file('screenshot.png')
True
保存成功就会返回打印True。
Selenium与单元测试结合
还是面拖拽的例子,加入了Python单元测试与Selenium结合使用。
from selenium import webdriver
from selenium.webdriver import ActionChains
import unittest
class TestSelenium(unittest.TestCase):
driver = None
def setUp(self):
global driver
driver = webdriver.Firefox()
driver.get('http://pythonscraping.com/pages/javascript/draggableDemo.html')
def test_drag(self):
global driver
element = driver.find_element_by_id('draggable')
target = driver.find_element_by_id('div2')
actions = ActionChains(driver).drag_and_drop(element, target)
actions.perform()
self.assertEqual('You are definitely not a bot!', driver.find_element_by_id('message').text)
if __name__ == '__main__':
unittest.main(argv=['ignored', '-v'], exit=False)
test_drag (__main__.TestSelenium) ... ok
test_simple (__main__.TestSimple) ... ok
test_content_exists (__main__.TestWiki) ...
set up
teardown
D:\Anaconda3\lib\site-packages\bs4\builder\_lxml.py:250: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() or inspect.getfullargspec()
self.parser.feed(markup)
ok
test_title (__main__.TestWiki) ... ok
----------------------------------------------------------------------
Ran 4 tests in 23.923s
OK
依然得使用FireFox, 以及main中加上参数argv=['ignored', '-v'], exit=False。看打印结果,上面的测试也执行了,好像在Jupyter中,虽然在一个Cell中执行单元测试,所有测试都会得到执行。普通Python代码就不是这样,每个Cell独立运行,还可以使用上面Cell定义过的变量和已经导入的库。
by @sunhaiyu
2017.7.19
Python网络数据采集7-单元测试与Selenium自动化测试的更多相关文章
- [python] 网络数据采集 操作清单 BeautifulSoup、Selenium、Tesseract、CSV等
Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesseract.CSV等 Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesse ...
- 笔记之Python网络数据采集
笔记之Python网络数据采集 非原创即采集 一念清净, 烈焰成池, 一念觉醒, 方登彼岸 网络数据采集, 无非就是写一个自动化程序向网络服务器请求数据, 再对数据进行解析, 提取需要的信息 通常, ...
- Python网络数据采集PDF高清完整版免费下载|百度云盘
百度云盘:Python网络数据采集PDF高清完整版免费下载 提取码:1vc5 内容简介 本书采用简洁强大的Python语言,介绍了网络数据采集,并为采集新式网络中的各种数据类型提供了全面的指导.第 ...
- Python网络数据采集6-隐含输入字段
Python网络数据采集6-隐含输入字段 selenium的get_cookies可以轻松获取所有cookie. from pprint import pprint from selenium imp ...
- Python网络数据采集4-POST提交与Cookie的处理
Python网络数据采集4-POST提交与Cookie的处理 POST提交 之前访问页面都是用的get提交方式,有些网页需要登录才能访问,此时需要提交参数.虽然在一些网页,get方式也能提交参.比如h ...
- Python网络数据采集3-数据存到CSV以及MySql
Python网络数据采集3-数据存到CSV以及MySql 先热热身,下载某个页面的所有图片. import requests from bs4 import BeautifulSoup headers ...
- Python网络数据采集2-wikipedia
Python网络数据采集2-wikipedia 随机链接跳转 获取维基百科的词条超链接,并随机跳转.可能侧边栏和低栏会有其他链接.这不是我们想要的,所以定位到正文.正文在id为bodyContent的 ...
- Python网络数据采集1-Beautifulsoup的使用
Python网络数据采集1-Beautifulsoup的使用 来自此书: [美]Ryan Mitchell <Python网络数据采集>,例子是照搬的,觉得跟着敲一遍还是有作用的,所以记录 ...
- Python网络数据采集PDF
Python网络数据采集(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/16c4GjoAL_uKzdGPjG47S4Q 提取码:febb 复制这段内容后打开百度网盘手 ...
随机推荐
- 原生js在绑定事件时不用获取id名可直接用id名绑定???
<div id="btn"> 点击 </div> <script> btn.onclick=function(){ console.log(bt ...
- Transparent Application Failover 透明应用切换
官方文档地址 http://docs.oracle.com/cd/E11882_01/network.112/e41945/advcfg.htm#NETAG455 About Transparent ...
- vue setTimeout用法 jquery滚动到某一个div的底部
//vue 中setTimeOut用法 var $this = this; setTimeout(function(){ $this.goEnd() }, 10); goEnd:function(){ ...
- 学编程担心自己英语不好吗?(IT软件开发常用英语词汇)
发一份,我们导师的收集的常用词汇,与大家共享 欢迎加入Java学习交流裙六一六九五九四四四! S 欢迎加入Java学习交流裙 六一六 九五九 四四四!
- Example004自动关闭的广告窗口
<!-- 实例004自动关闭的广告窗口--> <!-- 3秒后关闭 --> <body onload="window.setTimeout('window.cl ...
- Linux编程之epoll
现在有这么一个场景:我是一个很忙的大老板,我有100个手机,手机来信息了,我的秘书就会告诉我"老板,你的手机来信息了."我很生气,我的秘书就是这样子,每次手机来信息就只告诉我来信息 ...
- div自身高度、屏幕高度
获取元素高度 scrollWidth //显示当前元素的宽度 scrollHeight //显示当前元素的高度 scrollLeft //显示当前元素的左边距左侧的距离 scroll ...
- js把时间戳转换为普通日期格式
第一种 function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1 ...
- 使用python操作mysql数据库
这是我之前使用mysql时用到的一些库及开发的工具,这里记录下,也方便我查阅. python版本: 2.7.13 mysql版本: 5.5.36 几个python库 1.mysql-connector ...
- DOM编程从入门到忘记
文档对象模型(Document Object Model,DOM)是使用 W3C 定义的 API (Application Program Interface) 来操作 HTML 文档 (此处不局限于 ...