Files
WebAutoTest/commons/custom_expected_condition.py
CNWei 92d06dd9cf init: 初始化项目
- 创建了基本的项目结构
- 添加了 .gitignore 文件
- 配置了基本的开发环境
- 添加清华镜像源
- 设置了基础的文件夹和文件(如 commons, utils, POM, pytest.ini)

项目说明:
- [项目名称]:Web自动化测试
- [项目描述]:基于pytest,selenium的自动化测试工具
- [开发环境]:Python
2025-04-14 23:05:39 +08:00

100 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# coding=utf-8
"""
@author: CNWei
@Software: PyCharm
@contact: t6i888@163.com
@file: expected_conditional_function
@date: 2025/4/4 14:15
@desc:
"""
import logging
from dataclasses import dataclass
from typing import Dict, Callable, TypeVar, Tuple
from typing import Literal
from typing import Optional
from typing import Union
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webdriver import WebElement
from selenium.webdriver.support import expected_conditions as EC
__all__ = ["EC","custom_ec"]
logger = logging.getLogger(__name__)
T = TypeVar("T")
WebDriverOrWebElement = Union[WebDriver, WebElement]
"""
常用等待条件expected_conditions--来自EC模块
presence_of_element_located: 元素存在于DOM。
visibility_of_element_located: 元素可见。
element_to_be_clickable: 元素可点击。
title_contains: 页面标题包含特定文本。
text_to_be_present_in_element: 元素包含特定文本。
"""
# 自定义预期条件(Custom Expected Condition)
custom_ec = {
}
def register(name: str | None = None):
def decorator(func):
if name is not None:
custom_ec[name] = func
custom_ec[func.__name__] = func
return func
return decorator
@register()
def examples_no_parameters(driver: WebDriverOrWebElement)->T:
"""
无参预期条件函数
driver
- WebDriver的实例Ie、Firefox、Chrome或远程或 一个WebElement
- driver形参不可省略即使不使用
:return:
"""
_ = driver
list_ = [1, 2, 3, 4, 5, 6]
for item in list_:
logger.info(item)
if item == 5:
return item
@register()
def examples_have_parameters(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
"""
有参预期条件函数(暂不支持)
:param locator:
:return:
"""
def _predicate(driver: WebDriverOrWebElement):
try:
return driver.find_element(*locator)
except Exception as e:
return False
return _predicate
def func_3(mark):
...
def func_4(mark):
...
if __name__ == "__main__":
print(custom_ec)