郴州网站建设公司免费个人网站申请
1.项目背景
本项目是基于 SSM 框架(Spring、Spring MVC、MyBatis)开发的 个人博客系统,包括用户登录、博客发布、编辑、删除、查看等功能,提供博客列表页和详情页展示博客内容。用户登录后可以查看自己和其他用户的博客,并通过系统记录博客的发布时间、标题和发布者信息。项目采用 Selenium 自动化测试 进行功能验证,测试内容涵盖用户登录、博客列表展示、用户信息校验、博客数量统计、查看详情、博客发布与编辑、删除博客等,确保系统的稳定性和功能完整性。
2.测试内容
2.1.功能测试
2.2.1.编写测试用例
1. 登录测试
(1)介绍
系统已在数据库中存储了用户名和密码,用户成功登录后会跳转至博客列表页面;执行注销操作后,将返回登录页面。
(2)测试用例
-
(a)界面展示
-
(b)输入正确的账号和密码(如:账号 lisi,密码 123456)
- 预期结果:成功登录,并跳转至博客列表页面。
- 实际结果:
-
(c)输入错误的账号或密码(如:账号 lisi,密码 123)
- 预期结果:提示“账号或密码错误”。
- 实际结果:
2. 博客列表页及博客数量测试
(1)简介
登录成功后,系统将跳转至博客列表页面,该页面显示博客的总数量、当前用户的头像和用户名,并提供博客的基本信息(如标题、发布时间及部分内容)。
(2)测试用例
- (a)列表页展示(博客数量大于 0)
3. 博客详情页(点击“查看全文”)
(1)简介
用户点击任意一篇博客的“查看全文”按钮,即可进入博客详情页面,查看完整内容。
- 若文章属于当前用户,则页面提供“编辑”和“删除”选项。
- 若文章属于其他用户,则仅支持阅读,不提供编辑和删除权限。
(2)测试用例
- (a)详情页界面
- ① 其他用户的博客详情页
- 预期结果:页面显示博客标题和内容,不包含“编辑”和“删除”按钮。
- 实际结果:
- ② 当前用户的博客详情页
- 预期结果:页面显示博客标题和内容,并提供“编辑”和“删除”按钮。
- 实际结果:
- ① 其他用户的博客详情页
4. 博客编辑与发布
(1)简介
用户可通过博客列表页右上角的“写博客”按钮进入博客编辑页面,并进行新博客的撰写和发布。
(2)测试用例
- (a)登录后跳转至博客列表页
- (b)点击“写博客”按钮,跳转至博客编辑页面
- (c)输入标题和内容
- 预期结果:标题和内容不能为空,且输入的内容符合规范。
- 实际结果:
- (d)点击“发布文章”按钮
- 预期结果:博客成功发布,并跳转回博客列表页;新发布的文章位于列表顶部,并显示文章标题、发布时间及部分内容(若博客内容较长)。
- 实际结果:
5. 删除博客
(1)简介
在博客列表页面,点击当前用户发布的博客的“查看全文”按钮,进入详情页面后点击“删除”按钮,即可删除该博客,并返回博客列表页。删除后,该博客将不再显示。
(2)测试用例
- (a)博客列表页展示
- (b)点击“查看全文”按钮,进入博客详情页
- (c)点击“删除”按钮
- 预期结果:弹窗提醒,确定后博客成功删除,返回博客列表页,且该文章不再显示,文章数量减少1。
- 实际结果:
6. 退出当前账号
(1)简介
用户在博客列表页面点击“注销”按钮后,将返回登录页面,并清空账号和密码输入框内容。
(2)测试用例
- (a)博客列表页展示
- (b)点击右上角“注销”按钮
- 预期结果:成功退出,跳转至登录页面,且账号和密码输入框为空。
- 实际结果:
2.2 使用 Selenium 进行 Web 自动化测试(Python)
2.2.1 安装 WebDriverManager 和 Selenium
WebDriverManager 是用于管理 Web 驱动程序的工具,在进行 Selenium 测试时,浏览器需要对应的 WebDriver 进行控制和操作。WebDriverManager 能够自动检测本地浏览器版本并下载匹配的驱动程序(如 ChromeDriver、FirefoxDriver 等),避免手动下载和配置的繁琐过程,提高测试效率和稳定性。
2.2.2 创建测试项目
-
在项目中打开 File → Settings,进入设置界面。
-
在“Project: 当前项目名”下,检查是否已安装
selenium
和webdriver-manager
依赖。 -
若未安装,可使用以下命令进行安装:
pip install selenium webdriver-manager
2.2.3 编写自动化测试脚本
1.公共工具(Utils.py)
测试目标
-
提供
WebDriver
实例,供各测试脚本调用。 -
统一管理浏览器驱动,支持截图功能。
测试步骤
-
在
Utils.py
中定义BlogDriver
类,初始化Edge
浏览器驱动。 -
提供
get_driver()
方法,返回WebDriver
实例。 -
提供
screenshot()
方法,支持测试过程中截图。
import datetime
import os
import sys
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium import webdriver
from selenium.webdriver.edge.service import Serviceclass Driver:driver = Nonedef __init__(self):"""初始化WebDriver并启动浏览器"""options = webdriver.EdgeOptions()options.page_load_strategy = 'eager'# options.add_argument('-headless') # 启动无界面模式edge_driver_path = EdgeChromiumDriverManager().install()self.driver = webdriver.Edge(service=Service(edge_driver_path), options=options)def getScreenShot(self):"""保存当前页面的截图到指定目录"""dirname = datetime.datetime.now().strftime('%Y-%m-%d')screenshot_dir = os.path.join(os.path.dirname(os.getcwd()), 'images', dirname)# 如果截图目录不存在,则创建if not os.path.exists(screenshot_dir):os.makedirs(screenshot_dir)# 根据方法名和时间戳生成唯一的文件名filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + ".png"# 保存截图screenshot_path = os.path.join(screenshot_dir, filename)self.driver.save_screenshot(screenshot_path)# 创建BlogDriver实例
BlogDriver = Driver()
2.登录功能自动化测试(BlogLogin.py)
测试目标
-
验证博客系统的登录功能是否正常,包括各种异常情况的处理。
测试步骤
-
创建
BlogLogin.py
,引入Utils.py
及相关依赖。 -
定义
BlogLogin
类,管理登录测试用例。 -
实现以下测试方法:
-
正常登录:输入正确的用户名和密码,验证是否成功跳转到主页。
-
错误登录:测试以下情况:
-
空用户名
-
空密码
-
无效用户名
-
无效密码
-
-
个人资料页面注销:
-
进入个人资料页面。
-
点击注销按钮。
-
验证是否跳转到登录页面。
-
-
-
在
RunTest.py
中调用BlogLogin
进行测试。
import time
from common.Utils import BlogDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.alert import Alertclass BlogLogin:url = ''driver = ''def __init__(self):"""初始化登录页面并打开指定URL"""self.url = "http://8.137.19.140:9090/blog_login.html"self.driver = BlogDriver.driverself.driver.get(self.url)def loginSucTest1(self):"""测试用户 'zhangsan' 的登录成功"""self._login('zhangsan', '123456')self._verify_login_success()self.driver.back()def loginSucTest2(self):"""测试用户 'lisi' 的登录成功"""self._login('lisi', '123456')self._verify_login_success()self.driver.back()def _login(self, username, password):"""封装的登录方法,接收用户名和密码"""WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#username')))self.driver.find_element(By.CSS_SELECTOR, '#username').clear()self.driver.find_element(By.CSS_SELECTOR, "#password").clear()self.driver.find_element(By.CSS_SELECTOR, '#username').send_keys(username)self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(password)self.driver.find_element(By.CSS_SELECTOR, '#submit').click()def _verify_login_success(self):"""验证用户是否成功登录"""BlogDriver.getScreenShot()try:WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'body > div.container > div.left > div > h3')))except:passassert self.driver.find_element(By.CSS_SELECTOR, 'body > div.container > div.left > div > h3') is not Nonedef test_login_empty_username_empty_password(self):"""测试用户名和密码为空时的登录行为"""self._login('', '')self._verify_alert('账号或密码不能为空')self.driver.back()def test_login_empty_username_valid_password(self):"""测试用户名为空,密码有效时的登录行为"""self._login('', '123456')self._verify_alert('账号或密码不能为空')self.driver.back()def test_login_empty_username_invalid_password(self):"""测试用户名为空,密码无效时的登录行为"""self._login('', '123')self._verify_alert('账号或密码不能为空')self.driver.back()def test_login_invalid_username_empty_password(self):"""测试用户名无效,密码为空时的登录行为"""self._login('123', '')self._verify_alert('账号或密码不能为空')self.driver.back()def test_login_valid_username_empty_password(self):"""测试用户名有效,密码为空时的登录行为"""self._login('123', '')self._verify_alert('账号或密码不能为空')self.driver.back()def test_login_valid_username_invalid_password(self):"""测试用户名有效,密码无效时的登录行为"""self._login('lisi', '123')self._verify_alert('密码错误')self.driver.back()def test_login_invalid_username_valid_password(self):"""测试用户名无效,密码有效时的登录行为"""self._login('123', '123456')self._verify_alert('用户不存在')self.driver.back()def test_login_invalid_username_invalid_password(self):"""测试用户名无效,密码无效时的登录行为"""self._login('123', '123')self._verify_alert('用户不存在')self.driver.back()def _verify_alert(self, expected_alert_text):"""验证弹出警告框的文本内容"""BlogDriver.getScreenShot()alert = WebDriverWait(self.driver,5).until(EC.alert_is_present())assert alert.text == expected_alert_textalert.accept()def test_logout_via_profile(self):"""测试通过个人资料页面登出"""self._login('zhangsan', '123456')self.driver.find_element(By.CSS_SELECTOR, '#submit').click()self.driver.get('http://8.137.19.140:9090/blog_list.html')WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'body > div.nav > a:nth-child(6)')))self.driver.find_element(By.CSS_SELECTOR,'body > div.nav > a:nth-child(6)').click()BlogDriver.getScreenShot()WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'body > div.container-login > div > h3')))self.driver.back()
结果演示:
3.用户注销自动化测试(BlogCancellation.py)
测试目标
-
确保用户注销功能正确,注销后无法访问受保护页面。
测试步骤
-
创建
BlogCancellation.py
,引入Utils.py
及相关依赖。 -
定义
BlogCancellation
类,管理注销测试用例。 -
实现以下测试方法:
-
注销功能测试:
-
登录后,点击注销按钮。
-
验证是否跳转到登录页面。
-
确保用户名和密码输入框为空。
-
-
未登录访问受保护页面:
-
访问博客管理页面。
-
确保系统跳转到登录页面。
-
-
-
在
RunTest.py
中调用BlogCancellation
进行测试。
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import BlogDriverclass BlogCancellation:url = ''driver = ''def __init__(self):"""初始化 WebDriver 并打开博客列表页"""self.url = 'http://8.137.19.140:9090/blog_list.html'self.driver = BlogDriver.driverself.driver.get(self.url)def test_logout_success(self):"""测试用户注销后是否正确跳转回登录页,并限制访问受保护页面"""# 点击注销按钮self.driver.find_element(By.CSS_SELECTOR, 'body > div.nav > a:nth-child(6)').click()BlogDriver.getScreenShot()# 等待页面跳转到登录页(10秒内出现登录标题)WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'body > div.container-login > div > h3')))# 确保用户名和密码框为空assert self.driver.find_element(By.CSS_SELECTOR,'#username').get_attribute('value') == ''assert self.driver.find_element(By.CSS_SELECTOR,'#password').get_attribute('value') == ''# 测试访问受保护的页面,确保被重定向到登录页protected_pages = ["http://8.137.19.140:9090/blog_list.html","http://8.137.19.140:9090/blog_detail.html?blogId=24428",]for page in protected_pages:self.driver.get(page)WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'body > div.container-login > div > h3')))# 额外检查:尝试编辑博客,确保未登录状态下不能提交self.driver.get("http://8.137.19.140:9090/blog_edit.html")try:self.driver.find_element(By.CSS_SELECTOR, '#title').send_keys('1')self.driver.find_element(By.CSS_SELECTOR, '#submit').click()WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'body > div.container-login > div > h3')))except:print("未登录用户无法提交博客,测试通过")
结果演示:
4. 博客列表页及博客数量自动化测试(BlogList.py)
测试目标
-
验证博客列表页是否正确加载博客内容,并确保未登录状态下无法访问博客列表。
测试步骤
-
创建
BlogList.py
,引入Utils.py
及相关依赖。 -
定义
BlogList
类,管理博客列表测试用例。 -
实现以下测试方法:
-
已登录状态:
-
登录后访问博客列表页面。
-
检查博客标题、内容、按钮等元素是否存在。
-
验证博客数量是否大于 0。
-
-
未登录状态:
-
直接访问博客列表页面。
-
系统应拒绝访问并返回错误提示。
-
-
-
在
RunTest.py
中调用BlogList
进行测试。
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from common.Utils import BlogDriverclass BlogList:"""博客列表页测试类"""url = ''driver = ''def __init__(self):"""初始化 WebDriver 并打开博客列表页"""self.url = 'http://8.137.19.140:9090/blog_list.html' # 博客列表页 URLself.driver = BlogDriver.driver # 获取 WebDriver 实例self.driver.get(self.url) # 打开博客列表页def test_account_name_display(self):"""测试用户名称是否正确显示"""BlogDriver.getScreenShot() # 截图用于调试user_name = self.driver.find_element(By.CSS_SELECTOR, 'body > div.container > div.left > div > h3').textassert user_name in ['zhangsan', 'lisi'], f"用户名不匹配,当前显示:{user_name}"def test_article_count_display(self):"""测试文章统计信息是否正确显示"""BlogDriver.getScreenShot()article_text = self.driver.find_element(By.CSS_SELECTOR, 'body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)').textassert article_text == '文章', f"页面上未找到'文章'文本,当前文本:{article_text}"def test_category_display(self):"""测试分类信息是否正确显示"""BlogDriver.getScreenShot()category_text = self.driver.find_element(By.CSS_SELECTOR, 'body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)').textassert category_text == '分类', f"页面上未找到'分类'文本,当前文本:{category_text}"def test_blog_title_exists(self):"""测试博客标题是否存在"""BlogDriver.getScreenShot()assert self.is_element_present(By.CSS_SELECTOR, 'body > div.container > div.right > div:nth-child(1) > div.title'), "博客标题未找到"def test_blog_date_exists(self):"""测试博客发布日期是否存在"""BlogDriver.getScreenShot()assert self.is_element_present(By.CSS_SELECTOR, 'body > div.container > div.right > div:nth-child(1) > div.date'), "博客发布日期未找到"def test_blog_content_exists(self):"""测试博客内容简介是否存在"""BlogDriver.getScreenShot()assert self.is_element_present(By.CSS_SELECTOR, 'body > div.container > div.right > div:nth-child(1) > div.desc'), "博客内容简介未找到"def test_blog_button_exists(self):"""测试博客详情按钮是否存在"""BlogDriver.getScreenShot()assert self.is_element_present(By.CSS_SELECTOR, 'body > div.container > div.right > div:nth-child(1) > a'), "博客详情按钮未找到"def test_navigation_to_blog_list(self):"""测试导航栏跳转到博客列表页"""self.driver.find_element(By.CSS_SELECTOR, 'body > div.nav > a:nth-child(4)').click()WebDriverWait(self.driver, 5).until(EC.title_is('博客列表页'))assert self.driver.title == '博客列表页', f"页面跳转失败,当前页面标题:{self.driver.title}"def test_navigation_to_blog_editor(self):"""测试导航栏跳转到博客编辑页"""self.driver.find_element(By.CSS_SELECTOR, 'body > div.nav > a:nth-child(5)').click()try:WebDriverWait(self.driver, 5).until(EC.title_is('博客编辑页'))except:passassert self.driver.title == '博客编辑页', f"页面跳转失败,当前页面标题:{self.driver.title}"def test_logout_redirect_to_login_page(self):"""测试退出账号是否跳转到登录页"""self.driver.find_element(By.CSS_SELECTOR, 'body > div.nav > a:nth-child(6)').click()try:WebDriverWait(self.driver, 5).until(EC.title_is('博客登录页'))except:passassert self.driver.title == '博客登陆页', f"退出登录失败,当前页面标题:{self.driver.title}"def is_element_present(self, by, selector):"""检查页面元素是否存在"""try:WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((by, selector)))return Trueexcept:return False
结果演示:
注意:未登录状态下访问博客列表可能导致找不到页面元素,应正确处理异常。
5. 博客详情页自动化测试(BlogDetail.py)
测试目标
-
确保博客详情页的功能正常,包括编辑和删除权限。
测试步骤
-
创建
BlogDetail.py
,引入Utils.py
及相关依赖。 -
定义
BlogDetail
类,管理博客详情测试用例。 -
实现以下测试方法:
-
当前用户可编辑和删除:
-
登录并访问自己的博客详情页。
-
确保编辑和删除按钮存在。
-
-
其他用户不可编辑或删除:
-
登录并访问其他用户的博客详情页。
-
确保页面上无编辑和删除按钮。
-
-
-
在
RunTest.py
中调用BlogDetail
进行测试。
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.alert import Alert
from common.Utils import BlogDriver
from selenium.webdriver.common.by import Byclass BlogDetail:url = ''driver = ''def __init__(self):"""初始化 WebDriver 并打开博客详情页"""self.url = 'http://8.137.19.140:9090/blog_detail.html?blogId=24713'self.driver = BlogDriver.driverself.driver.get(self.url)def test_current_user_can_edit(self):"""测试当前用户是否能编辑博客"""WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'body > div.container > div.right > div > div.operating > button:nth-child(1)')))self.driver.find_element(By.CSS_SELECTOR,'body > div.container > div.right > div > div.operating > button:nth-child(1)').click()BlogDriver.getScreenShot()# 确保页面跳转到博客编辑页assert self.driver.title == '博客编辑页', f"编辑页面标题错误:{self.driver.title}"def test_current_user_can_delete(self):"""测试当前用户是否能删除博客"""# 等待删除按钮可点击WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'body > div.container > div.right > div > div.operating > button:nth-child(2)')))self.driver.find_element(By.CSS_SELECTOR,'body > div.container > div.right > div > div.operating > button:nth-child(2)').click()# 等待弹出确认框alert = WebDriverWait(self.driver,5).until(EC.alert_is_present())assert alert.text == '确定删除?', f"弹窗提示不匹配:{alert.text}"alert.accept()BlogDriver.getScreenShot()# 等待页面跳转至博客列表页WebDriverWait(self.driver, 10).until(EC.title_is('博客列表页'))assert self.driver.title == '博客列表页', f"页面标题错误:{self.driver.title}"def test_other_users_cannot_edit_delete(self):"""测试其他用户无法编辑或删除博客"""edit_buttons = self.driver.find_elements(By.CSS_SELECTOR,'body > div.container > div.right > div > div.operating > button:nth-child(1)')delete_buttons = self.driver.find_elements(By.CSS_SELECTOR,'body > div.container > div.right > div > div.operating > button:nth-child(2)')BlogDriver.getScreenShot()# 确保没有编辑和删除按钮assert not edit_buttons, "其他用户能看到编辑按钮"assert not delete_buttons, "其他用户能看到删除按钮"
结果演示:
这里注意下,因为博客每天都会清空 ,所以
self.url = 'http://8.137.19.140:9090/blog_detail.html?blogId=24713'
要改成当天lisi发的一篇文章自动化测试才能正确运行
或者不嫌麻烦的话可以补完这句
def urlget():BlogDriver.driver.get('http://8.137.19.140:9090/blog_list.html')WebDriverWait(BlogDriver.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'body > div.container > div.right > div:nth-child(1) > a')))BlogDriver.driver.find_element(By.CSS_SELECTOR,'body > div.container > div.right > div:nth-child(1) > a').click()url = BlogDriver.driver.current_urlreturn url
把div:nth-child(1)的1改成循环到找到lisi发的文章就可以了,条件可以用找到编辑按钮?
WebDriverWait(BlogDriver.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'body > div.container > div.right > div:nth-child(1) > a')))
下面用
class BlogDetail:driver = ''def __init__(self):"""初始化 WebDriver 并打开博客详情页"""self.url = urlget()self.driver = BlogDriver.driverself.driver.get(self.url)
也能解决问题
6.博客编辑页自动化测试(BlogEdit.py)
测试目标
-
确保博客发布后页面能正确跳转。
测试步骤
-
创建
BlogEdit.py
,引入Utils.py
及相关依赖。 -
定义
BlogEdit
类,管理博客编辑测试用例。 -
实现以下测试方法:
-
博客发布测试:
-
登录并进入博客编辑页面。
-
输入标题、内容,点击提交。
-
确保页面跳转到博客列表。
-
-
-
在
RunTest.py
中调用BlogEdit
进行测试。
from common.Utils import BlogDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECclass BlogEdit:url = ''driver = ''def __init__(self):"""初始化WebDriver并打开博客编辑页面"""self.url = 'http://8.137.19.140:9090/blog_edit.html'self.driver = BlogDriver.driverself.driver.get(self.url)def test_publish_redirect_list(self):"""测试发布博客后是否跳转到博客列表页"""# 输入博客标题self.driver.find_element(By.CSS_SELECTOR, '#title').send_keys('789')# 提交博客self.driver.find_element(By.CSS_SELECTOR, '#submit').click()BlogDriver.getScreenShot()# 等待页面跳转到博客列表页,并验证跳转是否成功try:WebDriverWait(self.driver, 10).until(EC.title_is('博客列表页'))except:pass# 确保页面标题为'博客列表页'assert self.driver.title == '博客列表页', f"页面标题错误,实际为:{self.driver.title}"
结果演示:
7. 测试执行入口(RunCases.py)
测试目标
-
统一管理测试脚本的执行。
测试步骤
-
创建
RunCases.py
,引入所有测试脚本。 -
依次调用各测试类的测试方法。
-
运行测试并记录测试结果。
import time
from common.Utils import BlogDriver
from cases import BlogLogin, BlogList, BlogEdit, BlogDetail, BlogCancellationdef run_blog_login_tests():"""运行登录模块的所有测试"""print("登录模块测试中...")BlogLogin.BlogLogin().loginSucTest1() # 测试用户名 zhangsan 的登录BlogLogin.BlogLogin().loginSucTest2() # 测试用户名 lisi 的登录BlogLogin.BlogLogin().test_login_empty_username_empty_password() # 测试用户名和密码都为空BlogLogin.BlogLogin().test_login_empty_username_valid_password() # 测试用户名为空,密码有效BlogLogin.BlogLogin().test_login_invalid_username_invalid_password() # 测试用户名和密码无效def run_blog_list_tests():"""运行博客列表页的所有测试"""print("博客列表页测试中...")BlogList.BlogList().test_account_name_display() # 测试用户名是否正确显示BlogList.BlogList().test_article_count_display() # 测试文章数是否正确显示BlogList.BlogList().test_category_display() # 测试分类信息是否正确显示BlogList.BlogList().test_blog_title_exists() # 测试博客标题是否存在BlogList.BlogList().test_blog_date_exists() # 测试博客发布日期是否存在BlogList.BlogList().test_blog_content_exists() # 测试博客内容简介是否存在BlogList.BlogList().test_blog_button_exists() # 测试博客详情按钮是否存在BlogList.BlogList().test_navigation_to_blog_list() # 测试博客列表跳转BlogList.BlogList().test_navigation_to_blog_editor() # 测试博客编辑页跳转BlogList.BlogList().test_logout_redirect_to_login_page() # 测试登出后是否跳转到登录页def run_blog_edit_tests():"""运行博客编辑页的所有测试"""print("博客编辑页测试中...")BlogEdit.BlogEdit().test_publish_redirect_list() # 测试发布后是否跳转到博客列表页def run_blog_detail_tests():"""运行博客详情页的所有测试"""print("博客详情页测试中...")BlogDetail.BlogDetail().test_current_user_can_edit() # 测试当前用户是否能编辑博客BlogDetail.BlogDetail().test_current_user_can_delete() # 测试当前用户是否能删除博客BlogDetail.BlogDetail().test_other_users_cannot_edit_delete() # 测试其他用户是否能编辑或删除博客def run_blog_cancellation_tests():"""运行博客注销的所有测试"""print("博客注销模块测试中...")BlogCancellation.BlogCancellation().test_logout_success() # 测试注销是否成功并限制访问def run_all_tests():"""运行所有测试"""run_blog_login_tests() # 运行登录相关测试run_blog_list_tests() # 运行博客列表相关测试BlogLogin.BlogLogin().loginSucTest1() # 重新登录run_blog_edit_tests() # 运行博客编辑相关测试run_blog_detail_tests() # 运行博客详情页相关测试run_blog_cancellation_tests() # 运行博客注销相关测试print("测试已成功通过!",end='')if __name__ == "__main__":try:run_all_tests()except Exception as e:print(f"错误发生: {e}")finally:BlogDriver.driver.quit() # 结束所有测试后关闭浏览器
全部演示一遍:
3.各函数作用总览
BlogCancellation.py
__init__(self)
: 初始化 WebDriver 并打开博客列表页。test_logout_success(self)
: 测试用户注销后是否正确跳转回登录页,并限制访问受保护页面。
RunCases.py
- 运行函数,测试所有函数是否正确执行。
Utils.py
__init__(self)
: 初始化 WebDriver。getScreenShot(self)
: 用于保存截图的函数。
BlogDetail.py
__init__(self)
: 初始化 WebDriver 并打开博客详情页。test_current_user_can_edit(self)
: 测试当前用户是否能编辑博客。test_current_user_can_delete(self)
: 测试当前用户是否能删除博客。test_other_users_cannot_edit_delete(self)
: 测试其他用户无法编辑或删除博客。
BlogEdit.py
__init__(self)
: 初始化 WebDriver 并打开博客编辑页面。test_publish_redirect_list(self)
: 测试发布博客后是否跳转到博客列表页。
BlogList.py
__init__(self)
: 初始化 WebDriver 并打开博客列表页。test_account_name_display(self)
: 测试用户名称是否正确显示。test_article_count_display(self)
: 测试文章统计信息是否正确显示。test_category_display(self)
: 测试分类信息是否正确显示。test_blog_title_exists(self)
: 测试博客标题是否存在。test_blog_date_exists(self)
: 测试博客发布日期是否存在。test_blog_content_exists(self)
: 测试博客内容简介是否存在。test_blog_button_exists(self)
: 测试博客详情按钮是否存在。test_navigation_to_blog_list(self)
: 测试导航栏跳转到博客列表页。test_navigation_to_blog_editor(self)
: 测试导航栏跳转到博客编辑页。test_logout_redirect_to_login_page(self)
: 测试退出账号是否跳转到登录页。
BlogLogin.py
__init__(self)
: 初始化登录页面并打开指定URL。loginSucTest1(self)
: 测试用户 'zhangsan' 的登录成功。loginSucTest2(self)
: 测试用户 'lisi' 的登录成功。_login(self, username, password)
: 登录方法,接收用户名和密码。_verify_login_success(self)
: 验证用户是否成功登录。test_login_empty_username_empty_password(self)
: 测试用户名和密码为空时的登录行为。test_login_empty_username_valid_password(self)
: 测试用户名为空,密码有效时的登录行为。test_login_empty_username_invalid_password(self)
: 测试用户名为空,密码无效时的登录行为。test_login_invalid_username_empty_password(self)
: 测试用户名无效,密码为空时的登录行为。test_login_valid_username_empty_password(self)
: 测试用户名有效,密码为空时的登录行为。test_login_valid_username_invalid_password(self)
: 测试用户名有效,密码无效时的登录行为。test_login_invalid_username_valid_password(self)
: 测试用户名无效,密码有效时的登录行为。test_login_invalid_username_invalid_password(self)
: 测试用户名无效,密码无效时的登录行为。_verify_alert(self, expected_alert_text)
: 验证弹出警告框的文本内容。test_logout_via_profile(self)
: 测试通过个人资料页面登出。
4.测试环境与执行方式
运行环境
-
Python 版本:3.13
-
依赖库:
selenium
-
浏览器:Microsoft Edge 版本 134.0.3124.68 (正式版本) (64 位)
-
WebDriver:
msedgedriver
需正确配置
运行方式
-
安装
selenium
依赖:pip install selenium
-
确保
msedgedriver
已正确安装。 -
运行
RunCases.py
以执行完整的测试流程:python RunCases.py
结果验证
-
所有测试案例执行完成后,测试日志将会记录各个测试步骤的执行结果。
-
关键页面截图将在
images/
目录中保存,以供参考。 -
若有测试失败,请参考日志进行问题排查,并根据错误信息调整代码或系统设置。
5.自动化测试的特点与亮点
特点:该自动化测试框架结合了Selenium WebDriver与自定义的功能模块,能够有效地进行博客网站的功能验证。通过编写多种测试用例,涵盖了从登录、发布博客到页面跳转等各个功能场景。每个测试用例都封装为独立的函数,测试目标明确,执行步骤清晰,能够快速验证网站功能是否正常工作。自动化脚本可以反复执行,提高了测试的覆盖率和效率,同时能够通过截图等方式记录测试过程中的异常情况。通过使用Selenium WebDriver的等待机制,可以确保页面元素完全加载后再进行操作,从而减少因页面加载问题而引起的测试失败。
亮点:
-
驱动复用:在测试中采用了统一的
BlogDriver
类来创建WebDriver实例,避免了每个测试用例中重复创建WebDriver对象的问题,提升了测试效率和性能。 -
功能清晰的测试用例:每个测试用例封装成一个独立的函数,清晰地定义了测试的目标和验证条件,增强了代码的可读性和可维护性。
-
完善的等待机制:通过结合显式等待和隐式等待,解决了由于页面渲染速度与测试执行速度不一致的问题,确保了每个操作都在页面元素完全加载后执行,提高了自动化测试的稳定性和效率。
-
截图功能:在每个测试用例中都加入了截图功能,能够在测试执行过程中自动保存截图,这有助于在测试失败时快速定位问题,提高了调试效率。
-
自动化的重复执行:该框架能够轻松地重复执行测试用例,帮助在不同的时间段或多次执行中检查系统的稳定性和可靠性。
-
页面元素检查:通过对页面元素的存在性和可见性进行验证,确保用户交互的每个元素都能够正常显示,避免了因页面渲染错误导致的功能不完整问题。
6.完整测试代码连接
GitHub - bbppooii/Personal-Blog-System-Automated-Testing: 个人博客系统测试报告
bbppooi/个人博客系统自动化测试 - Gitee.com