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 Set_System_Date(self, date): u''' Set OS system date the first time you use this function,you must follow below steps! 1:run SaveAdministrator.bat (Doraemon/batch/SaveAdministrator.bat) 2:input your admin password Examples: | ${ctime} | Get Current Date | result_format=%H:%M | | ${cdate} | Get Current Date | result_format=%Y-%m-%d | | Set System Date | 2017-06-22 | | Set System Date | 2017/06/22 | | Run your test case | | Set System Time | ${cdate} | ''' if mswindows: sp = subprocess.Popen([ 'runas', '/user:Administrator', '/savecred', '%s %s' % (env.date_bat, date) ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) dlog.write(sp.stdout.read()) time.sleep(2)
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 Test_Data(self, pattern, path=None): u''' get test folder or file return List | @{files}= | Test Data | .*.txt | | @{files}= | Test Data | .*.txt | shop/TC01.001 | ''' if path is None: test_path = CONTEXT.get_test_case_path() else: test_path = CONTEXT.get_path(path) dlog.write(test_path) datas = [] for fn in os.listdir(test_path): match = re.findall(pattern, fn) if match: datas.append(fn) if len(datas) == 0: dlog.write( "Can't find defined files,you can reference this link(http://10.200.107.43/wiki/en/Automation_File_Format" ) return datas datas.sort(cmp=mycmp) datas = [os.path.join(test_path, d) for d in datas] return datas
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 _go_print_log(self): while True: try: if self.process.poll() is not None: break else: log = self.process.stdout.readline() if log != "": dlog.write( (log.replace('\r\n', '')).decode('gbk').encode("utf-8"), console=True) except Exception: pass
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 _create_jetty_web_xml(self, projectname, project_path, kwargs): try: if "pn" in kwargs: if kwargs["pn"].lower() == "true": engine = tenjin.Engine(cache=tenjin.MemoryCacheStorage()) web_xml = engine.render( os.path.join(env.mytemplateDir, 'jetty-web.xml'), {"project_name": projectname}) f = open( os.path.join(project_path, 'WEB-INF', 'jetty-web.xml'), 'w') f.write(web_xml) f.close() except Exception, e: dlog.write(e, console=True)
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 _ccs_duration_check(self, dur, ccsfile, jsonquery): while 1: try: if dur.status: return ccs = json.loads(open(CONTEXT.get_path(ccsfile)).read(), encoding='utf-8') expression = jmespath.compile(jsonquery) r = expression.search(ccs) if len(dur.result) == 0: dur.result.append(r) elif str(r) != str(dur.result[-1]): dur.result.append(r) time.sleep(0.01) except Exception, e: dlog.write(e)
def kill_process_by_name(self, process_name): u''' kill process by name Examples: | kill_process_by_name | chromedriver.exe | ''' for proc in psutil.process_iter(): try: pinfo = proc.as_dict(attrs=['pid', 'name']) if pinfo['name'] == str(process_name): proc.kill() except Exception, e: dlog.write("kill_process_by_name:%s" % e, console=True)
def get_suite_name(self): suite_name = "" try: self._get_variables() if self.check_file(self.variables['${SUITE_SOURCE}']): suite_name = self.variables['${SUITE_SOURCE}'].split( '\\')[-1].split('.')[0] else: suite_name = self.variables['${SUITE_SOURCE}'].split('\\')[-1] p = re.compile('\d+__') rs = p.findall(suite_name) if len(rs) > 0: suite_name = suite_name.replace(rs[0], "") return suite_name except Exception, e: dlog.write(e, level='DEBUG')
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 properties_change(self, path, dict): u''' Examples: | properties change | ../classes/ccs.properties | {"ccs.url":"file://./setupdata/CCS/abc"} | ''' if type(dict) is unicode: dict = eval(dict) p = Properties() p.load(open(CONTEXT.get_path(path))) for k, v in dict.items(): if p.has_key(k): p[k] = v else: dlog.write("path dosen't exist this key" % k, level='WARN') p.store(open(CONTEXT.get_path(path), 'w'))
def render(self, templatefile, context, save=True): u''' Examples: | Render | template.xml | {"timestamp":"12323213"} | Template Engine: tenjin ''' dlog.write(context) templatefile = CONTEXT.get_path(templatefile) if type(context) is unicode: # context = json.loads(context, encoding="utf-8") context = eval(context) path = '\\'.join((templatefile.split('\\'))[:-1]) tfile = templatefile.split('\\')[-1] engine = tenjin.Engine(path=[path], cache=tenjin.MemoryCacheStorage()) newstring = engine.render(tfile, context) return newstring
def properties_read(self, path, keys): u''' Examples: | @{pros}= | properties read | ../classes/ccs.properties | ['ccs.url',hotel.topic] | | ${ccsurl}= | @{pros}[0] | | @{hoteltopic}= | @{pros}[1] | ''' if type(keys) is unicode: keys = ast.literal_eval(keys) p = Properties() p.load(open(CONTEXT.get_path(path))) rkeys = [] for key in keys: if p.has_key(key): rkeys.append(p[key]) else: dlog.write("path dosen't exist this key" % key, level='WARN') return rkeys
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 _run_go(self, app): opath = os.getcwd() app = CONTEXT.get_path(app) dlog.write(app, console=True) os.chdir(os.path.dirname(app)) if mswindows: command = "%s" % os.path.basename(app) print command self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) else: app = app.replace('.exe', '') command = "%s" % os.path.basename(app) print command self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) os.chdir(opath)
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 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 _elfilter(self, root, tagfilters, namespaces, source): for tag in tagfilters: tag, attrib = self._filterattrib(tag) ellist = root.findall(tag, namespaces=namespaces) if len(ellist) == 0: if source == 'actural': dlog.write( "actural xml filter error:cannot find '%s' element or attribute" % tag) elif source == 'expected': dlog.write( "expected xml filter error:cannot find '%s' element or attribute" % tag) else: for el in ellist: if attrib != "": for key in el.attrib: if key.find(attrib) != -1: del el.attrib[key] else: pel = el.getparent() pel.remove(el)
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 _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 get_test_name(self): try: self._get_variables() return self.variables['${TEST_NAME}'] except Exception, e: dlog.write(e, level='DEBUG')
def get_variables(self, name): try: self._get_variables() return self.variables[name] except Exception, e: dlog.write(e, level='DEBUG')
def ChangeJsonValue(self, jsonfile, patterns, values): u''' Examples: Before Json File: test.json = { "errorMessage": null, "value": { "HY": ["true", { "__disabled": false, "disabled": false, "__updated": 1444901673000, "description": "HYATT", "created": 1401171574000, "ratePlanMappingRequired": "true", "updated": 1444901673000, "gtaCode": "HY", "__created": 1401171574000, "derbyCode": "HYATT", "cacheHour": "", "taxCalRequired": "true", "childAsAdultRequired": "true" }] } "success": true } | Change Json Value | test.json | ["$..HY[0]","$..description"] | ["false","this is test"] | Changed Json File: test.json={ "errorMessage": null, "value": { "HY": ["false", { "__disabled": false, "disabled": false, "__updated": 1444901673000, "description": "this is test", "created": 1401171574000, "ratePlanMappingRequired": "true", "updated": 1444901673000, "gtaCode": "HY", "__created": 1401171574000, "derbyCode": "HYATT", "cacheHour": "", "taxCalRequired": "true", "childAsAdultRequired": "true" }] } "success": true } how to filter json path: | 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. | jsonpath detail see:http://goessner.net/articles/JsonPath/ ''' jsonfile = CONTEXT.get_path(jsonfile) dlog.write(jsonfile) f = open(str(jsonfile)) fs = f.read() f.close() j = json.loads(fs, encoding="utf-8", object_pairs_hook=OrderedDict) if type(patterns) is unicode: patterns = ast.literal_eval(patterns) if type(values) is unicode: values = ast.literal_eval(values) for pattern, v in zip(patterns, values): matches = jsonpath_rw.parse(pattern).find(j) for match in matches: j = json_ext.update_json(j, json_ext.get_path(match), v) nj = json.dumps(j, indent=4, ensure_ascii=False) dlog.write(nj) f = open(jsonfile, 'w') f.writelines(nj) f.close()
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)
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)