目录
测试用例
功能测试
界面测试
自动化测试
性能测试
总结
测试用例
功能测试
下面以 注册功能 测试为例进行展示:
正常注册:
以 姓名:王五 邮箱:1342111111@qq.com 手机号:13421111111 密码:1342111111 为例进行注册:
点击注册:
输入姓名、邮箱、手机号和密码,点击注册按钮后,页面会提示 "注册成功,去登录" ,正常注册情况下功能正常
异常注册:
异常注册主要从以下角度进行测试:
1. 用户名异常:用户名为空
2. 邮箱异常
a. 邮箱为空
b. 邮箱格式错误
c. 邮箱已存在
3. 手机号异常:
a. 手机号为空
b. 手机号格式错误
c. 手机号已存在
4. 密码异常:
a. 密码为空
b. 密码长度格式错误
c. 密码中包含特殊字符
未填写完注册信息,直接点击注册:
页面会提示对应信息未输入
邮箱格式错误:
邮箱已存在:
手机号格式错误:
手机号已存在:
密码过长(大于 12 位):
密码过短(小于 6 位):
密码中包含特殊字符:
异常注册情况下,会提示对应的异常信息
通过测试,在注册页面并未发现 bug
界面测试
以 登录页面 测试为例进行展示:
登录页面图片、按钮以及输入框正常显示,提示信息正确,无错别字,无关键信息遮挡情况
密码登录和验证码登录之间能够正确切换,验证码登录按钮、输入框正确显示
压缩情况下,图片不再显示,仅显示登录信息
注册链接正确显示,能够跳转到注册页面
自动化测试
以 人员列表 为例进行演示:
引入依赖:
<dependencies><!-- selenium --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>4.0.0</version></dependency><!-- 驱动管理--><dependency><groupId>io.github.bonigarcia</groupId><artifactId>webdrivermanager</artifactId><version>5.5.3</version></dependency><!-- 屏幕截图--><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency></dependencies>
我们创建 Utils 类,用于存放自动化代码中的通用方法:
public class Utils {public static WebDriver webDriver;/*** 创建 webDriver 并 访问指定 url* @param url*/public Utils(String url) {if (null == webDriver) {WebDriverManager.edgedriver().setup();EdgeOptions options = new EdgeOptions();// 允许访问所有连接options.addArguments("--remote-allow-origins=*");this.webDriver = new EdgeDriver(options);// 隐式等待 3 秒this.webDriver.manage().timeouts().implicitlyWait(java.time.Duration.ofSeconds(3));}webDriver.get(url);}/*** 关闭 WebDriver 和浏览器*/public void closeBrowser() {if (webDriver != null) {webDriver.quit();}}/*** 进行屏幕截图并将其保存到自定路径* 路径: ./src/tests/image/2025-2-24/LoginPage-17548130.png* @param str*/public void getScreenShot(String str) {try {DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmmssSS");String dirTime = dateFormatter.format(LocalDateTime.now());String fileTime = timeFormatter.format(LocalDateTime.now());// 使用 File.separator 来适应不同操作系统上的路径分隔符String filename = "src" + File.separator + "test" + File.separator + "image" + File.separator + dirTime + File.separator + str + "-" + fileTime + ".png";// 创建目录Path path = Paths.get(filename).getParent();if (path != null && !Files.exists(path)) {Files.createDirectories(path);}// 将截图存放到指定位置File srcFile = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);File destFile = new File(filename);FileUtils.copyFile(srcFile, destFile);} catch (IOException e) {// 更好的错误处理或日志记录System.err.println("截图失败: " + e.getMessage());e.printStackTrace();}}
}
对人员列表页进行测试:
public class UserListPage extends Utils {private static String url = "http://49.108.48.236:8080/user-list.html";public UserListPage() {super(url);}/*** 未登录状态下访问人员列表页*/public void noLoginToUserListPage() {// 处理弹窗WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现Alert alert = webDriver.switchTo().alert();alert.accept();// 判断是否返回到登录页面String expect = webDriver.getTitle();assert expect.equals("管理员登录页面");}/*** 检查人员列表页是否加载成功*/public void userListPageRight() {// 1. 进行登录webDriver.findElement(By.cssSelector("#phoneNumber")).clear();webDriver.findElement(By.cssSelector("#password")).clear();webDriver.findElement(By.cssSelector("#phoneNumber")).sendKeys("13311111111");webDriver.findElement(By.cssSelector("#password")).sendKeys("13311111111");webDriver.findElement(By.cssSelector("#loginForm > button")).click();// 2. 等待页面加载WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#userList")));// 3. 导航到人员列表页webDriver.findElement(By.cssSelector("#userList")).click();// 4. 查看列表元素wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#userList")));getScreenShot(getClass().getName());}
}
进行测试:
public class RunTest {public static void main(String[] args) {UserListPage userListPage = new UserListPage();userListPage.noLoginToUserListPage();userListPage.userListPageRight();userListPage.closeBrowser();}
}
测试通过;
性能测试
使用 JMeter 对 管理员密码登录接口、获取人员列表接口、获取奖品列表接口 以及 获取活动列表接口 进行性能测试:
聚合报告:
结果分析:
1. 所有接口的异常率为0%,且响应时间较为稳定,说明系统在当前并发场景下具有良好的稳定性和可靠性
2. 总体吞吐量达到361.4/sec,单个接口的吞吐量也都在90次/秒以上,表明系统在当前并发场景下具备较强的处理能力
响应时间:
结果分析:
1. 管理员密码登录接口的响应时间波动较大,平均在75 ms左右
2. 获取人员列表、奖品列表和活动列表接口的响应时间相对稳定,平均在43 ms左右
每秒处理事务数:
结果分析:
1. 在测试过程中,每秒处理事务数基本保持在90次左右,说明系统在当前并发情况下仍能保持较高的处理能力
2. 接近测试结束时,TPS有明显下降趋势,这是由于线程数量逐渐减少导致的
活跃线程数:
结果分析:
1. 测试开始后,线程数量迅速上升至20个,并在大部分时间内保持稳定
2. 接近测试结束时,线程数量逐渐减少,最终降至0,表明所有线程任务完成并退出
总结
功能测试:
1. 在线抽奖系统的基本功能正常运行,正常流程能够正确执行
2. 用户进行抽奖时响应及时,能够对抽奖过程中的异常情况进行处理
3. 在进行抽奖通知时,仅使用 qq 邮箱进行通知,可对其进行优化,使系统支持多种邮箱通知以及验证码通知
界面测试:
1. 所有按钮点击响应及时,页面显示良好,无错别字,无遮挡或显示错误情况
2. 对于异常情况都使用弹窗进行提示,对用户体验不友好,可使用自定义模态框、表单验证反馈等进行提示
性能测试:
1. 该系统的性能测试结果表明其在当前并发场景下表现优秀,能够满足需求
2. 后续可调整测试配置,增加线程数或调整循环次数,以测试更高并发用户数下性能
其他改进点:
1. 客户端发送的手机号和密码在网络中明文传输,应对其进行加密
2. 添加奖品信息修改删除功能以及人员信息修改删除功能
3. 限制单个用户的抽奖次数和频率,防止恶意刷奖