57. Page Object模式 - selenium fin
2024-03-29 本文已影响0人
薛东弗斯
优点:增加日志
第一次封装,实现page.find_element(之前,只能用driver.find_element)
project > base > base_page.py
project > utils > log_util.py
project > utils > get_filepath.py
project > PO > test_baidu.py
project > page > page_baidu.py
# project > base > base_page.py
from utils.log_util import logger
class BasePage:
def __init__(self,driver): # 此处的driver是形参,最终会调用conftest里面的driver
self.driver = driver
# self.driver.maximize_window()
self.driver.implicitly_wait(10)
self.driver.get("https://www.baidu.com/")
# 为了让page也有find_element功能,此处进行定义
def find_element(self,locator):
logger.info(f"定位元素{locator}")
return self.driver.find_element(*locator)
# project > utils > log_util.py
import logging
import os
import time
from utils.get_filepath import get_log_path
log_path = get_log_path()
if not os.path.exists(log_path):
os.mkdir(log_path)
class Logger:
def __init__(self):
# 定义日志位置和文件名
self.logname = os.path.join(log_path, "{}.log".format(time.strftime("%Y-%m-%d")))
# 定义一个日志容器
self.logger = logging.getLogger("log")
# 设置日志打印的级别,大于DEBUG都可以被打印出来
self.logger.setLevel(logging.DEBUG)
# 创建日志输入的格式
self.formater = logging.Formatter(
'[%(asctime)s][%(filename)s %(lineno)d][%(levelname)s]: %(message)s')
# 创建日志处理器,用来存放日志文件
self.filelogger = logging.FileHandler(self.logname, mode='a', encoding="UTF-8")
# 文件存放日志级别
self.filelogger.setLevel(logging.DEBUG)
# 文件存放日志格式
self.filelogger.setFormatter(self.formater)
# 创建日志处理器,在控制台打印
self.console = logging.StreamHandler()
# 设置控制台打印日志界别
self.console.setLevel(logging.DEBUG)
# 控制台打印日志格式
self.console.setFormatter(self.formater)
# 将日志输出渠道添加到日志收集器中
self.logger.addHandler(self.filelogger)
self.logger.addHandler(self.console)
logger = Logger().logger
if __name__ == '__main__':
logger.debug("我打印DEBUG日志")
logger.info("我打印INFO日志")
logger.warning("我打印WARNING日志")
logger.error("我打印ERROR日志")
# project > utils > get_filepath.py
import os
import time
def get_report_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "allure-report/export",
'prometheusData.txt')
return path
def get_screen_shot_path():
file_name = "截图{}.png".format(time.strftime("%Y-%m-%d_%H-%M-%S"))
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", file_name)
return path
def get_logo_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", "logo.jpg")
return path
def download_file_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file")
return path
def get_yaml_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "data.yaml")
return path
def get_ini_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config", "settings.ini")
return path
def get_log_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "log")
return path
if __name__ == '__main__':
print(get_report_path())
# project > PO > test_baidu.py
from time import sleep
from page.page_baidu import PageBaidu
class TestBaidu:
def test_baidu3(self, driver):
page = PageBaidu(driver) # driver实例化
# # driver.find_element(*page.input).send_keys("UI自动化") # 之前只能用driver.find_element,希望通过page.find_element方法来做
page.find_element(page.input).send_keys("UI自动化")
# # driver.find_element(*page.button).click() # 用page.find_element替代driver.find_element
page.find_element(page.button).click()
sleep(2)
# project > page > page_baidu.py
from selenium.webdriver.common.by import By
from base.base_page import BasePage
from utils.log_util import logger
class PageBaidu(BasePage):
# 新闻
news = (By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]')
# 百度一下按钮
button = (By.ID, 'su')
# 百度输入框
input = (By.ID, 'kw')
def search_keyword(self, keyword): # 把操作封装到方法里面去。
logger.info("查找元素并输入内容")
self.driver.find_element(*self.input).send_keys(keyword)
logger.info("点击按钮")
self.driver.find_element(*self.button).click()