相思资源网 Design By www.200059.com
故事背景
读取一个Excel中的一条数据用例,请求接口,然后返回结果并反填到excel中。过程中会生成请求回来的文本,当然还会生成一个xml文件。具体的excel文件如下:
代码方案
# -*- coding: UTF-8 -*-
from xml.dom import minidom
import xlrd
import openpyxl
import requests
import json
import sys
import HTMLParser
import os
import re
import codecs
import time
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')
class OptionExcelData(object):
"""对Excel进行操作,包括读取请求参数,和填写操作结果"""
def __init__(self, excelFile,excelPath=''):
self.excelFile = excelFile
self.excelPath = excelPath
self.caseList = []
"""
传入:传入用例Excel名称
返回:[],其中元素为{},每个{}包含行号、城市、国家和期望结果的键值对
"""
def getCaseList(self,excelFile,excelPath=''):
readExcel = xlrd.open_workbook(fileName) #读取指定的Excel
try:
table = readExcel.sheet_by_index(0) #获取Excel的第一个sheet
trows = table.nrows #获取Excel的行数
for n in range(1,trows):
tmpdict = {} #把一行记录写进一个{}
tmpdict['id'] = n #n是Excel中的第n行
tmpdict['CityName'] = table.cell(n,2).value
tmpdict['CountryName'] = table.cell(n,3).value
tmpdict['Rspect'] = table.cell(n,4).value
self.caseList.append(tmpdict)
except Exception, e:
raise
finally:
pass
return self.caseList
"""
传入:请求指定字段结果,是否通过,响应时间
返回:
"""
def writeCaseResult(self,resultBody,isSuccess,respTime, excelFile,theRow,theCol=5):
writeExcel = openpyxl.load_workbook(excelFile) #加载Excel,后续写操作
try:
wtable = writeExcel.get_sheet_by_name('Sheet1') #获取名为Sheet1的sheet
wtable.cell(row=theRow+1,column=theCol+1).value = resultBody #填写实际值
wtable.cell(row=theRow+1,column=theCol+2).value = isSuccess #填写是否通过
wtable.cell(row=theRow+1,column=theCol+3).value = respTime #填写响应时间
writeExcel.save(excelFile)
except Exception, e:
raise
finally:
pass
class GetWeather(object):
"""获取天气的http请求"""
def __init__(self, serviceUrl,requestBody,headers):
self.serviceUrl = serviceUrl
self.requestBody = requestBody
self.headers = headers
self.requestResult = {}
"""
传入:请求地址,请求体,请求头
返回:返回{},包含响应时间和请求结果的键值对
"""
def getWeath(self,serviceUrl,requestBody,headers):
timebefore = time.time() #获取请求开始的时间,不太严禁
tmp = requests.post(serviceUrl,data=requestBody, headers=headers)
timeend = time.time() #获取请求结束的时间
tmptext = tmp.text
self.requestResult['text'] = tmptext #记录响应回来的内容
self.requestResult['time'] = round(timeend - timebefore,2) #计算响应时间
return self.requestResult
class XmlReader:
"""操作XML文件"""
def __init__(self,testFile,testFilePath=''):
self.fromXml = testFile
self.xmlFilePath = testFilePath
self.resultList = []
def writeXmlData(self,resultBody,testFile,testFilePath=''):
tmpXmlFile = codecs.open(testFile,'w','utf-16') #新建xml文件
tmpLogFile = codecs.open(testFile+'.log','w','utf-16') #新建log文件
tmp1 = re.compile(r'\<.*"""返回一个list"""
def readXmlData(self,testFile,testFilePath=''):
tmpXmlFile = minidom.parse(testFile)
root = tmpXmlFile.documentElement
tmpValue = root.getElementsByTagName('Status')[0]. childNodes[0].data
return tmpValue #获取特定字段并返回结果,此处选取Status
if __name__ == '__main__':
requesturl = 'http://www.webservicex.net/globalweather.asmx/GetWeather'
requestHeadrs = {"Content-Type":"application/x-www-form-urlencoded"}
fileName = u'用例内容.xlsx'
ed = OptionExcelData(fileName)
testCaseList = ed.getCaseList(ed.excelFile)
for caseDict in testCaseList:
caseId = caseDict['id']
cityName = caseDict['CityName']
countryName = caseDict['CountryName']
rspect = caseDict['Rspect']
requestBody = 'CityName='+cityName+'&CountryName='+countryName
getWeather = GetWeather(requesturl,requestBody,requestHeadrs)
#获取请求结果
tmpString = getWeather.getWeath(getWeather.serviceUrl, getWeather.requestBody,getWeather.headers)
xd = XmlReader(str(caseId) + '.xml')
#把请求内容写入xml和log
xd.writeXmlData(tmpString,xd.fromXml)
response = xd.readXmlData(str(caseId) + '.xml')
respTime = tmpString['time']
if response == rspect:
theResult = 'Pass'
else:
theResult = 'False'
ed.writeCaseResult(response,theResult,respTime,fileName,caseId)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无一个Python最简单的接口自动化框架的评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
