python+Appium自动化:logging配置代码分离

配置文件信息log.conf:

[loggers]
keys=root,simpleExample

[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handlers]
keys=fileHandler,consoleHandler

[handler_consoleHandler]
class=StreamHandler
args=(sys.stdout,)
level=INFO
formatter=simpleFormatter

[handler_fileHandler]
class=FileHandler
args=(‘runlog.log‘, ‘a‘)
level=INFO
formatter=simpleFormatter

[formatters]
keys=simpleFormatter

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

使用到如下方法读取配置文件

logging.config.fileConfig(‘log.conf‘)

再创建一个日志器

logging=logging.getLogger(‘simpleExample‘或者不填默认root)

脚本代码:

# -*- coding: utf-8 -*-#

from appium import webdriver
import yaml
import logging
import logging.config
from selenium.common.exceptions import NoSuchElementException

file = open(../case/capability.yaml,encoding="utf-8")
data = yaml.load(file,yaml.FullLoader)
#
CON_LOG=log.conf
logging.config.fileConfig(CON_LOG)
logging=logging.getLogger()


desired_caps={
               "platformName": data["platformName"],
               "platformVersion": data["platformVersion"],
               "deviceName": data["deviceName"],
               "appPackage": data["appPackage"],
               "appActivity": data["appActivity"],
               "unicodeKeyboard":data["unicodeKeyboard"],
               "resetKeyboard":data["resetKeyboard"],
               "noReset": data["noReset"]
                }
logging.info("logging app...")
driver = webdriver.Remote(http://+str(data[ip])+:+str(data[port])+/wd/hub,desired_caps)

#检测同意协议弹窗
def check_agreebtn():
    logging.info("check_agreebtn")
    try:
        agreebtn=driver.find_element_by_id(com.taobao.taobao:id/provision_positive_button)
    except NoSuchElementException:
        logging.info("no agreebtn")
    else:
        agreebtn.click()

def check_localtionbtn():
    logging.info("check_localtionbtn")
    try:
        localtionbtn=driver.find_element_by_id(com.taobao.taobao:id/uik_mdButtonDefaultPositive)
    except NoSuchElementException:
        logging.info("no localtionbtn")
    else:
        localtionbtn.click()

check_agreebtn()
check_localtionbtn()

输出日志:

2019-12-24 10:07:41,427 - root - INFO - logging app...
2019-12-24 10:08:08,984 - root - INFO - check_agreebtn
2019-12-24 10:08:11,754 - root - INFO - no agreebtn
2019-12-24 10:08:11,754 - root - INFO - check_localtionbtn
2019-12-24 10:08:19,418 - root - INFO - no localtionbtn

python+Appium自动化:logging配置代码分离

上一篇:ApplicationRunner接口


下一篇:uniapp - 生成证书(ios和android)