import pytest from pages import UserPage @pytest.mark.uniapp @pytest.mark.user_center class TestUserCenter: """用户中心测试类""" @pytest.fixture(autouse=True) def setup(self, uniapp_page, config): """设置测试环境""" self.page = uniapp_page self.config = config self.user_center = UserPage(self.page, config["uniapp_url"]) self.user_center.navigate() def test_user_center_page_loaded(self): """测试用户中心页面加载""" assert self.user_center.is_loaded() def test_get_user_info(self): """测试获取用户信息""" user_info = self.user_center.get_user_info() assert "username" in user_info assert "role" in user_info assert "phone" in user_info assert "email" in user_info assert user_info["username"] is not None def test_edit_profile_success(self): """测试成功编辑用户资料""" self.user_center.click_edit_profile() self.user_center.edit_profile( {"username": "updated_username", "phone": "13900139000", "email": "updated@example.com"} ) user_info = self.user_center.get_user_info() assert user_info["username"] == "updated_username" assert user_info["phone"] == "13900139000" assert user_info["email"] == "updated@example.com" def test_edit_profile_with_invalid_email(self): """测试使用无效邮箱编辑用户资料""" original_email = self.user_center.get_user_info()["email"] self.user_center.click_edit_profile() self.user_center.edit_profile({"email": "invalid-email"}) user_info = self.user_center.get_user_info() assert user_info["email"] == original_email def test_change_password_success(self): """测试成功修改密码""" self.user_center.click_change_password() self.user_center.change_password("oldpassword", "newpassword123", "newpassword123") assert "dashboard" in self.page.url.lower() def test_change_password_with_mismatch(self): """测试使用不匹配的密码修改密码""" self.user_center.click_change_password() self.user_center.change_password("oldpassword", "newpassword123", "differentpassword") assert "/user/center" in self.page.url def test_change_password_with_empty_fields(self): """测试使用空字段修改密码""" self.user_center.click_change_password() self.user_center.change_password("", "", "") assert "/user/center" in self.page.url def test_click_settings(self): """测试点击设置""" self.user_center.click_settings() assert "/settings" in self.page.url def test_logout(self): """测试登出""" self.user_center.logout() assert "/login" in self.page.url def test_click_my_favorites(self): """测试点击我的收藏""" self.user_center.click_my_favorites() assert "/favorites" in self.page.url def test_click_my_history(self): """测试点击我的历史""" self.user_center.click_my_history() assert "/history" in self.page.url def test_click_my_subscriptions(self): """测试点击我的订阅""" self.user_center.click_my_subscriptions() assert "/subscriptions" in self.page.url def test_click_notifications(self): """测试点击通知""" self.user_center.click_notifications() assert "/notifications" in self.page.url def test_get_notification_count(self): """测试获取未读通知数量""" count = self.user_center.get_notification_count() assert isinstance(count, int) assert count >= 0 def test_clear_notifications(self): """测试清除所有通知""" self.user_center.clear_notifications() count = self.user_center.get_notification_count() assert count == 0 def test_get_favorites_list(self): """测试获取收藏列表""" self.user_center.click_my_favorites() favorites = self.user_center.get_favorites_list() assert isinstance(favorites, list) def test_remove_favorite(self): """测试移除收藏""" self.user_center.click_my_favorites() favorites = self.user_center.get_favorites_list() if favorites: favorite_id = favorites[0]["id"] initial_count = len(favorites) self.user_center.remove_favorite(favorite_id) updated_favorites = self.user_center.get_favorites_list() assert len(updated_favorites) == initial_count - 1 assert not any(f["id"] == favorite_id for f in updated_favorites) def test_get_history_list(self): """测试获取历史记录列表""" self.user_center.click_my_history() history = self.user_center.get_history_list() assert isinstance(history, list) def test_clear_history(self): """测试清除历史记录""" self.user_center.click_my_history() self.user_center.clear_history() history = self.user_center.get_history_list() assert len(history) == 0 def test_get_subscription_status(self): """测试获取订阅状态""" status = self.user_center.get_subscription_status() assert "is_subscribed" in status assert "plan" in status assert "expire_date" in status assert "auto_renew" in status def test_toggle_auto_renew(self): """测试切换自动续费""" initial_status = self.user_center.get_subscription_status() initial_auto_renew = initial_status["auto_renew"] self.user_center.toggle_auto_renew() updated_status = self.user_center.get_subscription_status() assert updated_status["auto_renew"] != initial_auto_renew def test_cancel_subscription(self): """测试取消订阅""" self.user_center.cancel_subscription() status = self.user_center.get_subscription_status() assert not status["is_subscribed"] def test_click_help(self): """测试点击帮助""" self.user_center.click_help() assert "/help" in self.page.url def test_click_about(self): """测试点击关于""" self.user_center.click_about() assert "/about" in self.page.url @pytest.mark.parametrize( "username,phone,email", [ ("user001", "13800138001", "user001@example.com"), ("user002", "13800138002", "user002@example.com"), ("user003", "13800138003", "user003@example.com"), ], ) def test_edit_profile_multiple_times(self, username, phone, email): """测试多次编辑用户资料""" self.user_center.click_edit_profile() self.user_center.edit_profile({"username": username, "phone": phone, "email": email}) user_info = self.user_center.get_user_info() assert user_info["username"] == username assert user_info["phone"] == phone assert user_info["email"] == email def test_user_avatar_display(self): """测试用户头像显示""" avatar = self.page.locator(".user-avatar") assert avatar.is_visible()