간단한python 스크립트, excel 표에 맞추어 인터페이스 자동화 테스트 실현

11699 단어
#encoding: utf-8

import urllib2,xlrd,xlwt,time
from xlutils.copy import copy
from lxml import etree

def Time():
    tim=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    return tim

class Test(object):

    def __init__(self,url,uri):
        self.url=url
        self.uri=uri

    def openurl(self):
        try:
            response=urllib2.urlopen(self.url)
            self.html=response.read()
            return ("pass",self.html)
        except urllib2.URLError,e:
            ex=e.code+e.reason
            return ("except",ex)
    # - 
    def case(self,EC_result,html):
        self.EC_result =EC_result
        root = etree.fromstring(html)
        Message=root.xpath('/ats/message')[0].text
        valuelist=[]
        if Message ==  "Successful.":
            __VALUE=root.xpath(self.uri)
            for val in __VALUE:
                value=val.text
                valuelist.append(value)return "PASS"
            else:
                return "FALSE"
        else:
            return Message
    # len 
    def caselen(self,EC_result,html):
        self.EC_result =EC_result
        root = etree.fromstring(self.html)
        Message=root.xpath('/ats/message')[0].text  
        if Message ==  "Successful.":
            __VALUE=root.xpath(self.uri)
            if len(__VALUE) > EC_result :
                return "PASS"
            else:
                return "FALSE"
        else:
            return Message
    # 
    def caseele(self,EC_result,html):
        self.EC_result =EC_result
        root = etree.fromstring(self.html)
        Message=root.xpath('/ats/message')[0].text
        
        valuelist=[]

        if Message ==  "Successful.":
            __VALUE=root.xpath(self.uri)
            for val in __VALUE:
                valuelist.append(val)
                strvalue=";".join(valuelist)
            if EC_result in strvalue:
                return "PASS"
            else:
                return "FALSE"
        else:
            return Message


oldex = xlrd.open_workbook(r'ClasTtestcase.xls')
oldsh = oldex.sheet_by_index(0)
nrows = oldsh.nrows
newex = copy(oldex)
newsh = newex.get_sheet(0)
newsh.write(1,6,Time())

#dict={"traffic/item":caseA_traffic_round,
for i in xrange(1,nrows):
    # LOG
    print "#%d."%i,oldsh.cell(i,0).value.strip(),u" "
    print "url:",oldsh.cell(i,1).value.strip()
    # Testcase
    TestCase = Test(oldsh.cell(i,1).value.strip(),oldsh.cell(i,4).value.strip())
    # 
    EC_result=oldsh.cell(i,2).value
    # 
    Path=oldsh.cell(i,5).value
    # 
    status,html=TestCase.openurl()
    # 
    if status == 'pass':
        if 'len' in Path:
            newsh.write(i,3,TestCase.caselen(EC_result,html))
        elif '@' in Path:
            newsh.write(i,3,TestCase.caseele(EC_result,html))
        else:
            newsh.write(i,3,TestCase.case(EC_result,html))
    else:
        newsh.write(i,3,html)
newex.save(r'ClasTtestcase.xls')
print u' '
excel  , 

다음으로 전송:https://www.cnblogs.com/BUGU/p/4616767.html

좋은 웹페이지 즐겨찾기