def load_xml_cases_dom(dom): # load cases from an already-parsed XML DOM cases = [] param_map = {} for child in dom.getiterator(): if child.tag != dom.getroot().tag and child.tag == "param": name = child.attrib.get("name") value = child.attrib.get("value") param_map[name] = value if child.tag != dom.getroot().tag and child.tag == "case": req = Request() wait = child.attrib.get("wait") if wait: req.wait = int(wait) else: req.wait = 0 repeat = child.attrib.get("repeat") if repeat: req.repeat = int(repeat) else: req.repeat = 1 for element in child: if element.tag.lower() == "url": req.url = element.text if element.tag.lower() == "method": req.method = element.text if element.tag.lower() == "body": file_payload = element.attrib.get("file") if file_payload: req.body = open(file_payload, "rb").read() else: req.body = element.text if element.tag.lower() == "verify": req.verify = element.text if element.tag.lower() == "regular_expressions": req.regular_expressions = element.text if element.tag.lower() == "verify_negative": req.verify_negative = element.text if element.tag.lower() == "timer_group": req.timer_group = element.text if element.tag.lower() == "add_header": # this protects against headers that contain colons splat = element.text.split(":") x = splat[0].strip() del splat[0] req.add_header(x, "".join(splat).strip()) req = resolve_parameters(req, param_map) # substitute vars cases.append(req) return cases
def load_xml_cases_dom(dom): # load cases from an already-parsed XML DOM cases = [] param_map = {} for child in dom.getiterator(): if child.tag != dom.getroot().tag and child.tag == 'param': name = child.attrib.get('name') value = child.attrib.get('value') param_map[name] = value if child.tag != dom.getroot().tag and child.tag == 'case': req = Request() wait = child.attrib.get('wait') if wait: req.wait = int(wait) else: req.wait = 0 repeat = child.attrib.get('repeat') if repeat: req.repeat = int(repeat) else: req.repeat = 1 for element in child: if element.tag.lower() == 'url': req.url = element.text if element.tag.lower() == 'method': req.method = element.text if element.tag.lower() == 'body': file_payload = element.attrib.get('file') if file_payload: req.body = open(file_payload, 'rb').read() else: req.body = element.text if element.tag.lower() == 'verify': req.verify = element.text if element.tag.lower() == 'verify_negative': req.verify_negative = element.text if element.tag.lower() == 'timer_group': req.timer_group = element.text if element.tag.lower() == 'add_header': # this protects against headers that contain colons splat = element.text.split(':') x = splat[0].strip() del splat[0] req.add_header(x, ''.join(splat).strip()) req = resolve_parameters(req, param_map) # substitute vars cases.append(req) return cases
def load_xml_cases_dom(dom): # load cases from an already-parsed XML DOM cases = [] param_map = {} def dom_getiterator(): if sys.version.startswith('3.9'): # https://blog.csdn.net/suhao0911/article/details/110950742 return dom.iter() else: return dom.getiterator() for child in dom_getiterator(): if child.tag != dom.getroot().tag and child.tag == 'param': name = child.attrib.get('name') value = child.attrib.get('value') param_map[name] = value if child.tag != dom.getroot().tag and child.tag == 'case': req = Request() repeat = child.attrib.get('repeat') if repeat: req.repeat = int(repeat) else: req.repeat = 1 # support tenjin template engine tenjintag = child.attrib.get('tenjin') if tenjintag and tenjintag.lower() == 'true': req.tenjin = True for element in child: if element.tag.lower() == 'url': req.url_str = element.text if element.tag.lower() == 'method': req.method = element.text if element.tag.lower() == 'body': file_payload = element.attrib.get('file') if file_payload: #req.body = open(file_payload, 'rb').read() content_type, body = encode_multipart_formdata( [], [('file', file_payload.split("/")[-1], open(file_payload, 'rb').read())]) req.add_header('content-type', content_type) req.add_header('content-length', str(len(body))) req.body_str = body req.tenjin = False else: req.body_str = element.text if element.tag.lower() == 'verify': req.verify = element.text if element.tag.lower() == 'verify_negative': req.verify_negative = element.text if element.tag.lower() == 'timer_group': req.timer_group = element.text if element.tag.lower() == 'add_header': # this protects against headers that contain colons splat = element.text.split(':') x = splat[0].strip() del splat[0] req.add_header(x, ''.join(splat).strip()) if element.tag.lower() == 'add_header_tenjin': req.add_header_tenjin(TenjinEngine().renderFunction( saveStrTemaplate(element.text))) if element.tag.lower() == 'add_headers_tenjin': req.add_headers_tenjin(TenjinEngine().renderFunction( saveStrTemaplate(element.text))) req = resolve_parameters(req, param_map) # substitute vars # proces tenjin if req.tenjin: req.body = TenjinEngine().renderFunction( saveStrTemaplate(req.body_str)) req.url = TenjinEngine().renderFunction( saveStrTemaplate(req.url_str)) else: req.body = lambda: req.body_str req.url = lambda: req.url_str cases.append(req) return cases