提交 ba295466 编写于 作者: hzq18867809616's avatar hzq18867809616

更新web/common/base_page.py, web/common/weblog.py, web/common/get_yaml.py

上级 d8605e75
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
from weblog import WebLog
from get_path import GetPath
from datetime import datetime
import os
class BasePage():
def __init__(self, driver):
driver = webdriver.Chrome()
self.driver = driver
self.mylog = WebLog().runlog()
self.mypath = GetPath()
self.wait = WebDriverWait(self.driver, timeout=10, poll_frequency=0.1)
# 封装等待元素可见
def wait_element_visible(self, locator):
try:
t = self.wait.until(expected_conditions.visibility_of_element_located(locator))
except:
self.mylog.error(f'等待原价加载失败{locator}')
return t
# 封装等待元素加载
def wait_element_present(self, locator):
try:
t = self.wait.until(expected_conditions.presence_of_element_located(locator))
except:
self.mylog.error(f'等待元素加载失败{locator}')
return t
# 封装等待元素可被点击
def wait_element_clickable(self, locator):
try:
t = self.wait.until(expected_conditions.element_to_be_clickable(locator))
except:
self.mylog.error(f'等待元素可被点击时发生错误{locator}')
return t
# 判断某个元素是否出现
def element_is_dispaly(self, locator):
return self.driver.find_element(locator).is_displayed()
# 判断是否存在某个元素
def element_is_exist(self, locator):
result = True
try:
self.driver.find_element(locator)
return result
except:
result = False
return result
# 切换至最新的窗口
def switch_to_last_windows(self,id=-1):
try:
windows_id = self.driver.window_handles
self.driver.switch_to.window(windows_id[id])
except:
self.mylog(f'切换最新窗口失败')
# 切换至弹窗
def switch_to_alter(self):
self.driver.switch_to.alert
# 切换至iframe
def switch_to_iframe(self):
frame_ele = self.driver.find_element_by_xpath('//iframe')
self.driver.switch_to.frame(frame_ele)
# 屏幕截图
def save_screenshot(self):
# 设置截图保存路径
img_path = self.mypath.get_path('img')
img_time = datetime.now().strftime('%y-%m-%d-%H-%M-%S')
img_name = img_path + img_time + ".png"
self.driver.save_screenshot(img_name)
return self
import yaml
import os
from webtest.script.get_path import GetPath
class GetYaml():
def __init__(self, yamlfile=None):
self.addr = GetPath.get_path('config')
if yamlfile == None:
self.yaml_filename = os.path.join(self.addr, 'webtest.yaml')
else:
self.yaml_filename = os.path.join(self.addr, yamlfile)
with open(self.yaml_filename, encoding='utf-8') as y_file:
self.load_file = yaml.full_load(y_file)
def getyaml_info(self, section, option):
return self.load_file[section][option]
yaml_info = GetYaml()
import logging
import os
from webtest.script.get_path import GetPath
class WebLog():
def __init__(self, filename=None):
# 若没有输入日志文件名则选择默认文件名
if filename == None:
self.filename = 'webtest'
else:
self.filename = filename
# 创建日志对象
self.weblog = logging.getLogger(self.filename)
self.weblog.setLevel('DEBUG')
# 创建日志输出渠道,控制台输入日子
self.consle = logging.StreamHandler()
self.consle.setLevel('DEBUG')
# 获取log地址
self.log_addr = GetPath()
# 创建日志输出渠道,日志文件输出
self.fileaddr = os.path.join(self.log_addr.get_path('log'), "web.log")
self.log_file = logging.FileHandler(self.fileaddr, encoding='utf-8')
self.log_file.setLevel('DEBUG')
# 设置日志格式,报错时间-报错等级-报错详细信息-报错日志文件的名字-报错代码行数
self.log_format = logging.Formatter("%(asctime)s - [%(levelname)s] - [msg]:%(message)s - %(name)s - %(lineno)d")
self.consle.setFormatter(self.log_format)
self.log_file.setFormatter(self.log_format)
# 关联日志对象与日志渠道
self.weblog.addHandler(self.consle)
self.weblog.addHandler(self.log_file)
def runlog(self):
return self.weblog
if __name__ == '__main__':
a = WebLog()
b = a.runlog()
b.info('这是一个提示信心')
b.error('这是一个错误信息')
b.warning('这是一个警告信息')
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册