fix and add som usecases

This commit is contained in:
2025-09-15 17:41:07 +08:00
parent 838806b9e3
commit 414737fd48
37 changed files with 1133 additions and 51 deletions

View File

@@ -0,0 +1,34 @@
import pytest
import pandas as pd
from page_objects.login_page import LoginPage
@pytest.fixture(scope="function")
def logged_in_driver(driver, base_url):
"""
A fixture that provides a logged-in WebDriver instance.
It performs the following steps:
1. Reads valid login credentials from 'data/login_data.csv'.
2. Navigates to the login page.
3. Performs the login action.
4. Yields the driver to the test function.
The test function then runs on a page that requires authentication.
"""
# 1. 读取有效的登录凭据 (假设第一行为有效数据)
df = pd.read_csv('data/login_data.csv')
valid_user = df[df['test_case'] == 'valid_credentials'].iloc[0]
email = valid_user['email']
password = valid_user['password']
# 2. 初始化登录页面并打开
login_page = LoginPage(driver)
login_page.open(base_url)
# 3. 执行登录
login_page.login(email, password)
# 4. 将已登录的 driver 提供给测试用例
yield driver
# 测试结束后driver fixture 会自动关闭浏览器

View File

@@ -0,0 +1,102 @@
import time
import pytest
from selenium.webdriver.support.ui import WebDriverWait
from page_objects.home_dashboard_page import HomeDashboardPage
from page_objects.profile_editor_page import ProfileEditorPage
from page_objects.settings_panel_page import SettingsPanelPage
from page_objects.danger_zone_page import DangerZonePage
@pytest.mark.regression
@pytest.mark.usefixtures("logged_in_driver")
class TestDashboard:
"""
Test suite for the Home page and its nested pages (Dashboard).
Requires user to be logged in.
"""
def test_navigation_between_pages(self, logged_in_driver):
"""
Tests that the user can successfully navigate between the Profile,
Settings, and Danger Zone pages.
"""
# Arrange
dashboard_page = HomeDashboardPage(logged_in_driver)
settings_page = SettingsPanelPage(logged_in_driver)
danger_zone_page = DangerZonePage(logged_in_driver)
profile_page = ProfileEditorPage(logged_in_driver)
# Act & Assert: Navigate back to dashborade and verify
text = dashboard_page.verify_title_is_dashboarde()
assert text == 'Test Cases' , 'error switch to dashhoard tab'
# Act & Assert: Navigate to Settings tab and verify
dashboard_page.navigate_to_settings()
text = settings_page.verify_title_is_settings()
assert text == 'Application Settings' , 'error switch to settings tab'
# Act & Assert: Danger Zone and verify
text = danger_zone_page.verify_danger_zone_exist()
assert text == 'Danger Zone' , 'no exists Danger Zone '
# Act & Assert: Navigate back to Profile and verify
dashboard_page.navigate_to_profile()
print(text)
text = profile_page.verify_title_is_profile()
assert text == 'Personal Information' , 'error switch to profile tab'
def test_profile_editor_interaction(self, logged_in_driver):
"""
Tests basic interaction with the profile editor form.
"""
# Arrange
dashboard_page = HomeDashboardPage(logged_in_driver)
profile_page = ProfileEditorPage(logged_in_driver)
# Act: Ensure we are on the profile page and update username
dashboard_page.navigate_to_profile()
new_username = "Automation Tester"
profile_page.set_username(new_username)
# self.profile_page.click_save_changes() # This action is commented out as it might not have a real backend effect
# Assert: Check if the value was updated in the input field
updated_value = profile_page.find_element(profile_page.USERNAME_INPUT).get_attribute("value")
assert updated_value == new_username
def test_danger_zone_dialog_flow(self, logged_in_driver):
"""
Tests that the 'Delete Account' button shows a confirmation dialog
and that the cancel button closes it.
"""
# Arrange
danger_zone_page = DangerZonePage(logged_in_driver)
# Act: Click the delete button
danger_zone_page.click_delete_account()
# # Assert: The confirmation dialog is visible
# assert danger_zone_page.verify_confirmation_dialog_exist(), \
# "Confirmation dialog should be visible after clicking 'Delete Account'."
# Act: Click the cancel button
time.sleep(1)
danger_zone_page.click_cancel_in_dialog()
text = danger_zone_page.verify_confirmation_dialog_exist()
# Assert: The dialog is no longer visible
assert text == 'Are you absolutely sure?', \
"Confirmation dialog should not be visible after clicking 'Cancel'."
# Act: Click the delete button
time.sleep(1)
danger_zone_page.click_delete_account()
danger_zone_page.set_dialog_input()
danger_zone_page.click_confirm_in_dialog()
# 等待跳转到登录页
WebDriverWait(logged_in_driver, 5).until(
lambda driver: '/login' in driver.current_url
)
# 断言当前 URL
current_url = logged_in_driver.current_url
assert current_url.endswith('/login'), f"Expected to be on /login, but got {current_url}"

View File

@@ -0,0 +1,44 @@
import pytest
from page_objects.home_page import HomePage
from page_objects.login_page import LoginPage
@pytest.mark.usefixtures("logged_in_driver")
class TestHomePage:
"""
Test suite for the Home page, which requires user authentication.
"""
def test_welcome_message_is_displayed(self, logged_in_driver):
"""
Verifies that the correct welcome message is displayed after login.
"""
home_page = HomePage(logged_in_driver)
welcome_text = home_page.get_welcome_message()
# Assert against the hardcoded user name from auth.tsx
assert "Welcome, Test User!" in welcome_text, "Welcome message is not correct or not found."
def test_dashboard_is_visible_on_load(self, logged_in_driver):
"""
Verifies that the dashboard tab content is visible by default.
"""
home_page = HomePage(logged_in_driver)
assert home_page.is_dashboard_content_visible(), "Dashboard content should be visible by default."
def test_switch_to_profile_tab(self, logged_in_driver):
"""
Verifies that the user can switch to the Profile tab.
"""
home_page = HomePage(logged_in_driver)
home_page.switch_to_profile_tab()
# A proper test would assert that profile-specific elements are visible.
def test_logout_functionality(self, logged_in_driver):
"""
Verifies that clicking the logout button logs the user out and redirects to the login page.
"""
home_page = HomePage(logged_in_driver)
home_page.click_logout_button()
# Verify redirection to the login page
login_page = LoginPage(logged_in_driver)
assert login_page.is_login_form_visible(), "After logout, the login form should be visible."