内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

使用Python怎么實(shí)現(xiàn)一個(gè)接口自動(dòng)化框架-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)使用Python怎么實(shí)現(xiàn)一個(gè)接口自動(dòng)化框架,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

目前創(chuàng)新互聯(lián)建站已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、泰安網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
# -*- 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):
  """對(duì)Excel進(jìn)行操作,包括讀取請(qǐng)求參數(shù),和填寫操作結(jié)果"""
  def __init__(self, excelFile,excelPath=''):
    self.excelFile = excelFile
    self.excelPath = excelPath
    self.caseList = []

  """
  傳入:傳入用例Excel名稱
  返回:[],其中元素為{},每個(gè){}包含行號(hào)、城市、國(guó)家和期望結(jié)果的鍵值對(duì)
  """
  def getCaseList(self,excelFile,excelPath=''):
    readExcel = xlrd.open_workbook(fileName)              #讀取指定的Excel
    try:
      table = readExcel.sheet_by_index(0)               #獲取Excel的第一個(gè)sheet
      trows = table.nrows                       #獲取Excel的行數(shù)
      for n in range(1,trows):
        tmpdict = {}                        #把一行記錄寫進(jìn)一個(gè){}
        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

  """
  傳入:請(qǐng)求指定字段結(jié)果,是否通過,響應(yīng)時(shí)間
  返回:
  """
  def writeCaseResult(self,resultBody,isSuccess,respTime,\
    excelFile,theRow,theCol=5):
    writeExcel = openpyxl.load_workbook(excelFile)           #加載Excel,后續(xù)寫操作
    try:
      wtable = writeExcel.get_sheet_by_name('Sheet1')         #獲取名為Sheet1的sheet
      wtable.cell(row=theRow+1,column=theCol+1).value = resultBody  #填寫實(shí)際值
      wtable.cell(row=theRow+1,column=theCol+2).value = isSuccess   #填寫是否通過
      wtable.cell(row=theRow+1,column=theCol+3).value = respTime   #填寫響應(yīng)時(shí)間
      writeExcel.save(excelFile)
    except Exception, e:
      raise
    finally:
      pass


class GetWeather(object):
  """獲取天氣的http請(qǐng)求"""
  def __init__(self, serviceUrl,requestBody,headers):
    self.serviceUrl = serviceUrl
    self.requestBody = requestBody
    self.headers = headers
    self.requestResult = {}

  """
  傳入:請(qǐng)求地址,請(qǐng)求體,請(qǐng)求頭
  返回:返回{},包含響應(yīng)時(shí)間和請(qǐng)求結(jié)果的鍵值對(duì)
  """
  def getWeath(self,serviceUrl,requestBody,headers):
    timebefore = time.time()                      #獲取請(qǐng)求開始的時(shí)間,不太嚴(yán)禁
    tmp = requests.post(serviceUrl,data=requestBody,\
      headers=headers)
    timeend = time.time()                        #獲取請(qǐng)求結(jié)束的時(shí)間
    tmptext = tmp.text
    self.requestResult['text'] = tmptext                #記錄響應(yīng)回來的內(nèi)容
    self.requestResult['time'] = round(timeend - timebefore,2)     #計(jì)算響應(yīng)時(shí)間
    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'\<.*?\>')                   #生成正則表達(dá)式:<*?>
    tmp2 = tmp1.sub('',resultBody['text'])               #替換相應(yīng)結(jié)果中的<*?>
    html_parser = HTMLParser.HTMLParser()
    xmlText = html_parser.unescape(tmp2)                #轉(zhuǎn)換html編碼

    try:
      tmpXmlFile.writelines(xmlText.strip())             #去除空行并寫入xml
      tmpLogFile.writelines('time: '+\
        str(resultBody['time'])+'\r\n')               #把響應(yīng)時(shí)間寫入log
      tmpLogFile.writelines('text: '+resultBody['text'].strip())   #把請(qǐng)求回來的文本寫入log
    except Exception, e:
      raise
    finally:
      tmpXmlFile.close()
      tmpLogFile.close()

  """返回一個(gè)list"""
  def readXmlData(self,testFile,testFilePath=''):
    tmpXmlFile = minidom.parse(testFile)
    root = tmpXmlFile.documentElement
    tmpValue = root.getElementsByTagName('Status')[0].\
    childNodes[0].data
    return tmpValue                           #獲取特定字段并返回結(jié)果,此處選取Status


if __name__ == '__main__':

  requesturl = 'http://www.webservicex.net/globalweather.asmx/GetWeather'
  requestHeadrs = {"Content-Type":"application/x-www-form-urlencoded"}
  fileName = u'用例內(nèi)容.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)
    #獲取請(qǐng)求結(jié)果
    tmpString = getWeather.getWeath(getWeather.serviceUrl,\
      getWeather.requestBody,getWeather.headers)

    xd = XmlReader(str(caseId) + '.xml')
    #把請(qǐng)求內(nèi)容寫入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)

以上就是使用Python怎么實(shí)現(xiàn)一個(gè)接口自動(dòng)化框架,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

文章名稱:使用Python怎么實(shí)現(xiàn)一個(gè)接口自動(dòng)化框架-創(chuàng)新互聯(lián)
當(dāng)前地址:http://www.rwnh.cn/article38/cssepp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈、網(wǎng)站改版、標(biāo)簽優(yōu)化、定制網(wǎng)站小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
邵阳市| 普陀区| 吕梁市| 北京市| 河曲县| 永年县| 共和县| 嘉祥县| 广州市| 长岭县| 容城县| 叙永县| 沭阳县| 黄陵县| 龙岩市| 蓬莱市| 灌南县| 新巴尔虎左旗| 高陵县| 类乌齐县| 钦州市| 侯马市| 米泉市| 怀仁县| 盐津县| 宝应县| 桂平市| 临颍县| 泰顺县| 昭平县| 临湘市| 来宾市| 台东县| 饶平县| 麻江县| 明溪县| 安康市| 娄烦县| 宝坻区| 广昌县| 明光市|