-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorators.py
More file actions
68 lines (58 loc) · 2.39 KB
/
Copy pathdecorators.py
File metadata and controls
68 lines (58 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/5/25 8:00
# @Author : libaojin
# @File : decorators.py
import logging.config
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from functools import wraps
from selenium.webdriver.support.wait import WebDriverWait
# 加载日志配置文件
logging.config.fileConfig('logging.conf')
logger = logging.getLogger(__name__)
def log_exceptions(func):
@wraps(func)
def wrapper(*args, **kwargs):
self = args[0] if args else None
if self and hasattr(self, 'logger'):
self.logger.info(f"开始执行测试用例 {func.__name__}")
else:
logger.info(f"开始执行测试用例 of {func.__name__}")
try:
return func(*args, **kwargs)
except Exception as e:
if self and hasattr(self, 'logger'):
self.logger.exception(f"发生异常 {func.__name__}")
else:
logger.exception(f"发生异常 {func.__name__}")
raise
return wrapper
def ensure_page_loaded(element_locator, start_url, timeout=5):
"""
装饰器:确保页面加载,等待指定元素出现。如果元素未出现,则导航到起始页面。
:param element_locator: 定位元素的定位器
:param start_url: 起始页面的URL
:param timeout: 等待时间(默认5秒)
"""
def decorator(func):
@wraps(func)
def wrapper(page, *args, **kwargs):
try:
WebDriverWait(page.driver, timeout).until(
EC.presence_of_element_located(element_locator)
)
print(f"Element {element_locator} found on the page.")
except TimeoutException:
print(f"Element {element_locator} not found, navigating to {start_url}.")
page.driver.get(start_url)
try:
WebDriverWait(page.driver, timeout).until(
EC.presence_of_element_located(element_locator)
)
print(f"Element {element_locator} found on the start URL page.")
except TimeoutException:
print(f"Element {element_locator} still not found after navigating to start URL.")
return func(page, *args, **kwargs)
return wrapper
return decorator