- Auth:认证登录token机制
- BasePage:公共方法
- Common:
- Config
- Logs
- PageLocators
- Pages
- TestCases
- TestDatas
新建BasePage.py文件
点击查看代码
from playwright.sync_api import expect, Page
from Config.config import Config
import os,time
class BasePage:
def __init__(self, page: Page):
self.page = page
def _goto_url(self, url):
"""打开网页"""
self.page.goto(url)
def _click(self, locator, frame_locator=None):
"""
点击元素
:param locator: 传入元素定位器
:param frame_locator: 传入frame框架的的定位器,如果没有传入,则一般点击
:return:
"""
try:
if frame_locator is not None:
self.page.frame_locator(frame_locator).locator(locator).click()
else:
self.page.click(locator)
except Exception as e:
print(e)
def click_text(self, text):
"""
点击指定文本内容的元素
:param text: 文本
:return:
"""
try:
self.page.get_by_text(text).click()
except Exception as e:
print(e)
def hover(self, locator, frame_locator=None):
"""
定位目标元素,并执行鼠标悬停操作
:param locator: 传入元素定位器
:param frame_locator: 传入frame框架的的定位器,如果没有传入,则一般点击
:return:
"""
try:
if frame_locator is not None:
self.page.frame_locator(frame_locator).locator(locator).hover()
else:
self.page.hover(locator)
except Exception as e:
print(e)
# def _fill(self, locator, value, frame_locator=None):
# """
# 定位元素,输入内容
# :param locator:传入元素定位器
# :param value:传入输入的值
# :param frame_locator: 传入frame框架
# :return:
# """
# try:
# if frame_locator is not None:
# self.page.frame_locator(selector=frame_locator).locator(selector_or_locator=locator).fill(value)
# else:
# self.page.fill(selector=locator, value=value)
# except Exception as e:
# print(e)
def type(self, locator, value, frame_locator=None):
"""
模拟人工输入,一个键一个键的输入
:param locator:传入元素定位器
:param value:传入输入的值
:param frame_locator: 传入frame框架
:return:
"""
try:
if frame_locator is not None:
self.page.frame_locator(selector=frame_locator).locator(selector_or_locator=locator).type(text=value,
delay=50)
else:
self.page.type(selector=locator, text=value, delay=50)
except Exception as e:
print(e)
def file(self, locator, files, frame_locator=None):
"""
上传文件的方法
:param locator: 定位器
:param files: 单个文件名,或者列表存放多个文件
:param frame_locator: iframe框架定位器,如果没有就不传
:return:
"""
try:
if frame_locator is not None:
self.page.frame_locator(frame_locator).locator(locator).set_input_files(files=files)
else:
self.page.locator(locator).set_input_files(files=files)
except Exception as e:
print(e)
def choose_file_from_custom_button(self, button_selector, file_path):
# 如果不是input输入框,必须点开文件框的情况(selenium上没法实现的操作)
# 使用expect_file_chooser()来等待文件选择器出现
try:
with self.page.expect_file_chooser() as fc_info:
self.page.locator(button_selector).click()
# 获取文件选择器对象并设置文件路径
file_chooser = fc_info.value.set_files(file_path)
return file_chooser
except Exception as e:
print(e)
def ele_to_be_visible(self, locator):
"""断言元素可见"""
return expect(self.page.locator(locator)).to_be_visible()
def _ele_to_be_visible_force(self, locator, frame_locator=None, timout: int = 5):
"""强制等待某个元素可见"""
ele = None
if frame_locator is not None:
ele = self.page.frame_locator(frame_locator).locator(locator)
else:
ele = self.page.locator(locator)
for t in range(0, timout):
self.page.wait_for_timeout(500)
if ele.is_visible():
break
else:
raise Exception("元素未找到!")
def select_option_by_keyword(self, dropdown_locator, option_locator, keyword):
"""下拉选项操作"""
try:
# 点击下拉框,展开选项
self.click(dropdown_locator)
time.sleep(1)
print(f"文本内容为:{keyword}")
# 定位选项列表
options = self.page.query_selector_all(option_locator)
print(f"options为:{options}")
# 遍历选项并选择匹配的关键字
for option in options:
print(f"option为:{option}")
# assert keyword in option.text_content(), f"{option.text_content()} 不包含关键字 {keyword}"
text = option.evaluate('(element) => element.textContent')
print(f"option.text为:{text}")
if keyword in text:
print("进入了判断")
option.click()
print("点击完成")
break
except Exception as e:
print(e)
def _ele_is_checked(self, selector):
"""判断元素是否被选选中"""
return self.page.is_checked(selector)
def _browser_operation(self, reload=False, forward=False, back=False):
"""浏览器操作,reload 刷新,forward 前进,back 后退"""
if reload:
self.page.reload()
if back:
self.page.go_back()
if forward:
self.page.go_forward()
def screenshot(self, path, full_page=True, locator=None):
"""截图功能,默认截取全屏,如果传入定位器表示截取元素"""
if locator is not None:
self.page.locator(locator).screenshot(path=path)
return path
self.page.screenshot(path=path, full_page=full_page)
return path
def _del_auth(self):
auth_path = Config.auth_dir + os.path.sep + "auth.json"
if os.path.exists(auth_path):
os.remove(auth_path)
# def ocr_image(image_path):
# ocr = ddddocr.DdddOcr(show_ad=False) # 实例化 OCR 对象
# with open(image_path, 'rb') as f: # 打开图片
# img_bytes = f.read() # 读取图片内容
# result = ocr.classification(img_bytes) # 进行 OCR 分类
# return result