selenium4封装一个完整的POM模式,两种登录代码,一种正常登录,一种登录失败,并分别获取登录成功后的提示信息,登录失败后的提示信息
时间: 2023-10-15 19:08:09 浏览: 43
好的,我可以为您提供一个基于Selenium4的Page Object Model(POM)模式的示例代码,同时包括正常登录和登录失败的情况,并获取相应的提示信息。
首先,我们需要创建一个Page类,用于封装登录页面的元素和操作,代码如下:
```python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username_input = (By.NAME, "username")
self.password_input = (By.NAME, "password")
self.login_button = (By.XPATH, "//button[@type='submit']")
self.error_message = (By.XPATH, "//div[@class='error-message']")
self.success_message = (By.XPATH, "//div[@class='success-message']")
def enter_username(self, username):
username_input = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.username_input))
username_input.clear()
username_input.send_keys(username)
def enter_password(self, password):
password_input = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.password_input))
password_input.clear()
password_input.send_keys(password)
def click_login_button(self):
login_button = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.login_button))
login_button.click()
def login(self, username, password):
self.enter_username(username)
self.enter_password(password)
self.click_login_button()
def get_error_message(self):
error_message = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.error_message))
return error_message.text
def get_success_message(self):
success_message = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.success_message))
return success_message.text
```
在上面的代码中,我们使用了`By`、`WebDriverWait`和`expected_conditions`等Selenium库中的功能,分别用于定位元素、等待元素出现并获取元素,以及判断元素是否可见等操作。
接下来,我们需要编写登录测试用例的代码。下面是一个正常登录的测试用例代码:
```python
from selenium import webdriver
# 创建浏览器对象
driver = webdriver.Chrome()
# 打开登录页面
driver.get("url/to/login/page")
# 导入LoginPage类
from login_page import LoginPage
# 实例化LoginPage类
login_page = LoginPage(driver)
# 输入用户名和密码并登录
login_page.login("your_username", "your_password")
# 获取登录成功提示信息
success_message = login_page.get_success_message()
print(success_message)
# 关闭浏览器
driver.quit()
```
在上面的代码中,我们首先创建了一个浏览器对象,然后打开登录页面。接着,我们导入了上面定义的LoginPage类,并实例化该类。然后使用该类中的`login`方法,输入用户名和密码,并点击登录按钮。最后,我们使用`get_success_message`方法,获取登录成功提示信息,并打印输出。
下面是一个登录失败的测试用例代码:
```python
from selenium import webdriver
# 创建浏览器对象
driver = webdriver.Chrome()
# 打开登录页面
driver.get("url/to/login/page")
# 导入LoginPage类
from login_page import LoginPage
# 实例化LoginPage类
login_page = LoginPage(driver)
# 输入错误的用户名和密码并登录
login_page.login("wrong_username", "wrong_password")
# 获取登录失败提示信息
error_message = login_page.get_error_message()
print(error_message)
# 关闭浏览器
driver.quit()
```
在上面的代码中,我们同样首先创建了一个浏览器对象,然后打开登录页面。接着,我们导入了上面定义的LoginPage类,并实例化该类。然后使用该类中的`login`方法,输入错误的用户名和密码,并点击登录按钮。最后,我们使用`get_error_message`方法,获取登录失败提示信息,并打印输出。
需要注意的是,代码中的`url/to/login/page`需要替换成你要登录的页面的URL地址,`your_username`和`your_password`需要替换成你的用户名和密码。同时,为了让上面的代码正常运行,需要将Page类和测试用例代码分别保存为`login_page.py`和`login_test.py`文件,并将它们放在同一个目录下。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)