from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import Selectfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.common.exceptions import NoAlertPresentExceptionimport unittest, time, reclass 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()

啪啪啪上来就是一串代码,是不是有点懵!,下面小编来解释一下: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.driverdriver.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 Falsereturn Trueis_element_present 函数用来查找页面元素是否存在,try...except....为 python 语言的异常捕捉。is_element_present 函数在这里用处不大,通常删除,因为判断页面元素是否存在一般都加在 testcase 中。

def is_alert_present(self):try: self.driver.switch_to_alert()except NoAlertPresentException, e: return Falsereturn True对弹窗异常的处理

def close_alert_and_get_its_text(self):try:alert = self.driver.switch_to_alert()alert_text = alert.textif self.accept_next_alert:alert.accept()else:alert.dismiss()return alert_textfinally : self.accept_next_alert = True关闭警告以及对得到文本框的处理,if 判断语句前面已经多次使用,并不陌生;try....finally...为 python的异常处理。

def tearDown(self):self.driver.quit()self.assertEqual([], self.verificationErrors)tearDown 方法在每个测试方法执行后调用,这个地方做所有测试用例执行完成的清理工作,如退出浏览器等。

self.assertEqual([], self.verificationErrors)这个是难点,对前面 verificationErrors 方法获得的列表进行比较;如查 verificationErrors 的列表不为空,输出列表中的报错信息

if __name__ == "__main__":unittest.main()unitest.main()函数用来测试 类中以 test 开头的测试用例

这样一一分析下来,我们对unittest  框架有了初步的了解。运行脚本,因为引入了unittest 框架,

所以控制台输出了用例的执行个数、时间以及是否ok 等信息。

本篇只是初步对 unittest 的框架进行了分析, 还有许细节将放在后面章节进行讨论, 例如 python 的
异常处理机制等。

在学习过程中有遇到疑问的,可以加selenium(python+java) QQ群交流:232607095



Selenium2+python自动化30-引入unittest框架的更多相关文章

  1. Selenium2+python自动化30-引入unittest框架【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/unittest/ from selenium import webdriverfrom ...

  2. Selenium2+python自动化52-unittest执行顺序

    前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...

  3. Selenium2+python自动化52-unittest执行顺序【转载】

    前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...

  4. Selenium2+python自动化59-数据驱动(ddt)

    前言 在设计用例的时候,有些用例只是参数数据的输入不一样,比如登录这个功能,操作过程但是一样的.如果用例重复去写操作过程会增加代码量,对应这种多组数据的测试用例,可以用数据驱动设计模式,一组数据对应一 ...

  5. Selenium2+python自动化55-unittest之装饰器(@classmethod)

    前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...

  6. Selenium2+python自动化59-数据驱动(ddt)【转载】

    前言 在设计用例的时候,有些用例只是参数数据的输入不一样,比如登录这个功能,操作过程但是一样的.如果用例重复去写操作过程会增加代码量,对应这种多组数据的测试用例,可以用数据驱动设计模式,一组数据对应一 ...

  7. Selenium2+python自动化55-unittest之装饰器(@classmethod)【转载】

    前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...

  8. Selenium2+python自动化54-unittest生成测试报告(HTMLTestRunner)

    前言 批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的. unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLT ...

  9. Selenium2+python自动化33-文件上传(send_keys)

    前言 文件上传是web页面上很常见的一个功能,自动化成功中操作起来却不是那么简单. 一般分两个场景:一种是input标签,这种可以用selenium提供的send_keys()方法轻松解决: 另外一种 ...

随机推荐

  1. Storm简介

    Storm特性 1. 低延迟和高性能 在一个小集群中,每个节点每秒可以处理数以百万计的消息. 2. 可扩展 在Storm集群中主要有三个实体:工作进程.线程和任务.Storm集群中每台机器上都可以运行 ...

  2. 在package.json里面的script设置环境变量,区分开发及生产环境。注意mac与windows的设置方式不一样

    在package.json里面的script设置环境变量,区分开发及生产环境. 注意mac与windows的设置方式不一样. "scripts": { "publish- ...

  3. 查看并更改mysql编码

    show variables like 'character%'; set character_set_client=utf8 ; set character_set_connection=utf8 ...

  4. 数据库的索引和填充因子fillfactor

    索引分为聚簇索引和非聚簇索引 1.聚簇索引/聚集索引 聚簇索引的顺序就是数据的物理存储顺序,对于一个表来说,只有一个聚簇索引 create unique clustered index id_inde ...

  5. Windows下使用命令行启动php

    E:\xampp\php>php-cgi.exe -b 127.0.0.1:9000 -c php.ini

  6. 数据库连接池dbcp基本配置

    DBCP(DataBase connection pool),数据库连接池.是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件.单独使用dbcp需要2个包: com ...

  7. System.exit(1)

    用于退出java的虚拟机,也是finally块中语句不被执行的唯一情况

  8. git 相关

    github入门   一.先了解 相比CVS\SVN优势: - 支持离线开发,离线Repository- 强大的分支功能,适合多个独立开发者协作- 速度快 github 本地有仓库,储存着所有repo ...

  9. JS适配问题。

    动画requestAnimFrame + cancelAnimationFrame window.requestAnimFrame = (function(){ return window.reque ...

  10. OKR——Objectives and Key Results

    1.OKR天生就有两个典型特征: 1)在精不在多——因为它是用来明确工作重心的(set one's priorities): 2)全体公开.透明——当你能够看到你的同级(peers).小老板(直接上级 ...