项目介绍
项目简介
测试个人博客系统的主要功能。目的是为了对个人博客系统的主要功能:登录页面、列表页面、详情页面、编辑页面相关功能进行自动化测试。旨在提高测试效率与质量。
测试地址
http://8.137.19.140:9090/blog_login.html
项目设计
1.python编程语言设计测试脚本
2.webdriver驱动浏览器并操作页面元素
3.二次封装webdriver 操作方法
4.通过第三方插件pytest-html生成测试报告
设计测试用例
环境准备与使用工具
工具:
1、windows系统
2、Chrome浏览器(版本:128.0.6613.138)
3、PyCharm
环境:
1、Python 3.11
2、配置安装:Selenium 4.24.0 (pip install selenium)
3、配置安装:Pytest 8.3.3 (pip install pytest)
4、配置安装:Pytest-html 4.1.1 (pip install pytest-html)
5、配置安装:webdriver-manager (pip install webdriver-manager)
目录结构
NewAutoBlogTest
-common
--head.py
--_init_.py
-tests
--BlogLogin_test.py
--BlogList_test.py
--BlogDetail_test.py
--BlogEdit_test.py
--_init_.py
代码实现
common包下的创建Chrome浏览器web驱动对象并进行封装
from webbrowser import Chrome
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Driver:
driver=""
def __init__(self):
self.driver=webdriver.Chrome(service=Service(ChromeDriverManager().install()))
WebDriver=Driver()
tests包下的页面功能测试
登录页面
账号密码都正确
from time import sleep
import pytest
from selenium.webdriver.common.by import By
from common.head import Driver, WebDriver
class TestBlogLogin:
driver=""
url=""
@classmethod
def setup_class(cls):
cls.driver = WebDriver.driver
cls.url="http://8.137.19.140:9090/blog_login.html"
cls.driver.get(cls.url)
# 账号密码都正确
def test_LoginSuc(self):
self.driver.maximize_window()
self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
sleep(2)
self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
sleep(2)
assert self.driver.current_url=="http://8.137.19.140:9090/blog_list.html"
账号正确,密码不正确
def test_LoginDef01(self):
self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(6)").click()
self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
sleep(2)
self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")
self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
sleep(3)
alert = self.driver.switch_to.alert
# print(alert.text)
assert alert.text=="密码错误"
alert.accept()
sleep(3)
账号不正确,密码正确
def test_LoginDef02(self):
self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
sleep(2)
self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangn")
self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
sleep(2)
alert=self.driver.switch_to.alert
# print(alert.text)
assert alert.text == "用户不存在"
alert.accept()
sleep(2)
账号不正确,密码不正确
def test_LoginDef03(self):
self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
sleep(2)
self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangn")
self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("13465")
self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
sleep(2)
alert = self.driver.switch_to.alert
# print(alert.text)
assert alert.text == "用户不存在"
alert.accept()
sleep(2)
账号密码都为空
def test_LoginDef04(self):
self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
sleep(2)
self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
sleep(2)
alert = self.driver.switch_to.alert
# print(alert.text)
assert alert.text=="账号或密码不能为空"
alert.accept()
sleep(2)
列表页功能
登录状态查找信息
from time import sleep
import pytest
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
# 导入类
from tests.BlogLogin_test import TestBlogLogin
from common.head import Driver, WebDriver
class TestBlogList:
driver=""
url=""
@classmethod
def setup_class(cls):
cls.driver=WebDriver.driver
cls.url="http://8.137.19.140:9090/blog_login.html"
cls.driver.get(cls.url)
def test_LoginSuc(self):
self.driver.maximize_window()
self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
sleep(2)
self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
sleep(2)
# BlogDriver.cutpicture()
assert self.driver.current_url=="http://8.137.19.140:9090/blog_list.html"
def test_LoginSucList(self):
self.driver.maximize_window()
# 查找昵称
self.driver.implicitly_wait(3)
self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
txt=self.driver.find_element(By.XPATH,"//a[text()='GitHub 地址']").text
print(txt)
# 查找博客题名为1的文章
self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title")
assert self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > span").text=="我的博客系统"
# 查找注销
self.driver.find_element(By.CSS_SELECTOR," body > div.nav > a:nth-child(6)")
未登录状态
def test_LoginDefList(self):
self.driver.get("http://8.137.19.140:9090/blog_list.html")
wait=WebDriverWait(self.driver,10)
wait.until(expected_conditions.alert_is_present())
登录状态详情页展示
查找个人信息、文章标题等
from time import sleep
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from common.head import WebDriver, WebDriver
class TestBlogDetail:
driver=""
url=""
@classmethod
def setup_class(cls):
cls.driver=WebDriver.driver
cls.url="http://8.137.19.140:9090/blog_login.html"
cls.driver.get(cls.url)
def test_LoginSuc(self):
self.driver.maximize_window()
self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
sleep(2)
self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
sleep(2)
assert self.driver.current_url=="http://8.137.19.140:9090/blog_list.html"
#
def test_LoginSucDetail(self):
# 以标题为博客系统自动化测试的文章为例
# 博客系统自动化测试的文章名
self.driver.get("http://8.137.19.140:9090/blog_detail.html?blogId=3995")
wait=WebDriverWait(self.driver,10)
txt1=wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"body > div.container > div.right > div > div.title"))).text
print(txt1)
# 文章发表时间
sleep(3)
txt2=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.date").text
print(txt2)
sleep(3)
txt3=self.driver.find_element(By.CSS_SELECTOR,"#detail").text
print(txt3)
sleep(2)
未登录状态
def test_LoginDefDetail(self):
self.driver.get("http://8.137.19.140:9090/blog_detail.html?blogId=3995")
wait = WebDriverWait(self.driver, 10)
wait.until(EC.alert_is_present())
编辑页功能
登陆状态写博客–从链接打开编辑页面
from time import sleep
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from common.head import WebDriver
class TestBlogEdit:
driver=""
url=""
@classmethod
def setup_class(cls):
cls.driver = WebDriver.driver
cls.url = "http://8.137.19.140:9090/blog_login.html"
cls.driver.get(cls.url)
def test_LoginSuc(self):
self.driver.maximize_window()
self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
sleep(2)
self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
sleep(2)
assert self.driver.current_url == "http://8.137.19.140:9090/blog_list.html"
# 登录状态--从链接打开编辑页面 问题:标题相同的依旧可以发布
def test_LoginSucEdit01(self):
# self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
self.driver.get("http://8.137.19.140:9090/blog_edit.html")
self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys("陕西西安")
sleep(5)
self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
sleep(2)
登录状态–从例表页打开编辑页面
def test_LoginSucEdit02(self):
self.driver.get("http://8.137.19.140:9090/blog_list.html")
self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
sleep(1)
self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys("南昌")
self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
sleep(3)
登录状态–从例表页打开编辑页面
def test_LoginSucEdit03(self):
self.driver.get("http://8.137.19.140:9090/blog_detail.html?blogId=3995")
#点击-写博客
self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)").click()
sleep(1)
self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("江西南昌")
self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
sleep(3)
登录状态–从链接打开编辑页面–不写标题
def test_LoginSucEdit04(self):
self.driver.get("http://8.137.19.140:9090/blog_edit.html")
self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
sleep(2)
alert=self.driver.switch_to.alert
alert.accept()
sleep(3)
未登录状态
def test_LoginDefEdit(self):
self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(6)").click()
# 点击写博客
# 是否出现弹窗
wait=WebDriverWait(self.driver,5)
wait.until(expected_conditions.alert_is_present())
测试输出报告
自动生成html格式报告,其中报告里面附带用例执行日志明细
通过pytest --html=report.html
自动生成测试报告
共有三个执行用例未通过,查看得知,问题是均未出现弹窗而引发错误。
此外,还发现发布文章时,文章标题相同的依旧可以发布。
可联系开发人员添加对应功能和修改。