前面有一篇对于常见元素的识别和操作的python自动化脚本,这一篇就接着聊下python的类继承,已经它的第三款unittest框架,和报告收集包HTMLtestRunner的应用。

还是直接上代码吧。

unittest和HTMLtestrunner的应用:

# coding = utf-8
import time
import unittest
import HTMLTestRunner
import os
import sys
from selenium import webdriver

# 继承unittest类
class testClass(unittest.TestCase):
    # unittest提供的初始化方法,setup,可以在这里进行一些初始化的准备工作
def setUp(self):
print "setup"
self.driver=webdriver.Firefox()
self.driver.get("http://www.baidu.com")
time.sleep(3)

    # 下面是测试方法
def testsearch2(self):
input=self.driver.find_element_by_id('kw')
search=self.driver.find_element_by_id('su')
input.send_keys("byebye")
search.click()
def testsearch(self):
input=self.driver.find_element_by_id('kw')
search=self.driver.find_element_by_id('su')
input.send_keys("hello")
search.click()
print "assertion" self.assertTrue(search.is_displayed(),"baidu yixia should display")

    # 运行结束后的处理动作
def tearDown(self):
print 'test down...'
#driver.quit()
self.driver.close() if __name__ == '__main__':
#unittest.main(), 这里要说明一下, 如果测试方法是以test开头的,那么unittest可以识别出来,这里就可以直接调用它的main方法来执行所有测试方法了,运行顺序就是按测试方法的名字排序
#unittest.TestCase.assertTrue()     #这块是获取项目路径,后面在生成report时会放到改目录下
current_path=os.getcwd()
print 'current path: ',current_path
project_path=os.path.dirname(current_path)
print "project path:",project_path

    #这块是通过unittest的testsuite方式来组织测试
testsuite=unittest.TestSuite()
#testsuite.addTest(testClass("testsearch2"))
testsuite.addTest(testClass("testsearch"))
temp=str(time.time()) filedir=project_path+"//report//"+temp
os.makedirs(filedir)
filename="//pyresult.html"
filepath=filedir+filename
fp=file(filepath,'wb')     # 调用HTMLtestrunner来执行脚本并生成测试报告,html格式的
runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='report',description='demo')
runner.run(testsuite)

注: 上面带#号的都是注释, 由于直接编辑的原因,所以他没有显示成绿色,大家如果要复制出来,记得注意调整一下。

Python类的继承:

#coding=utf-8
from pyse import Pyse
from time import sleep class Page(object):
#login_url = 'http://www.126.com' def __init__(self, selenium_driver, base_url, parent=None):
self.base_url = base_url
self.driver = selenium_driver
self.timeout = 30
self.parent = parent def _iopen(self,url):
#url = self.base_url + url
self.base_url=url
self.driver.open(url)
assert self.on_page(),'Did not land on %s' % url def iopen(self):
self._iopen(self.url) def on_page(self):
print 'get_url is: ',self.driver.get_url()
print 'base_url is: ',self.base_url
return self.driver.get_url() == self.base_url class login(Page):
def systemout(self):
print 'pass' def testlogin():
driver=Pyse('ff')
print 'get in'
url='http://www.baidu.com/'
lg=login(driver,'')
lg._iopen(url) if __name__=='__main__':
testlogin()

这块是python类的继承,page继承根object类,然后login继承了page类,然后login就可以使用page类的方法了。

这里要说明的是,page类有2个方法,一个是_iopen(), 另一个是iopen(), 他们的区别就是,在用login的对象调用时:

调用iopen,不需要url参数,但是需要login类有url的成员,因为在其父类page里的self.url其实就是调用login自己的url成员。

而调用_iopen时,可以直接传递一个url参数。

