日志
logger.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-04-15 09:00:00
# @Author : Canon
# @Link : https://www.python.org
# @Version : 3.6.1
import logging
import logging.config
from common.conf_utils import Project
# 日志配置文件
logging.config.fileConfig(Project().read_log())
class Log(object):
def __init__(self):
# 创建一个日志器 logger
self.logger = logging.getLogger("AutoTest")
def __console(self, level, message):
if level == 'info':
self.logger.info(message)
elif level == 'debug':
self.logger.debug(message)
elif level == 'warning':
self.logger.warning(message)
elif level == 'error':
self.logger.error(message, exc_info=True)
def debug(self, message):
self.__console('debug', message)
def info(self, message):
self.__console('info', message)
def warning(self, message):
self.__console('warning', message)
def error(self, message):
self.__console('error', message)
if __name__ == "__main__":
log = Log()
log.info("---测试开始----")
log.info("操作步骤1,2,3")
log.info("----测试结束----")
获取元素定位
xml_utils.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-04-15 09:00:00
# @Author : Canon
# @Link : https://www.python.org
# @Version : 3.6.1
import xml.etree.ElementTree as Et
class XmlUtils(object):
def __init__(self, xml_path):
# 打开xml文档
self.tree = Et.ElementTree(file=xml_path)
# 获得根元素对象
self.root_obj = self.tree.getroot()
def get_tag(self):
"""
获得根节点名称
:return: type: str
"""
return self.root_obj.tag
def get_attribute(self):
"""
获得根节点属性
:return: type: str
"""
return self.root_obj.attrib
def get_attr_by_tag(self, sec_attr, sec_value, third_tag):
"""
根据二级标签的 sec_name 遍历此标签下的标签名为 third_tag 的所有属性数据
:meth sec_attr: 二级标签的属性名 type: str
:meth sec_value: sec_attr 对应的属性值 type: str
:meth third_tag: 三级标签的标签名 type: str
:return: third_tag 对应的所有属性数据 type: dict
"""
data_dict = {}
# 遍历 root 的下一层
for next_node in self.root_obj:
# 遍历 third_tag 标签的所有数据
if next_node.attrib[sec_attr] == sec_value:
for sub_item in next_node.iter(third_tag):
data_dict.setdefault(sub_item.text, tuple(sub_item.attrib.values()))
return data_dict
if __name__ == '__main__':
print(XmlUtils('../data/locator/gateway/PayPage.xml').get_attr_by_tag("pageName", 'webpay', 'locator'))