Python+selenium之带unittest的脚本分析
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, NoAlertPresentException
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 as e:
return False
return True def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException as 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()
****注:以上代码适用与Python2.7版本,Python3.0版本会报错
import unittest
1.首先导入unittest模块
class BaiduTest(unittest.TestCase)
2.BaiduTest类继承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
1.setUp用于设置初始化工作,在执行每一测试用例前先被执行,它与tearDown方法相呼应,后者在每一个测试用例执行后被执行。初始化工作定义了浏览器启动和基础URL地址。
2.implicitly_wait()设置页面上元素的隐性等待时间为30s
3.verificationErrors数组,脚本运行时的错误信息将被记录到这个数组中。
4.定义accept_next_alert变量,表示是否继续接受下一个警告,初始状态为True
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException as e:
return False
return True
1.is_element_present方法用于查找页面元素是否存在,通过find_element()来接收元素的定位方法(how)和定位值(what)。
如果定位到元素则返回True。否则爆出异常并返回False。try.....except....为Python语言的异常处理。
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException as e:
return False
return True
1.is_alert_prent()方法用于判断当前页面是否存在警告框,利用WebDriver提供的switch_to_alert()方法来捕捉页面上的警告框。
如果捕捉到警告框则返回True。否者跑输NoAlertPrentException类型的异常,并返回False 2.鉴于不管页面是否出现警告框,返回结果都为True。所以可以通过dirver.switch_to_alert().text,用于获取当前页面上的警告提示信息。可以获取到就返回True,获取不到则返回False。
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
1.close_alert_and_get_its_text()关闭警告并获得警告信息。
2.通过switch_to_alert()获得警告,通过text获得警告框的信息。
3.然后通过if语句判断accept_naxt_alert的状态,在setUp()中已经初始化状态为True,如果为True,则通过accept()接受警告,否者dismiss()忽略警告。
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
1.tearDown()方法在每个测试方法执行后调用,这个方法用于测试用例执行后的清理工作。如退出浏览器,关闭驱动,恢复用例执行状态。
2.在setUp()方法中定义了verificationErrors为空数组,这里通过assertEqual()比较其是否为空,如果为空则说明用例执行的过程中没有出现异常,否则将抛出AssertionErrory异常。
if __name__ == "__main__":
unittest.main()
通过unittest。main()方法来运行当前文件中的测试方法,其默认匹配并运行以test开头的方法。
**********************************************************************************************************************************************************************************************************************************
以上代码从《虫师》copy,但是会报如下的错误
解决的办法:注释掉driver.close()
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, NoAlertPresentException
import unittest, time, re class Baidu (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox ()
self.driver.implicitly_wait (30)
self.base_url = "http://XXXXXXXX /oneCard/login"
self.verificationErrors = []
self.accept_next_alert = True def test_baidu(self):
driver = self.driver
driver.get (self.base_url + "/")
driver.find_element_by_name ("username").clear ()
driver.find_element_by_name ("username").send_keys ("super")
driver.find_element_by_name ("password").clear ()
driver.find_element_by_name ("password").send_keys ("XXXXXXX")
driver.find_element_by_xpath ("/html/body/div[1]/div/div/div/div[2]/form/fieldset/div[4]/button").click ()
time.sleep (3) # 进入卡入库界面
driver.find_element_by_xpath ("//*[@id='sidebar-menu']/div/ul/li[1]/a").click ()
driver.find_element_by_xpath ("//*[@id='sidebar-menu']/div/ul/li[1]/ul/li[1]/a").click ()
time.sleep (3) # 点击卡入库,进入卡入库
driver.switch_to.frame (driver.find_element_by_xpath ("//*[@id='tabBody']/div[2]/div/iframe"))
driver.find_element_by_link_text ("入库").click ()
time.sleep (3)
driver.switch_to.default_content ()
driver.switch_to.frame (driver.find_element_by_xpath ("//*[@id='tabBody']/div[3]/div/iframe"))
driver.find_element_by_id ("cardNo").clear ()
driver.find_element_by_id ("cardNo").send_keys ("") time.sleep (3)
driver.find_element_by_id ("cardChipId").clear ()
driver.find_element_by_id ("cardChipId").send_keys ("")
# 输入外部成本 和内部成本
driver.find_element_by_id ("costInternal").clear ()
driver.find_element_by_id ("costInternal").send_keys (1)
time.sleep (2)
driver.find_element_by_id ("costExternal").clear ()
driver.find_element_by_id ("costExternal").send_keys (10)
# 提交
driver.find_element_by_xpath ("//*[@id='btnSubmit']").click () def is_element_present(self, how, what):
try:
self.driver.find_element (by=how, value=what)
except NoSuchElementException as e:
return False
return True def is_alert_present(self):
try:
self.driver.switch_to_alert ()
except NoAlertPresentException as e:
return False
return True def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert ()
alert_text = alert.text
print(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 ()
Python+selenium之带unittest的脚本分析的更多相关文章
- Selenium 2自动化测试实战33(带unittest的脚本分析)
带unittest的脚本分析 #test.py #coding:utf-8 from selenium import webdriver from selenium.webdriver.common. ...
- python +selenium 自带case +生成报告的模板
https://github.com/huahuijay/python-selenium2这个就是 python +selenium的 里面还自带case 然后也有生成报告的模板 我的: https: ...
- 使用Python+selenium实现第一个自动化测试脚本
原blog 一,安装Python. python官方下载地址:https://www.python.org/downloads/ 安装后点击开始菜单,在菜单最上面能找到IDLE. IDLE是pytho ...
- Python&Selenium 数据驱动【unittest+ddt+mysql】
一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用mysql数据库为数据源作为测试输入 二.SQL脚本 # encoding crea ...
- Python&Selenium 数据驱动【unittest+ddt+xml】
一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用xml文件作为数据文件作为测试输入 二.xml文件 <?xml version ...
- Python&Selenium 数据驱动【unittest+ddt+json】
一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用json文件作为数据文件作为测试输入,最后生成html测试报告 二.json文件 [ ...
- 【Python Selenium】简单数据生成脚本
最近因工作需要,写了一个简单的自动化脚本,纯属学习,顺便学习下selenium模块. 废话不多说,直接上代码!! 这里一位大神重写了元素定位.send_keys等方法,咱们直接进行调用. 适用Pyth ...
- Python&Selenium 数据驱动【unittest+ddt+Excel】
一.摘要 一般情况下我们为了更好的管理测试数据会选择将测试数据存储在Excel文件当中去,本节内容将展示给读者将测试数据存储在Excel文档中的案例. 二.创建存储测试数据的Excel 创建一个Exc ...
- Python&Selenium 数据驱动【unittest+ddt+json+HTMLTestRunner】
一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用json文件作为数据文件作为测试输入,最后借助著名的HTMLTestRunner.p ...
随机推荐
- scores
题意: m维偏序问题. 解法: 考虑对每一维按照每一个元素在这一维的数值分块,对于每一个块维护一个大小为 n 的bitset,表示前缀/后缀满足条件的元素集合. 对于每一个询问,我们可以枚举找到相应的 ...
- 虚拟机中的Linux安装VMware Tools
虚拟机中的Linux安装VMware Tools Tools" TITLE="虚拟机中的Linux安装VMware Tools" /> Tools" TI ...
- java之装箱拆箱
参考http://how2j.cn/k/number-string/number-string-wrap/22.html 封装类 所有的基本类型,都有对应的类类型 比如int对应的类是Integer ...
- WCF部署到IIS上调用报错:由于扩展配置问题而无法提供您请求的页面
将WCF部署到全新win7 x64 IIS7.5上访问报错:由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 原因:IIS不识别.sv ...
- Scipy的应用
首先总体概括一下Scipy的用处 >>> #Scipy依赖于numpy>>> #Scipy提供了真正的矩阵>>> #Scipy包含的功能:最优化, ...
- unity coroutine
http://gad.qq.com/article/detail/695 使用Unity 3D引擎的同学,对于Coroutine(协程)的使用肯定也是非常熟悉的了.然而Coroutine背后的技术以及 ...
- VBA for AutoCAD
Download the Microsoft Visual Basic for Applications Module (VBA) 2016 Downloads AutoCAD 2016 VBA mo ...
- Robot Frame应用实例讲解
关键字与变量 内置库(build in)关键字 要使用的其它库关键字(学会看文档)
- 输入apt-get update时出现Could not open lock file /var/lib/apt/lists/lock - open
我看了其它的资料发现不够清楚 我只报这些错误 1.1.ps-aux 查出apt-get进程的PID,通常是一个四位数字. 不好找apt-get进程 输入此代码就好找了 ps -aux|grep apt ...
- java基础第十一篇之Date、Math、自动装箱和拆箱
Date类 表示一个瞬间,就是一个时刻 * * 构造方法: * public Date();//创建一个表示当前系统时间的Date对象 * public Date(long time);//毫秒值,距 ...