- 1)項目背景
- 2)項目功能介紹
- 登陸
- 寫博客/編輯已存在博客
- 刪除博客
- 注銷
- 2)基于項目功能設計相關測試用例
- 3)基于測試用例編寫自動化測試
- 準備工作
- 登陸界面相關
- 博客首頁相關
- 博客詳情頁相關
- 編輯博客相關
- 刪除博客相關
- 注銷相關
- 4)基于個人博客系統的性能測試
- 準備工作
- 性能測試報告
項目背景
隨著自媒體行業快速發展,個人創作者對博客發布平臺的需求日益多元化,越來越多的用戶需要用到博客來記錄學習的過程,以及把個人的心得寫成技術博客分享出來供大家學習和參考,又或者把博客當成自己學習中的日記,總之博客對于現在的互聯網用戶來說,博客是一個于學習、參考、記錄方面不可或缺的平臺,本項目簡單來說就是博客平臺的精簡版。
1.本項目個人博客系統采用前后端分離的方法來實現,同時使用了數據庫來存儲相關的數據,同時將其部署到云服務器上。前端主要有四個頁面構成:登錄頁、博客首頁、博客詳情頁以及博客編輯頁,以上模擬實現了最精簡的個人博客系統。
2.該個人博客系統主要實現個人用戶簡單的博客記錄,時間、標題、內容以及發布者等都可以進行詳細地查看。
項目功能介紹
該個人博客系統主要實現了以下幾個功能:登錄、寫博客/編輯已存在博客、刪除博客、注銷等功能
登陸
寫博客/編輯已存在博客
刪除博客
注銷
基于項目功能設計相關測試用例
基于測試用例編寫自動化測試
該項目由Selenium庫進行測試web自動化測試,使用語言為Python。
準備工作
Selenium是廣泛使用的模擬瀏覽器運行的庫,它是一個用于Web應用程序測試的工具。 Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣,并且支持大多數現代 Web 瀏覽器。
先安裝好Python以及IDE(Pycharm),打開Pycharm軟件,在終端中使用pip命令安裝瀏覽器驅動管理,命令為pip insatll webdriver-manager
,安裝好之后繼續安裝Selenium庫,命令為pip insatll selenium==4.0.0
創建并初始化瀏覽器驅動,因為在運行每個自動化測試用例之前都需要進行創建瀏覽器驅動,運行所有的測試方法結束之后需要釋放瀏覽器驅動,所以創建一個類來初始化瀏覽器驅動和釋放瀏覽器驅動,這個類用單例模式,只提供一個對象供其他測試方法使用,另外寫一個截圖方法來實時查看測試中出現的情況
import sys
import os.path
from datetime import datetimefrom selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager# 創建一個瀏覽器對象
class Driver:driver = ""def __init__(self):options = Options()self.driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()),options=options)self.driver.implicitly_wait(7)def GetScreenShot(self):# 創建屏幕截圖# 截圖的路徑:../images/2025-07-11/2025-07-11-140735.pngdirname = datetime.now().strftime("%Y-%m-%d")# 如果不存在當天的截圖存放路徑文件就創建if not os.path.exists("../images/"+dirname):os.mkdir("../images/"+dirname)# 截圖的名稱:2025-07-11-140735.pngfilename = sys._getframe().f_back.f_code.co_name+"-"+datetime.now().strftime("%Y-%m-%d-%H%M%S"+".png")self.driver.save_screenshot("../images/"+dirname+"/"+filename)#單例模式創建一個驅動對象供所有測試使用
BlogDriver = Driver()
登陸界面相關
import time
from selenium.webdriver.common.by import By
from Common.Utils import BlogDriver#博客登陸測試
class BlogLogin:driver = ""url = ""def __init__(self):self.driver = BlogDriver.driverself.url = "http://8.137.19.140:9090/blog_login.html"self.driver.get(self.url)#正確登陸的測試用例#輸入正確的賬號和密碼def LoginSucTest(self):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("lisi")self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")self.driver.find_element(By.CSS_SELECTOR,"#submit").click()time.sleep(3)#登陸后頁面能找到昵稱就是成功登陸,否則登陸失敗self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# #返回登陸頁面# self.driver.back()# time.sleep(5)# self.driver.quit()# 異常登陸的測試案例def LoginFailTest(self):# 輸入正確的賬號和錯誤的密碼#連續多次send_keys會出現關鍵詞拼接的現象,而不是直接替換,如果想重新在輸入框中輸入內容,需要先清空該輸入框self.driver.find_element(By.CSS_SELECTOR,"#username").clear()self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("lisi")#輸入錯誤的密碼self.driver.find_element(By.CSS_SELECTOR,"#password").clear()self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")self.driver.find_element(By.CSS_SELECTOR, "#submit").click()#出現警告彈窗 賬號或密碼錯誤time.sleep(1)alert = self.driver.switch_to.alertalert_text = alert.text #保存彈窗文本信息以此來判斷是否登陸成功alert.accept()# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()#檢查是否符合預期assert alert_text == "密碼錯誤"# 返回登陸頁面,再測試下一種情況# 輸入錯誤的賬號和正確的密碼self.driver.find_element(By.CSS_SELECTOR,"#username").clear()self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("wangwu")#輸入正確的密碼self.driver.find_element(By.CSS_SELECTOR,"#password").clear()self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")self.driver.find_element(By.CSS_SELECTOR, "#submit").click()#出現警告彈窗 賬號或密碼錯誤time.sleep(1)alert = self.driver.switch_to.alertalert_text = alert.text # 保存彈窗文本信息以此來判斷是否登陸成功alert.accept()# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 檢查是否符合預期assert alert_text == "用戶不存在"# 輸入錯誤的賬號和錯誤的密碼self.driver.find_element(By.CSS_SELECTOR, "#username").clear()self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wangwu")# 輸入錯誤的密碼self.driver.find_element(By.CSS_SELECTOR, "#password").clear()self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")self.driver.find_element(By.CSS_SELECTOR, "#submit").click()# 出現警告彈窗 賬號或密碼錯誤time.sleep(1)alert = self.driver.switch_to.alertalert_text = alert.text #保存彈窗文本信息以此來判斷是否登陸成功alert.accept()# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 檢查是否符合預期assert alert_text == "用戶不存在"# 賬號為空,密碼隨便寫# 賬號隨便寫,密碼為空# 兩種情況都是一樣的,合并為一個測試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, "#password").send_keys("123")self.driver.find_element(By.CSS_SELECTOR, "#submit").click()# 出現警告彈窗 賬號或密碼為空time.sleep(1)alert = self.driver.switch_to.alertalert_text = alert.text #保存彈窗文本信息以此來判斷是否登陸成功alert.accept()# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 檢查是否符合預期assert alert_text == "賬號或密碼不能為空"# 賬號和密碼都為空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, "#submit").click()# 出現警告彈窗 賬號或密碼為空time.sleep(1)alert = self.driver.switch_to.alertalert_text = alert.text #保存彈窗文本信息以此來判斷是否登陸成功alert.accept()# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 檢查是否符合預期assert alert_text == "賬號或密碼不能為空"# self.driver.quit()# login = BlogLogin()
# login.LoginFailTest()
# login.LoginSucTest()
博客首頁相關
import time
from selenium.webdriver.common.by import By
from Common.Utils import BlogDriver# 博客列表測試
class BlogList:driver = ""url = ""def __init__(self):self.driver=BlogDriver.driverself.url = "http://8.137.19.140:9090/blog_list.html"self.driver.get(self.url)#測試首頁---登陸狀態下def ListTestByLogin(self):# 測試個人信息# 頭像、昵稱、文章、分類是否存在#檢查頭像是否存在self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > img")#檢查昵稱是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")#檢查github地址是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > a")#檢查文章是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")#檢查分類是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")# 測試博客列表#檢查博客標題是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(3) > div.title")#檢查博客發布時間是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(3) > div.date")#檢查博客內容是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(3) > div.desc")#檢查"查看全文"按鈕是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(3) > a")# 測試菜單欄#檢查主頁按鈕是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(4)")#檢查寫博客按鈕是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)")#檢查注銷按鈕是否存在self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)")# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# BlogDriver.driver.quit()# 測試首頁---未登陸狀態下# 未登錄狀態下打開博客首頁會跳轉到登陸界面# 根據網頁來判斷def ListTestByUnlogin(self):self.driver.back()# 訪問博客首頁urlself.driver.get(self.url)time.sleep(2)# 獲取當前url,看看是否跳轉到了登錄頁cur_url = self.driver.current_urlprint(cur_url)# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 檢查是否未登錄assert cur_url == "http://8.137.19.140:9090/blog_list.html"# time.sleep(3)# self.driver.quit()# BlogList=BlogList()
# BlogList.ListTestByLogin()
# BlogList.ListTestByUnlogin()
博客詳情頁相關
import time
from selenium.webdriver.common.by import By
from Common.Utils import BlogDriver# 博客詳情頁測試
class BlogDetail:driver = ""url = ""def __init__(self):self.driver=BlogDriver.driverself.url = "http://8.137.19.140:9090/blog_detail.html?blogId=116661"self.driver.get(self.url)# 博客詳情頁測試 --- 登陸狀態下def DetailTestByLogin(self):# 檢查博客標題self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.title")# 檢查博客發布時間self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.date")# 檢查博客內容self.driver.find_element(By.CSS_SELECTOR,"#detail > p")# 檢查編輯博客按鈕是否存在self.driver.find_element(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(2)")# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 博客詳情頁測試 --- 未登陸狀態下# 直接跳轉到登陸界面def DetailTestByUnLogin(self):# 訪問博客詳情頁urlself.driver.get(self.url)time.sleep(2)# 獲取當前url,看看是否跳轉到了登錄頁cur_url = self.driver.current_urlprint(cur_url)# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 檢查是否未登錄assert cur_url == "http://8.137.19.140:9090/blog_detail.html?blogId=116646"# BlogDetail = BlogDetail()
# BlogDetail.DetailTestByLogin()
# BlogDetail.DetailTestByUnLogin()
編輯博客相關
import time
from selenium.webdriver.common.by import By
from Common.Utils import BlogDriver# 博客編輯頁測試
class BlogEdit:driver = ""url = ""def __init__(self):self.driver=BlogDriver.driverself.url = "http://8.137.19.140:9090/blog_edit.html"self.driver.get(self.url)#博客編輯頁測試 --- 登陸狀態下正常發布博客def NormalEditTestByLogin(self):# self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)").click()# 檢查博客標題欄,并填寫標題self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys("這是一個測試")# 檢查博客輸入內容區域# 找到編輯區域,輸入博客內容(編輯區域不可操作)# 菜單欄元素無法定位# 博客系統的編輯內容區域默認就不為空,可以暫時不做處理# 直接點擊發布按鈕發布博客self.driver.find_element(By.CSS_SELECTOR, "#submit").click()# 檢查已發布博客是否存在博客首頁blog_title= self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(4) > div.title").textprint(blog_title)assert blog_title == "這是一個測試"# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()#博客編輯頁測試 --- 登陸狀態下異常發布博客def NotNormalEditTestByLogin(self):# 博客標題和內容都不輸入,直接點擊發布按鈕發布博客self.driver.find_element(By.CSS_SELECTOR, "#submit").click()# 出現警告彈窗無法提交,頁面依然是博客編輯頁面time.sleep(2)alert = self.driver.switch_to.alertalert.accept()# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 獲取當前頁面url,判斷是否還在博客編輯頁面cur_url = self.driver.current_urlassert cur_url == self.url#博客編輯頁測試 --- 未登陸狀態下正常發布博客# 點擊提交按鈕會直接跳轉到登陸頁面def NormalEditTestByUnLogin(self):# 檢查博客標題欄,并填寫標題self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("這是一個測試2")# 檢查博客輸入內容區域# 找到編輯區域,輸入博客內容(編輯區域不可操作)# 菜單欄元素無法定位# 博客系統的編輯內容區域默認就不為空,可以暫時不做處理# 直接點擊發布按鈕發布博客self.driver.find_element(By.CSS_SELECTOR, "#submit").click()time.sleep(2)# 獲取當前頁面url,判斷是否會直接跳轉到博客登陸頁面# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()cur_url = self.driver.current_urlprint(cur_url)assert cur_url == "http://8.137.19.140:9090/blog_login.html"#博客編輯頁測試 --- 未登陸狀態下異常發布博客# 點擊提交按鈕也是會直接跳轉到登陸頁面def NotNormalEditTestByUnLogin(self):# 博客標題和內容都不輸入,直接點擊發布按鈕發布博客self.driver.find_element(By.CSS_SELECTOR, "#submit").click()time.sleep(2)# 獲取當前頁面url,判斷是否會直接跳轉到博客登陸頁面# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()cur_url = self.driver.current_urlprint(cur_url)assert cur_url == "http://8.137.19.140:9090/blog_login.html"# BlogEdit = BlogEdit()
# BlogEdit.NormalEditTestByLogin()
# BlogEdit.NotNormalEditTestByLogin()
刪除博客相關
import time
from selenium.webdriver.common.by import By
from Common.Utils import BlogDriver# 刪除博客測試
class BlogDel:driver = ""url = ""def __init__(self):self.driver=BlogDriver.driverself.url = "http://8.137.19.140:9090/blog_detail.html?blogId=116665"self.driver.get(self.url)# 刪除博客測試 --- 登陸狀態下def BlogDelByLogin(self):# 檢查博客標題self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.title")# 檢查博客發布時間self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.date")# 檢查博客內容self.driver.find_element(By.CSS_SELECTOR,"#h2-u5728u8FD9u91CCu5199u4E0Bu4E00u7BC7u535Au5BA2")# 檢查編輯博客按鈕是否存在self.driver.find_element(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(2)")# 找到刪除按鈕 點擊刪除后,出現警告彈窗,確認刪除后,返回博客列表self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()alert = self.driver.switch_to.alert# 取消刪除,頁面依然是博客編輯頁面alert.dismiss()# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 獲取當前頁面urlcur_url1 = self.driver.current_urlassert cur_url1 == self.url# 確認刪除,頁面跳轉為博客列表頁self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()alert = self.driver.switch_to.alertalert.accept()# 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 獲取當前頁面urlcur_url2 = self.driver.current_urlassert cur_url2 == self.urlprint("123")# 刪除博客測試 --- 未登陸狀態下# 直接跳轉到登陸界面def BlogDelByUnLogin(self):time.sleep(2)# 獲取當前頁面urlcur_url = self.driver.current_urlprint(cur_url)# 檢查是否跳轉到登陸頁面assert cur_url == "http://8.137.19.140:9090/blog_login.html"# BlogDel = BlogDel()
# BlogDel.BlogDelByLogin()
注銷相關
import timefrom selenium.webdriver.common.by import Byfrom Common.Utils import BlogDriver# 注銷按鈕測試
class ExitBlog:driver = ""url = ""def __init__(self):self.driver=BlogDriver.driverself.url = "http://8.137.19.140:9090/blog_list.html"self.driver.get(self.url)# 對注銷按鈕進行測試 --- 登陸狀態下def ExitBlogTest(self):# 找到注銷按鈕并點擊self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(6)").click()time.sleep(2)# # 添加屏幕截圖來實時查看當時的場景BlogDriver.GetScreenShot()# 獲取當前頁面urlcur_url = self.driver.current_urlprint(cur_url)# 判斷是否跳轉到登陸頁面assert cur_url == "http://8.137.19.140:9090/blog_login.html"# ExitBlog = ExitBlog()
# ExitBlog.ExitBlogTest()
另外創建一個文件專門用來執行腳本、以及瀏覽器驅動的退出
from Common.Utils import BlogDriver
from Tests.BlogDel import BlogDel
from Tests.BlogDetail import BlogDetail
from Tests.BlogEdit import BlogEdit
from Tests.BlogList import BlogList
from Tests.BlogLogin import BlogLogin
from Tests.ExitBlog import ExitBlogif __name__ == "__main__":BlogLogin().LoginFailTest()# BlogLogin().LoginSucTest()# 登陸成功狀態下,進行博客首頁的測試# BlogList().ListTestByLogin()# 未登陸狀態下,進行博客首頁的測試# BlogList().ListTestByUnlogin()# 登陸成功狀態下,進行博客詳情頁的測試# BlogDetail().DetailTestByLogin()# 未登陸狀態下,進行博客詳情頁的測試# BlogDetail().DetailTestByUnLogin()# 登陸成功狀態下,進行博客編輯頁正常發布的測試# BlogEdit().NormalEditTestByLogin()# 登陸成功狀態下,進行博客編輯頁異常發布的測試# BlogEdit().NotNormalEditTestByLogin()# 未登陸狀態下,進行博客編輯頁正常發布的測試# BlogEdit().NormalEditTestByUnLogin()# 未登陸狀態下,進行博客編輯頁異常發布的測試# BlogEdit().NotNormalEditTestByUnLogin()# 登陸成功狀態下,進行博客刪除頁面的測試包含(取消刪除和確認刪除)# BlogDel().BlogDelByLogin()# 未登陸狀態下,進行博客刪除頁面測試會直接跳轉到登陸頁面# BlogDel().BlogDelByUnLogin()# 登陸成功狀態下,測試點擊注銷按鈕后是否會跳轉到登陸頁面# ExitBlog().ExitBlogTest()# 最后程序統一退出BlogDriver.driver.quit()
基于個人博客系統的性能測試
性能測試的目的是確保軟件系統在實際運行中具備良好的響應速度、穩定性、可擴展性和資源利用率,從而滿足業務需求和用戶期望,支撐系統的長期穩定運行。
準備工作
安裝好Java以及jmeter工具,jmeter需要下載插件功能,下載網址為https://jmeter-plugins.org/install/Install/
,下載好之后,進入jmeter安裝Custom Thread Groups和Page DataExtractor插件。
性能測試報告
JMeter測試報告是?個全??詳細的?檔,它提供了關于測試執?結果的詳細信息,幫助??全?評 估系統的性能并進?性能優化。
?成性能測試報告命令:在終端執行命令 Jmeter -n -t 腳本?件 -l ?志?件 -e -o ?錄
-n : ?圖形化運?
-t : 被運?的腳本
-l : 將運?信息寫??志?件,后綴為 jtl 的?志?件
-e : ?成測試報告
-o : 指定報告輸出?錄
本項目性能測試報告如下:
性能分析主要幾個方面:
1)響應時間:如果相應時間超過了要求,代表性能到達了瓶頸
2)錯誤率:檢查高并發場景下,系統是否能夠正常處理業務
3)吞吐量:吞吐量越?,性能越好;吞吐量相對穩定或者變低,可能系統達到了性能瓶頸