第二个UI脚本--Python+selenium之unittest+HTMLtestRunner及python的继承的更多相关文章

  1. python selenium 使用unittest 示例

    python selenium 使用unittest 示例 并等待某个元素示例 from selenium.webdriver.support.ui import WebDriverWait from ...

  2. Python+Selenium框架unittest执行脚本方法之discover()方法

    继续接着介绍,如何利用unittest管理和执行测试用例的问题,这里我们还是利用之前已经有的三条测试用例,如果你跳过了前面文章,请回到框架设计篇的第八篇和第七篇,里面有相关测试类的文件.本文来介绍,如 ...

  3. Python+Selenium框架-unittest执行脚本方法之addTest

    本文开始介绍如何通过unittest来管理和执行测试用例,这一篇介绍unittest下addTest()方法来加载测试用例到测试套件中去.为了演示效果,我在前面文章的脚本基础上,新建了一个测试脚本,这 ...

  4. Python+selenium之unittest单元测试(3)关于测试用例执行的顺序

    一.测试用例执行的顺序 用例的执行顺序涉及多个层级,在多个测试目录的情况下,先执行哪个目录?在多个测试文件的情况下,先执行哪个文件?在多个测试类的情况下,先执行哪个测试类?,在多个测试方法(用例)的情 ...

  5. 一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

    Web登录测试是很常见的测试!手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文作者就用python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动 ...

  6. 一次简单完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

    Web登录测试是很常见的测试,手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文就基于python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动化 ...

  7. Python+selenium之读取配置文件内容

    Python+selenium之读取配置文件内容 Python支持很多配置文件的读写,此例子中介绍一种配置文件的读取数据,叫ini文件,python中有一个类ConfigParser支持读ini文件. ...

  8. Python+Selenium(1)- 环境搭建

    一,Selenium 简介 Selenium是目前最流行的web自动化测试工具,也常用于网络爬虫,已经更新到3以上的版本. 1,组件 它提供了以下web自动化测试组件: Selenium IDE,Fi ...

  9. Python+selenium之带unittest的脚本分析

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...

随机推荐

  1. 自由软件VS开源软件

    自由软件VS开源软件 “自由软件运动”是一项倡导软件这种知识产品应该免费共享的社会运动,它主要是从社会伦理学,道德的高度,强调我们每个人都有自由使用软件的权利.这种权利不应该被软件私有所破坏. 反对软 ...

  2. Centos 7下安装Oracle 12c

    SQL Server玩了有些年,最近想玩玩Oracle,于是想到装一台Oracle server来玩玩.第一次在Linux下安装Oracle,整个过程参考了一篇文章:http://blog.csdn. ...

  3. HDU 2852 KiKi's K-Number 树状数组 + 二分

    一共最多才100000个数,并且数值范围0~100000. 树状数组 C[i] 记录数值为 i 的数有多少个. 删除时如果Query( a ) - Query( a - 1 ) == 0 则该数不存在 ...

  4. Ubuntu 12.04 LTS(64bit) 环境下JDK、 Eclipse、 ADT、 快捷图标

    一.在FriendlyARM,Tiny4412,,安装包下可补充: (按照手册添加openjdk-6-jdk 后) 安装JDK (Java),选择需要的JDK,或者全部安装. a) OpenJDK-6 ...

  5. leetcode:Search for a Range(数组,二分查找)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  6. leetcode:Intersection of Two Linked Lists(两个链表的交叉点)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. D3D渲染流程--转载

    http://www.cnblogs.com/ixnehc/articles/1282350.html 先从最基础的写起吧,关于Device的渲染流程. D3D9的Device就是D3D给我们提供的一 ...

  8. Oracle数据库之二

    SELECT查询 函数分为: 单行函数 -- 一条记录进入,一条记录输出 多行函数(分组函数)-- 多条记录进入,按组输出 单行函数: select id,first_name,nvl(commiss ...

  9. C# 对List成员排序的简单方法

    网上看到的方法,实在太方便了,转过来保存,原链接: http://blog.csdn.net/wanzhuan2010/article/details/6205884 using System; us ...

  10. VMware11安装Mac OS X10提示不可恢复错误解决

    VMware11安装Mac OS X10提示不可恢复错误(vcpu-0)怎么办?本文将详细介绍如何解决VMware11安装Mac OS X10提示不可恢复错误. 工具/原料   VMware11 PC ...