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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import logging
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import StaleElementReferenceException
class Base:
def __init__(self, driver):
self.driver = driver
self.logger = logging.getLogger(__name__)
def find_element(self, by, value):
"""
查找元素,并处理可能的异常
"""
try:
return WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((by, value))
)
except StaleElementReferenceException:
return WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((by, value))
)
def perform_login(self, username, password, verification_code):
"""
执行登录操作
"""
self.driver.implicitly_wait(5)
self.driver.refresh()
try:
# 清除并输入用户名
username_input = self.find_element(By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[2]/div/form/div[1]/div/input')
username_input.clear()
username_input.send_keys(username)
# 清除并输入密码
password_input = self.find_element(By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[2]/div/form/div[2]/div/input')
password_input.clear()
password_input.send_keys(password)
# 清除并输入验证码
verification_input = self.find_element(By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[2]/div/form/div[3]/div[1]/div/input')
verification_input.clear()
verification_input.send_keys(verification_code)
# 点击登录按钮
login_button = self.find_element(By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[2]/div/form/div[4]/input')
login_button.click()
# 检查登录是否成功
if self.check_login_null():
self.logger.info("登录失败,必填项不能为空!断言成功!")
elif self.check_login_success():
self.logger.info("登录成功,断言成功!")
self.return_to_login_page()
elif self.check_login_failure():
self.logger.info("登录失败, 必填项错误!断言成功!")
else:
self.logger.error("登录状态未知,断言失败!")
pytest.fail("登录状态未知,断言失败!")
except StaleElementReferenceException as e:
self.logger.error(f"元素已失效,重新查找... {e}")
pytest.fail(f"元素已失效,重新查找... {e}")
except Exception as e:
self.logger.error(f"登录操作失败: {e}")
pytest.fail(f"登录操作失败: {e}")
def return_to_login_page(self):
"""
返回登录页面
"""
try:
# 定位到退出按钮并点击
logout_button = self.find_element(By.XPATH, '//*[@id="app"]/div/div[1]/div/img[3]')
logout_button.click()
# 记录日志,表明已成功尝试返回登录页面
self.logger.info("已成功返回登录页面")
# 刷新页面,并等待一段时间以确保页面加载完成
self.driver.refresh()
self.driver.implicitly_wait(5)
except Exception as e:
# 如果发生异常,记录错误日志并使测试用例失败
self.logger.error(f"返回登录页面失败: {e}")
pytest.fail(f"返回登录页面失败: {e}")
def check_login_null(self):
"""
检查登录失败,必填项为空的情况
"""
try:
login_element_text = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/p'))
).text
# 断言登录失败后,提示信息中包含'请输入'关键词
assert "请输入" in login_element_text, "登录失败后未找到'请输入'提示语"
return True
except Exception as e:
self.logger.error(f"登录失败检查失败: {e}")
return False
def check_login_success(self):
"""
检查登录成功的情况
"""
try:
message_element_text = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), '欢迎')]"))
).text
# 断言欢迎信息是否符合预期
assert "欢迎 预定标准版测试" == message_element_text, "登录成功后找到'欢迎 预定标准版测试'提示语"
return True
except Exception as e:
self.logger.error(f"登录成功检查失败: {e}")
return False
def check_login_failure(self):
"""
检查登录失败的情况
"""
try:
login_element_text = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[1]/div[1]'))
).text
# 断言登录元素的文本是否为"账号登录",如果符合,则说明登录失败检查成功
assert "账号登录" == login_element_text, "登录失败后未找到'账号登录'提示语"
return True
except Exception as e:
self.logger.error(f"登录失败检查失败: {e}")
return False