文章目录
📎 前言
对自己所编写的博客系统项目进行软件测试活动,总结测试过程中的成功经验与不足,以便于以后更好的进行测试工作。旨在学习记录,希望也可以帮到需要的人,望指正。
一、项目背景
1.1 项目名称
文墨飞扬(博客系统)
1.2 测试时间
2024.06 - 2024.06
1.3 项目简介
- 该项目采用前后端分离的方法来实现,同时使用了数据库来存储相关的数据,并将其部署到云服务器上。前端主要有四个页面构成:登录页、列表页、详情页以及编辑页,以上模拟实现了最简单的个人博客系统。其结合后端实现了以下的主要功能:登录、编辑博客、注销、删除博客、以及强制登录等功能。
- 该项目没有设计用户注册功能,只能提前在数据库中存储用户信息后经过校验登录。
- 个人博客系统可以实现个人用户简单的博客记录,时间、标题、内容以及发布者等都可以进行详细地查看。
二、测试计划
2.1测试环境与相关配置
服务器:云服务器 CentOS 7.9
PC机:Dell G15 5511
平台:Windows 10 家庭版
浏览器:Chrome、FireFox、Edge
自动化测试工具:Selenium + Junit5
2.2 功能测试
2.2.1 测试用例
2.2.2 测试步骤
以下为实际执行测试时的一些截图
- 正常登录(输入正确的账号密码)
- 写博客测试
- 发布成功后查看详情页
- 删除新发布的博客
删除后进入详情页发现新发布的博客已经被删除
- 注销: 点击注销后回到登录页并清空输入框
三、自动化测试
3.1 测试前初始化操作
进行自动化测试前,我们需要进行一些准备工作:前置操作、后置操作、确认执行顺序
确定测试执行的顺序
①登录成功测试 ②确定博客列表数量 ③编辑博客 ④查看博客详情 ⑤删除博客 ⑥注销用户
测试前置操作与后置操作
public class InitAndEnd {
//初始化服务器驱动
static WebDriver webDriver;
/**
* 前置操作, 创建驱动, 打开浏览器
*/
@BeforeAll
static void SetUp() {
webDriver = new ChromeDriver();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS ); // 智能等待
}
/**
* 后置操作, 关闭浏览器所有窗口, 清除访问痕迹
* @throws InterruptedException
*/
@AfterAll
static void TearDown() throws InterruptedException {
sleep(2000); //强制等待2s
webDriver.quit();
}
}
3.2 登陆成功测试
执行顺序: 访问博客登录页, 输入预先准备的正确用户名和密码,单击登录
预期结果: 跳转到博客列表页,登录成功
/**
* 输入正常的账号, 密码登录成功
*/
@Test
@Order(1)
void LoginSuccess() throws InterruptedException {
//打开登录页面
webDriver.get("http://101.200.198.144:9090/blog_login.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//输入账号密码
webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//点击提交
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(300);
//跳转到列表页
//获取当前页面的URL
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.200.198.144:9090/blog_list.html", cur_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//列表页显示的登录信息为zhangsan
String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
Assertions.assertEquals("zhangsan", cur_admin);
}
3.3 博客列表数量测试
执行顺序: 访问博客列表页, 获取当前页面的文章标题数量
预期结果: 博客数量不为0
/**
* 博客列表页博客数量不为0
*/
@Test
@Order(2)
void BlogList() {
webDriver.get("http://101.200.198.144:9090/blog_list.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
//获取博客列表页面标题的数量
int title_num = webDriver.findElements(By.cssSelector(".title")).size();
//不期望是0
Assertions.assertNotEquals(0, title_num);
}
3.4 编辑博客
执行顺序: 登录状态下, 单击写博客进入到博客编辑页, 输入符合要求的博文内容并单击发布
预期结果: 发布成功并跳转到博客列表页
/**
* 编辑博客
*/
@Test
@Order(3)
void EditBlog() throws InterruptedException {
//找到写博客按钮并点击
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
sleep(2000);
//通过JS将标题输入
((JavascriptExecutor) webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
sleep(2000);
//点击发布
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(2000);
//获取当前URL
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals("http://101.200.198.144:9090/blog_list.html", cur_url);
}
3.5 博客详情页测试
执行顺序: 进入到博客详情页, 获取到页面的信息并与设定好的信息进行校对
预期结果: 提示测试通过
public static Stream<Arguments> Generator() {
return Stream.of(Arguments.arguments("http://101.200.198.144:9090/blog_detail.html",
"博客详情页",
"自动化测试"));
}
/**
* 博客详情页校验
* url
* 标题
* 页面title是"自动化测试"
*/
@ParameterizedTest
@Order(4)
@MethodSource("Generator")
void BlogDetail(String expected_url, String expected_title, String expected_blogTitle) throws InterruptedException {
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
//找到第一篇博客的"查看全文"按钮
webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
sleep(200);
//获取当前页面url
String cur_url = webDriver.getCurrentUrl();
//获取页面title
String cur_title = webDriver.getTitle();
//获取博客标题
String cur_blogTitle = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
Assertions.assertEquals(expected_title, cur_title);
Assertions.assertEquals(expected_blogTitle, cur_blogTitle);
if (cur_url.contains(expected_url)) {
System.out.println("测试通过");
} else {
System.out.println("测试不通过");
}
}
3.6 删除博客
执行顺序: 在博客列表页选择博客进入详情, 单击删除按钮返回列表页
预期结果: 判断第一个博客信息与发布的博客信息不同以证明删除成功
/**
* 删除博客
* @throws InterruptedException
*/
@Order(6)
@Test
void DeleteBlog() throws InterruptedException {
//打开博客列表页
//点击查看全文
//webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
sleep(2000);
//删除
webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();
sleep(2000);
//列表页第一篇博客标题不是"自动化测试"了
String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
Assertions.assertNotEquals(first_blog_title,"自动化测试");
}
3.7 注销用户
执行顺序: 找到右上角的注销按钮并单击, 之后获取跳转后的url与登录界面的提交按钮
预期结果: 获取到的校验值与预期相符, 注销成功.
/**
* 注销
* @throws InterruptedException 强制等待
*/
@Test
@Order(7)
void Logout() throws InterruptedException {
//点击注销按钮
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
sleep(2000);
//获取当前URL
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals("http://101.200.198.144:9090/blog_login.html",cur_url);
//校验提交按钮
WebElement element = webDriver.findElement(By.cssSelector("#submit"));
Assertions.assertNotNull(element);
}
自动化测试总结
在此次编写测试代码进行该项目的自动化测试期间出现了一些错误与异常情况
- 控制区显示未获取到某个元素
错误原因: 脚本执行速度过快, 页面未渲染完成便执行下一步
解决办法: 在每次跳转其他URL之前添加代码等待, 让代码暂时休眠. - 参数化方法显示未获取到参数
错误原因: 使用了中文逗号
解决办法: 将中文逗号换成英文逗号