def Compress_zip(self, targetfile, *sourcefile): u''' compress file or folder to zip package Examples: | Compress zip | H:\\my.zip | H:\\test1.csv | H:\\test2.csv | | Compress zip | H:\\mytest.zip | H:\\testfolder | ''' try: filelist = [] for source in sourcefile: if os.path.isfile(source): filelist.append(source) else: for root, dirs, files in os.walk(source): for name in files: filelist.append(os.path.join(root, name)) zf = zipfile.ZipFile(targetfile, "w", zipfile.zlib.DEFLATED) for f in filelist: arcname = os.path.split(f)[-1] zf.write(f, arcname) zf.close() except Exception, e: dlog.fail(e)
def _check_go_start_stdout(self): timeout = 60 while self.process is None: pass time1 = time.time() while True: log = self.process.stdout.readline() if log.find('service start') > -1: dlog.write(log.replace('\r\n', '')) break elif log.find("http server Running on") > -1: dlog.write(log.replace('\r\n', '')) break elif log.find('Exception') > -1: dlog.write(log.replace('\r\n', ''), level='WARN') break elif log.find('ERROR:') > -1: dlog.write(log.replace('\r\n', ''), level='WARN') break time2 = time.time() if time2 - time1 > float(timeout): dlog.fail("launch app is timeout %s seconds" % timeout, False) break self.process.stdout.close() time.sleep(0.5)
def DB_Get_Values(self, database, query, host=_Host, user=_User, password=_Password): u''' Default Value: host = 'localhost' user = '******' passwrod = 'derbysoft' Return:data List Examples: | @{result}= | DB Get Values | micros_adapter | select adult_count from availability_log where id ='1' | user=root1 | passwrod=123 | | @{result}= | DB Get Values | micros_adapter | select adult_count from availability_log where id ='1' | ''' time.sleep(1) try: self._connect(database, host, user, password) self.cur.execute(query) # ruslutlist = [] newresult = [] for result in self.cur: for r in result: newresult.append(str(r)) # ruslutlist.append(newresult) self._close() return newresult except Exception, e: dlog.fail(e)
def DB_Execute_Sql_String(self, sql, host=_Host, user=_User, password=_Password): u""" Default Value: user = '******' passwrod = 'derbysoft' Examples: | DB_Execute_Sql_String | UPDATE accor_adapter.`reservation` SET book_status='bbbb' | | DB_Execute_Sql_String | UPDATE accor_adapter.`reservation` SET book_status='bbbb';UPDATE accor_adapter.`reservation` SET channel_passport='GTA'; | """ try: conn = mysql.connector.connect(user=user, password=password, host=_Host) cur = conn.cursor() sqllist = sql.split(';') for s in sqllist: cur.execute(s) conn.commit() dlog.write("DB Execute Sql String is successfully") except Exception, e: dlog.fail(e)
def DB_Execute_Sql(self, sql, host=_Host, user=_User, password=_Password): u''' Default Value: user = '******' passwrod = 'derbysoft' Examples: | DB Execute Sql | ./sample.sql | ''' sql = CONTEXT.get_path(sql) newlines, status = Common.rendefile(sql) if status: newsql = Common.CreateTempFile(newlines) command = """mysql -h%s -u%s -p%s < "%s" """ % (host, user, password, newsql) else: command = """mysql -h%s -u%s -p%s < "%s" """ % (host, user, password, sql) dlog.write(command) m = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) errormessage = m.stderr.read() m.wait() if errormessage != "": dlog.fail(errormessage) else: dlog.write("DB Execute Sql is successfully")
def JsonCompare(self, actualJson, expectedJson, filters=None, sort='True'): u''' 比较两个JSON文件,如果有重复的节点,默认排序\n 参数:\n actualJson: 实际要对比的JSON文件或文件路径\n expectedJson: 预期要对比的JSON文件或文件路径\n filters: 过滤不需要对比的节点\n \n _*Examples:*_\n *对比JSON文件时,过滤节点,原始测试数据:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/expectresult1.json|expected.json],过滤后的测试数据:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/result1.json|result.json]* | Json Compare | actural.json | expected.json | ["$..EchoToken","$..Version"] | \n JSONpath和Xpath 的对比 | XPath | JSONPath | Result | | /store/book/author | $.store.book[*].author | the authors of all books in the store | | //author | $..author | all authors | | /store/* | $.store.* | all things in store, which are some books and a red bicycle. | | /store//price | $.store..price | the price of everything in the store. | | //book[3] | $..book[2] | the third book | | //book[last()] | $..book[(@.length-1)] | | | | $..book[-1:] | the last book in order. | | //book[position()<3] | $..book[0,1] | | | | $..book[:2] | the first two books | | //book[isbn] | $..book[?(@.isbn)] | filter all books with isbn number | | //book[price<10] | $..book[?(@.price<10)] | filter all books cheapier than 10 | | //* | $..* | all Elements in XML document. All members of JSON structure. | ''' try: if type(filters) is unicode: filters = ast.literal_eval(filters) actualstr = self._get_jsondata(actualJson) expectedstr = self._get_jsondata(expectedJson) if filters is not None: actualstr = self._json_filters(actualstr, filters) expectedstr = self._json_filters(expectedstr, filters) actual = self._sort_format_jsondata(actualstr, sort) expected = self._sort_format_jsondata(expectedstr, sort) htmlfile = '%f.html' % (time.time()) htmlstr = """please click <a target="_blank" href="%s">%s</a> to check details""" % ( htmlfile, htmlfile) change = difftohtmlbytext(actual, expected, htmlfile) if change: dlog.write(htmlstr, html=True) dlog.fail('JSON Compare Failed') else: dlog.write('JSON Compare :File Match') dlog.write(htmlstr, html=True) except Exception, e: dlog.fail(e)
def _getroot(self, xml): if Common.check_file(CONTEXT.get_path(xml)): tree1 = etree.parse(CONTEXT.get_path(xml)) root = tree1.getroot() else: try: Common.CreateTempFile(xml) if xml is unicode: root = etree.XML(xml.encode('utf-8')) else: root = etree.XML(xml) except Exception, e: dlog.fail('XML Parse error:%s,(%s)' % (e, xml), False)
def _get_jsondata(self, inputdata): try: if (type(inputdata) is list) or (type(inputdata) is dict): jsondata = inputdata else: if Common.check_file(CONTEXT.get_path(inputdata)): jsondata = json.loads(open( CONTEXT.get_path(inputdata)).read(), encoding='utf-8') else: jsondata = json.loads(str(inputdata), encoding='utf-8') return jsondata except Exception, e: dlog.fail("%s %s" % (e, inputdata))
def XML_Schema_Validate(self, xml, schema): u''' Examples: | ${schema} | Get Absolute Path | ../setup/schema.xml | | ${response}= | Get Http Server Request | | ${element}= | XML Get Element | ${response} | .//{http://www.derbysoft.com/doorway}HotelDescriptiveInfoResponse | | XML Schema Validate | ${element} | ${schema} | ======================================================================================================================= | ${response}= | Get Http Server Request | | ${element}= | XML Get Element | ${response} | .//{http://www.derbysoft.com/doorway}HotelDescriptiveInfoResponse | | XML Schema Validate | ${element} | http://52.43.244.90:7001/hotel/content/doorwayContent.xsd | The ``schema`` can be any of the following: - a file name/path - a file object - a file-like object - a URL using the HTTP or FTP protocol The ``xml`` can be any of the following: - a xml string - a xml element - a file name/path - a file object - a file-like object - a URL using the HTTP or FTP protocol ''' xmlschema_doc = etree.parse(schema) xmlschema = etree.XMLSchema(xmlschema_doc) if type(xml) is etree._Element: doc = xml else: try: doc = etree.parse(xml) except IOError: doc = etree.XML(xml) if xmlschema.validate(doc): dlog.write('XML Schema Validate is succeed') else: try: xmlschema.assert_(doc) except AssertionError, e: dlog.fail('XML Schema Validate is failed,%s' % e)
def Match(self, datas, patterns, values, namespaces=None, method='x'): u''' method : x(xpath) , j(jsonpath) Examples: Set ResponseMatch by Xpath | ${rs1}= | Match | ${requests} | ['ht:HotelIdInfo[1]/@id'] | ['890'] | {"ht":"http://schemas.xmlsoap.org/soap/envelope/"} | | ${rs2}= | Match | ${requests} | ['//ht:Header//@id','//hot1:CheckIn[1]'] | ['903','2013-12-30'] | {"ht":"http://schemas.xmlsoap.org/soap/envelope/","ht1":"http://schemas.tourico.com/webservices/hotelv3"} | | ${rs1}= | Match | ${requests} | ['.'] | ['com.derbysoft.storage.remote.dto.SaveDailyRateRequest'] | Set ResponseMatch by JsonPath | ${rs1}= | Match | ${requests} | ['$..hotel.name'] | ['BW'] | method=j | | ${rs2}= | Match | ${requests} | ['$..hotel.name',''$..hotel.id'] | ['BW'.'1234'] | method=j | ========================================================================================================================================================================================================================================= | XPath | JSONPath | Result | | /store/book/author | $.store.book[*].author | the authors of all books in the store | | //author | $..author | all authors | | /store/* | $.store.* | all things in store, which are some books and a red bicycle. | | /store//price | $.store..price | the price of everything in the store. | | //book[3] | $..book[2] | the third book | | //book[last()] | $..book[(@.length-1)] | | | | $..book[-1:] | the last book in order. | | //book[position()<3] | $..book[0,1] | | | | $..book[:2] | the first two books | | //book[isbn] | $..book[?(@.isbn)] | filter all books with isbn number | | //book[price<10] | $..book[?(@.price<10)] | filter all books cheapier than 10 | | //* | $..* | all Elements in XML document. All members of JSON structure. | ''' if type(datas) is not list: dlog.fail("The parameter datas must be a list") for d in datas: if Common.check_file(CONTEXT.get_path(d)): d = open(CONTEXT.get_path(d)).read() rm = ResponseMatch('', patterns, values, None, namespaces, method) rm.match(data(d)) if rm.status: return d
def DB_Execute_Sql_Parames(self, Sql, Params, host=_Host, user=_User, password=_Password): u""" Default Value: user = '******' passwrod = 'derbysoft' Examples: When gizp is a File | @{aaa}= | Test Data | maplist.xml | | DB_Execute_Sql_Parames | INSERT INTO marriott_ari.`process` (check_in, create_time, hotel, los_rs,process_key,rate_plan_candidate,rate_plan_candidate_value) VALUES (%s, %s, %s, %s, %s, %s, %s) | '2017-08-05', '2017-07-07 06:23:56', 'HNLOW',r'gzip@{aaa}[0]', 'HNLOW:2017-08-05:CATEGORY:DN1', 'CATEGORY', 'DN1' | When gizp is a String | @{aaa}= | Test Data | maplist.xml | | ${f} | get file | @{aaa}[0] | | DB_Execute_Sql_Parames | INSERT INTO marriott_ari.`process` (check_in, create_time, hotel, los_rs,process_key,rate_plan_candidate,rate_plan_candidate_value) VALUES (%s, %s, %s, %s, %s, %s, %s) | '2017-08-05', '2017-07-07 06:23:56', 'HNLOW','''gzip${f}''', 'HNLOW:2017-08-05:CATEGORY:DN1', 'CATEGORY', 'DN1' | """ try: conn = mysql.connector.connect(user=user, password=password, host=_Host) cur = conn.cursor() params = ast.literal_eval(Params) res = [] for p in params: if "gzip" in p: p = p.replace('gzip', '') u = Utility() p = u.Compress_Gzip(p) res.append(p) cur.execute(Sql, res) conn.commit() dlog.write("DB_Execute_Sql_Parames is successfully") except Exception, e: dlog.fail(e)
def AssertEmpty(self, obj, message=None): u''' Assert object is empty Examples: | Assert Empty | "${request}" | request is empty | | Assert Empty | "@{response}" | response is empty | ''' if type(obj) is unicode: if str(obj) == "" or str(obj).replace( " ", "") == "'[]'" or str(obj).replace(" ", "") == '"[]"': dlog.write(message) else: dlog.write(obj) dlog.fail('%s,obj=%s' % (message, str(obj))) elif type(obj) is str: if obj == "" or obj.replace(" ", "") == "[]" or obj.replace( " ", "") == '"[]"': dlog.write(message) else: dlog.write(obj) dlog.fail('%s,obj=%s' % (message, str(obj))) else: dlog.fail("unsupport obj type()%s" % type(obj))
def _check_jetty_start(self): timeout = 60 while self.process is None: pass time1 = time.time() while True: errlog = self.process.stderr.readline() if errlog.find('oejs.Server:main: Started') > -1: dlog.write(errlog.replace('\r\n', ''), console=True) break elif errlog.find('Exception') > -1: dlog.write(errlog.replace('\r\n', ''), level='WARN', console=True) elif errlog.find('ERROR:') > -1: dlog.write(errlog.replace('\r\n', ''), level='WARN', console=True) time2 = time.time() if time2 - time1 > float(timeout): dlog.fail("launch app is timeout %s seconds" % timeout, False) break
def AssertEqual(self, obj1, obj2, message=None): u''' Assert two object is equal Examples: | Assert Equal | "${result}" | abc | result is right | | Assert Equal | "${results}" | [123,badc] | result is right | ''' if type(obj1) == list: obj2 = ast.literal_eval(obj2) if obj1 == obj2: dlog.write(message) else: dlog.fail("%s,obj1=%s,obj2=%s" % (message, str(obj1), str(obj2))) else: if str(obj1) == str(obj2): dlog.write(message) else: dlog.fail("%s,obj1=%s,obj2=%s" % (message, str(obj1), str(obj2)))
def DB_Equal_Check(self, database, query, checkvalues, host=_Host, user=_User, password=_Password): u''' Default Value: host = 'localhost' user = '******' passwrod = 'derbysoft' Examples: | DB Equal Check | micros_adapter | select adult_count from availability_log where id ='1' | [('2',)] | | DB Equal Check | micros_adapter | select adult_count,channel from availability_log where id ='1' | [('2','DERBYSOFT'),('3','DERBYSOFT'),(...,...)] | | DB Equal Check | micros_adapter | select adult_count from availability_log where id ='1' | [('2',)] | user=root1 | passwrod=123 | ''' time.sleep(1) try: if type(checkvalues) is unicode: checkvalues = ast.literal_eval(checkvalues) self._connect(database, host, user, password) # query = 'select %s from %s'%(','.join(checkvalues[0]),query) expectvalues = list(checkvalues) expectvalues = sorted(expectvalues) self.cur.execute(query) ruslutlist = [] for result in self.cur: ruslutlist.append(result) ruslutlist = sorted(ruslutlist) status = True self._close() if len(ruslutlist) == 0: dlog.fail("[query:%s] DB result is empty" % (query)) return for result, expect in zip(ruslutlist, expectvalues): for r, e in zip(result, expect): if str(r) != str(e): status = False if status is True: dlog.write("[query:%s] check is correct" % (query)) else: dlog.fail( '[query:%s] check is incorrect \nactual result:%s\nexpect result:%s' % (query, str(ruslutlist), str(expectvalues))) except Exception, e: dlog.fail(e)
def DB_Contain_Check(self, database, query, checkvalues, host=_Host, user=_User, password=_Password): u''' Examples: | DB Contain Check | micros_adapter | select adult_count from availability_log where id ='1' | [('2',)] | ''' time.sleep(1) try: if type(checkvalues) is unicode: checkvalues = ast.literal_eval(checkvalues) self._connect(database, host, user, password) # query = 'select %s from %s'%(','.join(checkvalues[0]),query) expectvalues = list(checkvalues) expectvalues = sorted(expectvalues) self.cur.execute(query) ruslutlist = [] for result in self.cur: ruslutlist.append(result) ruslutlist = sorted(ruslutlist) self._close() status = True if len(ruslutlist) == 0: dlog.fail("[query:%s] DB result is empty" % (query)) return for result, expect in zip(ruslutlist, expectvalues): for r, e in zip(result, expect): if str(r).find(str(e)) == -1: status = False if status is True: dlog.write("[query:%s] check is correct" % (query)) else: dlog.fail( '[query:%s] check is incorrect \nactual result:%s\nexpect result:%s' % (query, str(ruslutlist), str(expectvalues))) except Exception, e: dlog.fail(e)
def DB_XML_Check(self, database, query, checkxml, tagfilters=None, namespaces=None, sorted_filter=None, matchid=None, gizp=False, host=_Host, user=_User, password=_Password): u''' Examples: | DB_XML_Check | micros_adapter | select request from availability_log where id ='2' | expectresult.xml | ['.//tag1[@timestamp]','.//tag2/tag3'] | | DB_XML_Check | micros_adapter | select request from availability_log where id ='2' | <?xml ....><root>....</root> | | | DB_XML_Check | micros_adapter | select request from availability_log where id ='2' | <?xml ....><root>....</root> | gizp=True | ''' time.sleep(1) try: self._connect(database, host, user, password) expectvalue = checkxml dlog.write(query) self.cur.execute(query) actualresult = '' mstauts = False for result in self.cur: actualresult = result[0] if gizp is not False: if type(actualresult) == bytearray: g = gzip.GzipFile( fileobj=StringIO.StringIO(str(actualresult))) actualresult = g.read() else: if actualresult.find('ZIPPED') != -1: b = base64.b64decode(actualresult[6:]) g = gzip.GzipFile( fileobj=StringIO.StringIO(str(b))) actualresult = g.read() if matchid != None: for dm in definematchs: if str(dm.matchid) == str(matchid): if dm.match(str(actualresult)): mstauts = True break if mstauts: break self._close() if matchid != None: if mstauts is False: dlog.fail("can't find match result from db") if actualresult == "": dlog.fail("can't find result from db") else: c = Compare() c.XmlCompare(str(actualresult), str(expectvalue), tagfilters, namespaces, sorted_filter) except Exception, e: dlog.fail(e)
def XmlCompare(self, actualresultxml, expectedresultxml, tagfilters=None, namespaces=None, sorted_filter=None, replace=None): u''' 对比两个XML文件,默认会把XML重复的节点排序\n 参数:\n actualresultxml: 实际要对比的XML文件或文件路径\n expectedresultxml: 预期要对比的XML文件或文件路径\n tagfilters: 过滤不需要对比的节点和节点属性\n namespace: xml的命名空间\n sorted_filter: 过滤不需要排序的节点\n \n _*Examples:*_\n *对比XML时,过滤XML中所有属性是"code"的属性,原始测试数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/expectresult1.xml|expectresult.xml],过滤之后的数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/result1.xml|result.xml]* | Xml Compare | @{requests}[0] | ./expectresult.xml | ['.//*[@Code]'] | \n *对比XML时,过滤属性是"ResponseReference"的属性和节点是"RoomSupplement"的节点,原始测试数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/expectresult1.xml|expectresult.xml],过滤之后的数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/result2.xml|result.xml]* | Xml Compare | @{requests}[0] | ./expectresult.xml | ['.[@ResponseReference]','.//RoomSupplement'] | \n *对比XML时,过滤包含命名空间的节点,原始测试数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/expectresult2.xml|expectresult.xml],过滤之后的数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/result3.xml|result.xml]* | Xml Compare | @{requests}[0] | ./expected-Request1.xml | ['.//ht:Header','.//xx:OTA_HotelResNotifRQ/xx:POS/xx:Source'] | {"ht":"http://schemas.xmlsoap.org/soap/envelope/","xx":"http://www.opentravel.org/OTA/2003/05"} | \n *对比XML时,把含有ns这种的命名空间给去掉,原始测试数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/expectresult3.xml|expectresult.xml],过滤之后的数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/result4.xml|result.xml]* | Xml Compare | @{requests}[0] | ./expectresult.xml | replace= {"ns\\\\d:":"",'xmlns:ns\\\\d=".*"':""} | \n *对比XML时,过滤不排序的节点,原始测试数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/expectresult4.xml|expectresult.xml] 如果排序后的数据样例:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/result5.xml|result.xml],使用这个参数后的结果:[https://github.com/kenzofeng/Doraemon/blob/master/src/Doraemon/example/expectresult4.xml|this]* | Xml Compare | @{requests}[0] | ./expectresult.xml | sorted_filter= [ota:RoomType] | ''' try: if type(tagfilters) is unicode: tagfilters = ast.literal_eval(tagfilters) if type(namespaces) is unicode: namespaces = eval(namespaces) if type(sorted_filter) is unicode: sorted_filter = ast.literal_eval(sorted_filter) if type(replace) is unicode: replace = eval(replace) actual_resul_root = self._getroot(actualresultxml) expect_result_root = self._getroot(expectedresultxml) if tagfilters is not None: self._elfilter(actual_resul_root, tagfilters, namespaces, 'actural') self._elfilter(expect_result_root, tagfilters, namespaces, 'expected') convertedDict1 = xmltodict.parse(etree.tostring(actual_resul_root)) convertedDict2 = xmltodict.parse( etree.tostring(expect_result_root)) for k, v in convertedDict1.items(): self._sortdict(convertedDict1, k, v, sorted_filter) for k, v in convertedDict2.items(): self._sortdict(convertedDict2, k, v, sorted_filter) actualxml = self._parsefromdictoxml(convertedDict1) expectxml = self._parsefromdictoxml(convertedDict2) if replace is not None: actualxml = self._replace(actualxml, replace) expectxml = self._replace(expectxml, replace) htmlfile = '%f.html' % (time.time()) htmlstr = """please click <a target="_blank" href="%s">%s</a> to check details""" % ( htmlfile, htmlfile) # actualxml = actualxml.decode('unicode_escape') # expectxml = expectxml.decode('unicode_escape') change = difftohtmlbytext(actualxml, expectxml, htmlfile) if change: dlog.write(htmlstr, html=True) dlog.fail('XML Compare Failed') else: dlog.write('XML Compare :File Match') dlog.write(htmlstr, html=True) except Exception, e: dlog.fail(e)