Test log (beginner)

1. Basic application of logs

#日志:纪录系统运行的信息,记录系统发生的事件日志文件
#作用:了解系统运行的状态,去定位和分析错误问题

#debug:调试1级别
#info:系统正常运行2级别
#warning:警告信息程序有问题,但是他又不影响程序运行不会提Bug3级别默认的级别
#error:错误,出现问题,提bug4级别
#critical:严重的,快要崩溃
#常用:info,error
#反应出问题的严重程度


#logging能够设置日志级别生成日志信息,自带的不需要安装
importlogging

#设置日志级别
#logging.basicConfig(level=logging.DEBUG)

#想要生成日志信息调用日志级别

logging.debug("debug信息")
logging.error("erroe信息")
logging.info("info信息")
logging.warning("warning信息")
logging.critical("critical信息")

operation result:

#设置日志级别
logging.basicConfig(level=logging.DEBUG)

operation result:

Format string field:

%(asctime)s time the log event occurred - human readable time, eg: 2003_07_08 16:47:45,896

%(created)f The event-time stamp of the log event, which is the value returned by the time() function when the time is called at that time

%(relativeCreated)d The relative number of milliseconds when the log event occurred relative to the loading time of the logging module (I don't know why it is used yet)

%(msecs)d milliseconds part of log event time

%(levelname)s The textual log level of the log record ('DEBUG','INFO','WARNING','ERROR','CRITICAL')

The name of the logger used by %(name)s, the default is 'root', because rootLogger is used by default

%(message)s The text content of the log record, oh that is calculated by msg % args

%(pathname)s The full path of the source code file that calls the logging function

%(filename)s The filename part of pathname, including the file suffix'

%(module)s The name part of filename, not including the file suffix

%(lineo)d The line number of the source code that called the logging function

%(funcName)s the function name that called the logging function

#日志:纪录系统运行的信息,记录系统发生的事件日志文件
#作用:了解系统运行的状态,去定位和分析错误问题

#debug:调试1级别
#info:系统正常运行2级别
#warning:警告信息程序有问题,但是他又不影响程序运行不会提Bug3级别默认的级别
#error:错误,出现问题,提bug4级别
#critical:严重的,快要崩溃
#常用:info,error
#反应出问题的严重程度


#logging能够设置日志级别生成日志信息,自带的不需要安装
importlogging

#生成日志信息生成时间文件名日志的状态类名方法名日志内容

fmt='%(asctime)s%(filename)s%(levelname)s%(module)s%(funcName)s%(message)s'

#设置日志级别
logging.basicConfig(level=logging.DEBUG,format=fmt)

#想要生成日志信息调用日志级别
logging.debug("debug信息")
logging.error("erroe信息")
logging.info("info信息")
logging.warning("warning信息")
logging.critical("critical信息")

 operation result:

The log information will only be saved in the log.log file, and the console will not output log information, which needs to be optimized

2. How to apply the log in the framework

First create a simple automated login framework

Login_page.py implements a login operation

from selenium import webdriver
from selenium.webdriver.common.by import By

from base_page import BasePage


classLoginPage(BasePage):
#采用POM思想,将元素定位拿出来找到输入框输入用户名和密码,找到登录按钮登录
#因为页面元素容易更改,所以将页面元素单独拎出来,方便后期修改
#页面元素
user=(By.NAME,'accounts')
pwd=(By.NAME,'pwd')
button=(By.NAME,'/html/body/div[4]/div/div[2]/form/div[3]/button')
#页面元素
url='http://39.98.138.157/shopxo/index.php?s=/index/user/logininfo.html'

#页面的操作流程
deflogin(self,txt,pwd_):
#原本的写法:self.driver.find_element_by_name('accounts').sendkeys('666666')
#为了不写多余的代码,利用关键字驱动进行了封装
self.open(self.url)
self.max()
self.input(self.user,txt)
self.input(self.pwd,pwd_)
self.click(self.button)


if__name__=='__main__':
driver=webdriver.Chrome()
user='666666'
pwd='111111'
Ip=LoginPage(driver)
Ip.login(user,pwd)

as the picture shows:

Base_page.py implements keyword-driven login

from selenium import webdriver
import logging


class BasePage:
#构造函数
def__init__(self,driver):
self.driver=driver

#访问url
def open(self,url):
self.driver.get(self.url)

#退出
def quit(self):
self.driver.quit()

#元素定位
def locator(self,loc):
returnself.driver.find_element(*loc)

#输入
def input(self,loc,txt):
self.locator(loc).send_keys(txt)

#点击
def click(self,loc):
self.locator(loc).click()

#窗体最大化
de fmax(self):
self.driver.maximize_window()

How to add logs to this login framework

Add input operations, click operations, etc. to the log file

Call the logging class

from selenium import webdriver
#日志

#logging能够设置日志级别生成日志信息,自带的不需要安装
import logging

#生成日志信息生成时间文件名日志的状态类名方法名日志内容

fmt='%(asctime)s%(filename)s%(levelname)s%(module)s%(funcName)s%(message)s'

#设置日志级别,日志格式,生成的日志文件保存到log.log
logging.basicConfig(level=logging.INFO,format=fmt,filename='./log.log')

class BasePage:
#构造函数
def__init__(self,driver):
logging.info("初始化driver{}".format(driver))
self.driver=driver

#访问url
def open(self,url):
logging.info("访问{}网址".format(url))
self.driver.get(self.url)

#退出
def quit(self):
logging.info("退出网址")
self.driver.quit()

#元素定位
def locator(self,loc):
logging.info("正在定位{}元素".format(loc))
returnself.driver.find_element(*loc)

#输入
def input(self,loc,txt):
logging.info("正在定位{}元素,输入{}内容".format(loc,txt))
self.locator(loc).send_keys(txt)
#点击
def click(self,loc):
logging.info("正在点击{}元素".format(loc))
self.locator(loc).click()

#窗体最大化
def max(self):
self.driver.maximize_window()

Running result: display password error

 

Where to log in the framework, at the bottom of the framework, keyword-driven, in the login method, the method is correct try exceptet to capture error logs

3. Optimize the log module

1. Write the log separately as a packaged method

Note that the method has a return value

2. The keyword-driven file needs to call the log file

from selenium import webdriver
#logging能够设置日志级别生成日志信息,自带的不需要安装
#import logging
##生成日志信息生成时间文件名日志的状态类名方法名日志内容
#fmt='%(asctime)s%(filename)s%(levelname)s%(module)s%(funcName)s%(message)s'
##设置日志级别,日志格式,生成的日志文件保存到log.log
#logging.basicConfig(level=logging.INFO,format=fmt,filename='./log.log')

from baselog import test_log

#实例化对象要顶格写
log=test_log()


class BasePage:
#构造函数
def__init__(self,driver):
#logging.info("初始化driver{}".format(driver))
log.info("初始化driver{}".format(driver))
self.driver=driver
#访问url
def open(self,url):
log.info("访问{}网址".format(url))
#logging.info("访问{}网址".format(url))
self.driver.get(self.url)
#退出
def quit(self):
#logging.info("退出网址")
log.info("退出网址")
self.driver.quit()
#元素定位
def locator(self,loc):
#logging.info("正在定位{}元素".format(loc))
log.info("正在定位{}元素".format(loc))
returnself.driver.find_element(*loc)
#输入
def input(self,loc,txt):
#logging.info("正在定位{}元素,输入{}内容".format(loc,txt))
log.info("正在定位{}元素,输入{}内容".format(loc,txt))
self.locator(loc).send_keys(txt)
#点击
def click(self,loc):
try:
#logging.info("正在点击{}元素".format(loc))
log.info("正在点击{}元素".format(loc))
self.locator(loc).click()
exceptExceptionase:
#logging.error('错误日志'%e)
log.error('错误日志'%e)
#窗体最大化
def max(self):
self.driver.maximize_window()

3. Just run it directly

continue to optimize

Baselog.py is a basic application of the log, there is a problem, the console has no log output, only the log file has

basicConfig log to optimize the package

#对于日志再优化封装


import logging

#创建一个日志器
logger=logging.getLogger()

#设置日志级别
logger.setLevel(logging.INFO)

#指定日志输出的目的地,控制台那要先创建一个控制台
#创建控制台处理器
sh=logging.StreamHandler()
#把日志信息放到控制台里面去
logger.addHandler(sh)

#控制台里面输出的日志信息有些是我们不需要的,有些是我们需要但没有的
#创建格式器
#生成日志信息生成时间文件名日志的状态类名方法名日志内容
fmt='%(asctime)s%(filename)s%(levelname)s%(module)s%(funcName)s%(message)s'
formator=logging.Formatter(fmt)
#优化控制台格式
sh.setFormatter(formator)

#日志信息也要显示再log文件里面
#创建文本处理器,指定日志信息输出到文本处理器
fh=logging.FileHandler('./Log/log.log',encoding='utf-8')
logger.addHandler(fh)
#文本里面的日志格式也可以自己重新定义,这里没有重新定义,直接用上述定义好的格式
fh.setFormatter(formator)

logger.info('日志信息')

operation result:

Path: ../ The current directory is two levels up

./ Current directory up one level

/ current directory sibling

How to use optimized log files in the framework?

1. Define a method loggingtest.py with a return value in the log file

#对于日志再优化封装
import logging


def test_logging():
#创建一个日志器
logger=logging.getLogger()

#设置日志级别
logger.setLevel(logging.INFO)

#指定日志输出的目的地,控制台那要先创建一个控制台
#创建控制台处理器
sh=logging.StreamHandler()
#把日志信息放到控制台里面去
logger.addHandler(sh)

#控制台里面输出的日志信息有些是我们不需要的,有些是我们需要但没有的
#创建格式器
#生成日志信息生成时间文件名日志的状态类名方法名日志内容
fmt='%(asctime)s%(filename)s%(levelname)s%(module)s%(funcName)s%(message)s'
formator=logging.Formatter(fmt)
#优化控制台格式
sh.setFormatter(formator)

#日志信息也要显示再log文件里面
#创建文本处理器,指定日志信息输出到文本处理器
fh=logging.FileHandler('./Log/log.log',encoding='utf-8')
logger.addHandler(fh)
#文本里面的日志格式也可以自己重新定义,这里没有重新定义,直接用上述定义好的格式
fh.setFormatter(formator)

return logger

#logger.info('日志信息')

2. Reference the method basepage.py at the bottom layer of the framework (keyword driver layer)

The following information remains unchanged

4. An error was reported when starting up, and the login was not successful.

 

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="/html/body/div[4]/div/div[2]/form/div[3]/button"]"}

  (Session info: chrome=92.0.4515.159)

Common exceptions. NoSuchElementException: Message: No such element: Unable to find element: { "method": "css selector", "selector": "[name="/html/body/div[4]/div/div[2] /form/div[3]/button"]"

(Session information: chrome=92.0.4515.159)

If you log in by yourself, you can’t log in successfully. The most probable problem is the server problem.

Guess you like

Origin blog.csdn.net/Lynn1111111/article/details/124860845