最近空闲时间在探索Selenium的自动化测试,简单的写了一个小框架来测试公司的一个web产品。该框架包括以下模块:

1. Test case编写模式(page模式,参考之前的博文http://www.cnblogs.com/AlwinXu/p/5537955.html)

2. Test case的管理及执行 (主要是用nose)

  该模块借助了一个外部txt文件来记录测试用例,每个用例为自身的文件名,如果不需要在本次执行,只需在文件名前添加一个“#”标识符就可以跳过该用例的执行。

3. 测试报告的生成(xml和html两种格式)

对于自动化测试而言,这些模块应该是最基本的配置了,当然还有一些辅助模块比如日志,其他公共库模块等需要根据具体的业务逐渐丰富。闲话少说,用代码交流吧。

测试用例编写

该模块用了Page模式,之前介绍过,这次只贴代码了

BasePage.py:

__author__ = 'xua'

#super class
class BasePage(object):
def __init__(self, driver):
self.driver = driver

然后是各个web page继承BasePage,LoginPage.py:

from BasePage import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys class LoginPage(BasePage):
"""description of class""" #page element identifier
usename = (By.ID,'username')
password = (By.ID, 'password')
dialogTitle = (By.XPATH,'//html/body/div[7]/div/div/div[1]/h3')
cancelButton = (By.XPATH,'//html/body/div[7]/div/div/div[3]/button[2]') #Get username textbox and input username
def set_username(self,username):
name = self.driver.find_element(*LoginPage.usename)
name.send_keys(username) #Get password textbox and input password, then hit return
def set_password(self, password):
pwd = self.driver.find_element(*LoginPage.password)
pwd.send_keys(password + Keys.RETURN) #Get pop up dialog title
def get_DiaglogTitle(self):
digTitle = self.driver.find_element(*LoginPage.dialogTitle)
return digTitle.text #Get "cancel" button and then click
def click_cancel(self):
cancelbtn = self.driver.find_element(*LoginPage.cancelButton)
cancelbtn.click()

测试用例信息类:

TestCaseInfo.py

class TestCaseInfo(object):
"""description of class""" def __init__(self, id="",name="",owner="",result="Failed",starttime="",endtime="",errorinfo=""):
self.id = id
self.name = name
self.owner = owner
self.result = result
self.starttime = starttime
self.endtime = endtime
self.errorinfo = errorinfo

最后是每个测试用例的编写:(每个用例必须有自己的用例信息,这里有ID,Name等等信息,也会调用测试结果报告生成模块来添加测试结果)

Test_Login.py

__author__ = 'xua'

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import unittest
import time
from LoginPage import LoginPage
from TestCaseInfo import TestCaseInfo
from TestReport import TestReport class Test_Login(unittest.TestCase): #Setup
def setUp(self):
self.driver = webdriver.Chrome(r'C:\Users\xua\Downloads\chromedriver_win32\chromedriver.exe')
self.driver.implicitly_wait(30)
self.base_url = "http://10.222.30.145:9000/"
#test case information
self.testcaseinfo = TestCaseInfo(id="",name="Login to floor manager lite using sbxadmin",owner="xua")
self.testResult = TestReport() def test_Login(self):
try:
self.testcaseinfo.starttime = str(time.asctime())
#Step1: open base site
self.driver.get(self.base_url)
#Step2: Open Login page
login_page = LoginPage(self.driver)
#Step3: Enter username
login_page.set_username("sbXadmin")
#Step4: Enter password
login_page.set_password("IGTtest1")
#Checkpoint1: Check popup dialog title
self.assertEqual(login_page.get_DiaglogTitle(),"Sign in","Not Equal")
#Step5: Cancel dialog
login_page.click_cancel()
self.testcaseinfo.result = "Pass"
except Exception as err:
self.testcaseinfo.errorinfo = str(err)
finally:
self.testcaseinfo.endtime = str(time.asctime()) #tearDown
def tearDown(self):
self.driver.close()
#write test result
self.testResult.WriteHTML(self.testcaseinfo) if __name__ == "__main__":
unittest.main()

用例执行模块

1. 借助外部文件记录需要执行的用例

testcases.txt(带“#”标识的用例不会被执行):

Test_Login.py
Test_Login_2.py
#Test_Login_3.py
Test_Login_4.py

2. 利用nose的nosetests命令执行各个用例:

import subprocess

class RunTests(object):
"""description of class"""
def __init__(self):
self.testcaselistfile = "testcases.txt" #use nosetests command to execute test case list
def LoadAndRunTestCases(self):
f = open(self.testcaselistfile)
testfiles = [test for test in f.readlines() if not test.startswith("#")]
f.close()
for item in testfiles:
subprocess.call("nosetests "+str(item).replace("\\n",""),shell = True) if __name__ == "__main__":
newrun = RunTests()
newrun.LoadAndRunTestCases()

测试结果报表生成模块

测试报表模块写了两种格式:xml和html

TestReport.py

from xml.etree import ElementTree as ET
import os
import lxml.etree as mytree
from lxml import html class TestReport(object):
"""description of class"""
def __init__(self):
self.testreport = "TestResult.xml" #If there is no "TestResult.xml", then create one
def CreateTestResultFile(self):
if os.path.exists(self.testreport) == False:
newElem = ET.Element("TestCases")
newTree = ET.ElementTree(newElem)
newTree.write(self.testreport) #Write test result to xml
def WriteResult(self,testcaseInfo):
self.CreateTestResultFile()
testResultFile = ET.parse(self.testreport)
root = testResultFile.getroot()
newElem = ET.Element("TestCase")
newElem.attrib = {
"ID":testcaseInfo.id,
"Name":testcaseInfo.name,
"Owner":testcaseInfo.owner,
"Result":testcaseInfo.result,
"StartTime":testcaseInfo.starttime,
"EndTime":testcaseInfo.endtime,
"ErrorInfo":testcaseInfo.errorinfo
}
root.append(newElem) testResultFile.write(self.testreport) #If there is no "TestResult.html" file exists, then create one with default style
def CreateHtmlFile(self):
if os.path.exists("TestResult.html") == False:
f = open("TestResult.html",'w')
message = """<html>
<head>
<title>Automation Test Result</title>
<style>
table {
border-collapse: collapse;
padding: 15px;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
}
th{
background-color: green;
color: white;
border: 1px solid #ddd;
padding-bottom: 15px;
padding-top: 15px;
}
tr{
border: 1px solid #008000;
padding-bottom: 8px;
padding-top: 8px;
text-align: left;
}
td{
border: 1px solid #008000;
}
</style>
</head>
<body>
<h1>Automation Test Result</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Owner</th>
<th>Result</th>
<th>StartTime</th>
<th>EndTime</th>
<th>ErrorMessage</th>
</tr>
</table>
</body>
</html>
"""
f.write(message)
f.close() #append new test result to testresult file
def WriteHTML(self,testcaseinfo): self.CreateHtmlFile() f = open("TestResult.html","r") htmlcontent = f.read()
f.close()
tree = html.fromstring(htmlcontent)
tableElem = tree.find(".//table")
if testcaseinfo.result == "Failed":
mytablerow = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td bgcolor=\"#FF0000\">{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>".format(testcaseinfo.id,testcaseinfo.name,testcaseinfo.owner,testcaseinfo.result,testcaseinfo.starttime,testcaseinfo.endtime,testcaseinfo.errorinfo)
else:
mytablerow = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>".format(testcaseinfo.id,testcaseinfo.name,testcaseinfo.owner,testcaseinfo.result,testcaseinfo.starttime,testcaseinfo.endtime,testcaseinfo.errorinfo)
tableElem.append(mytree.HTML(str(mytablerow))) f = open("TestResult.html","w")
#html.tostring
newContent = repr(html.tostring(tree,method="html",with_tail=False))
newContent = newContent.replace(r"\n","").replace(r"\t","").replace('b\'',"")
newContent = newContent[:len(newContent)-1]
f.write(newContent)
f.close()

ok,最后看一下生成的测试报表:

总结

在网上有很多关于Selenium自动化的Best Practice,当然大家也可以根据自己的需求来DIY自己的框架,不管简陋与否,好用才是硬道理:)!

简易selenium自动化测试框架(Python)的更多相关文章

  1. 《Selenium自动化测试实战:基于Python》Selenium自动化测试框架入门

    第1章  Selenium自动化测试框架入门 1.1  Selenium自动化测试框架概述 说到目前流行的自动化测试工具,相信只要做过软件测试相关工作,就一定听说过Selenium. 图1-1是某企业 ...

  2. Selenium自动化测试框架入门整理

    ​​关注嘉为科技,获取运维新知 本文主要针对Selenium自动化测试框架入门整理,只涉及总体功能及框架要点介绍说明,以及使用前提技术基础要求整理说明.作为开发人员.测试人员入门参考. 本文参考:Se ...

  3. PO模式在selenium自动化测试框架有什么好处

    PO模式是在UI自动化测试过程当中使用非常频繁的一种设计模式,使用这种模式后,可以有效的提升代码的复用能力,并且让自动化测试代码维护起来更加方便. PO模式的全称叫page object model( ...

  4. 《手把手教你》系列基础篇(八十)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试-番外篇(详解教程)

    1.简介 经过前边几篇知识点的介绍,今天宏哥就在实际测试中应用一下前边所学的依赖测试.这一篇主要介绍在TestNG中一个类中有多个测试方法的时候,多个测试方法的执行顺序或者依赖关系的问题.如果不用de ...

  5. Selenium自动化测试框架介绍

    Selenium自动化测试框架介绍 1.测试架构作用 a.可维护性 b.提高编写脚本效率 c.提高脚本的可读性 2.框架的几大要素: Driver管理,脚本,数据,元素对象,LOG,报告,运行机制,失 ...

  6. Python3简易接口自动化测试框架设计与实现(中)

    目录 7.Excel数据读取 7.1.读取配置文件 7.1.编写Excel操作类 8.用例组装 9.用例运行结果校验 10.运行用例 11 .小结 上一篇:Python3简易接口自动化测试框架设计与实 ...

  7. 《手把手教你》系列基础篇(七十五)-java+ selenium自动化测试-框架设计基础-TestNG实现DDT - 中篇(详解教程)

    1.简介 上一篇中介绍了DataProvider如何传递参数,以及和一些其他方法结合传递参数,今天宏哥接着把剩下的一些常用的也做一下简单的介绍和分享. 2.项目实战1 @DataProvider + ...

  8. 《手把手教你》系列基础篇(七十六)-java+ selenium自动化测试-框架设计基础-TestNG实现DDT - 下篇(详解教程)

    1.简介 今天这一篇宏哥主要是结合实际工作中将遇到的测试场景和前边两篇学习的知识结合起来给大家讲解和分享一下,希望以后大家在以后遇到其他的测试场景也可以将自己的所学的知识应用到测试场景中. 2.测试场 ...

  9. 《手把手教你》系列基础篇(七十七)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试- 上篇(详解教程)

    1.简介 今天主要是讲解和分享:TestNG中一个类中有多个测试方法的时候,多个测试方法的执行顺序或者依赖关系的问题.如果不用dependsOnMethods,testNG会自动根据@Test方法名称 ...

随机推荐

  1. zookeeper和kafka的安装

    一.安装zookeeper yum install -y java ##安装jdk1.6版本以上 wget http://mirror.bit.edu.cn/apache/zookeeper/zook ...

  2. python之发送HTML内容的邮件

    # 发送html内容的邮件 import smtplib, time, os from email.mime.text import MIMEText from email.header import ...

  3. 两个序列求前k大和

    ---恢复内容开始--- 没有题目,没有题意,这是学长提过的一个技巧,给你两个排好序的序列,每次可以各从中取一个,求前k大的和, 一个优先队列,先将a序列中最大的那个和b序列所有元素相加存进队列中,每 ...

  4. BZOJ1433[ZJOI2009]假期的宿舍——二分图最大匹配

    题目描述 学校放假了······有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如A 和B都是学校的学生,A要回家,而C来看B,C与A不认识.我们假设每个人只能睡和自己直接认 ...

  5. 2017ACM/ICPC亚洲区沈阳站-重现赛

    HDU 6222 Heron and His Triangle 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6222 思路: 打表找规律+大数运算 首先我 ...

  6. 自学Python2.1-基本数据类型-字符串方法 下

    自学Python之路 自学Python2.1-基本数据类型-字符串方法 下 class str(object): """ str(object='') -> str ...

  7. 添加AD RMS role时,提示密码不能被验证The password could not be validated

    "The password could not be validated" when attempting to provision an AD RMS server. Sympt ...

  8. 利用docker进行基础镜像制作

    前言 最近想通过Javaee来开发博客,但是不想因为环境配置问题总是耗时间配置相关配置,所以萌生出用docker镜像来搭建web的系统环境,也是作为docker学习实践的笔记. docker hub注 ...

  9. 2019.3.18考试&2019.3.19考试&2019.3.21考试

    2019.3.18 C O D E T1 树上直接贪心,环上for一遍贪心 哇说的简单,码了将近一下午终于码出来了 感觉自己码力/写题策略太糟糕了,先是搞了一个细节太多的写法最后不得不弃疗了,然后第二 ...

  10. 【loj3056】【hnoi2019】多边形

    题目 描述 ​ 给出一个 \(n\) 个点的多边形初始的三角剖分: ​ 一次合法的旋转定义为 \((a,b,c,d)\) ,满足 \(a<b<c<d\) : ​ 并且存在边\((a, ...