当前位置: 首页 > news >正文

基于java的叙事之眼系统自动化测试

1.公共类(Utils)

这是一个叙事之眼写小说自动化测试的公共工具类,进行Selenium 自动化测试,所有测试用例都可以共用它,统一创建、管理 Chrome 浏览器驱动,打开测试页面,设置等待时间,提供自动化截图功能。

作用:

  • 统一管理浏览器驱动,避免重复创建、资源浪费

  • 实现单例浏览器,所有测试用例共用一个 driver

  • 统一打开测试页面,统一设置等待时间

  • 提供公共截图工具,自动按时间命名、自动保存

  • 大幅减少测试代码冗余,让测试用例更简洁、易维护

public class Utils { public static WebDriver driver = null; public WebDriverWait wait = null; //内容页URL public static String detailUrl = "http://8.137.19.140:8080/novel_detail.html?blogId=115789"; public Utils(String url) { driver = createDriver(); //调用driver driver.get(url); //显示等待 wait = new WebDriverWait(driver,Duration.ofSeconds(3)); } //创建驱动对象 public static WebDriver createDriver() { if(driver == null) { //下载驱动 WebDriverManager.chromedriver().setup(); ChromeOptions options = new ChromeOptions(); //添加配置:允许访问所有的连接 options.addArguments("--remote-allow-origins=*"); //创建驱动对象 driver = new ChromeDriver(options); //添加隐式等待 driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3)); } return driver; } public void ScreenShot(String str) throws IOException { //年月日 SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd"); //时分秒毫秒 SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS"); String dirTime = sim1.format(System.currentTimeMillis()); String fileTime = sim2.format(System.currentTimeMillis()); //文件名:./src/test/java/images/2025-07-12/test01-15083020.png String filename = "./src/test/java/images/" + dirTime + "/" + str + "-" + fileTime + ".png"; File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(srcFile,new File(filename)); } }

2.小说登录页(loginPage)

该类基于 Selenium 实现对登录页面的全流程自动化验证,直接继承公共工具类Utils,复用浏览器驱动、页面加载、隐式等待、显示等待、自动截图等基础能力,无需重复编写底层代码。

作用:

  • 该类是小说系统登录页面的自动化测试类,继承工具类实现浏览器操作、页面访问、自动截图等基础功能。

  • 代码实现了页面加载校验、正常登录成功、异常登录失败三大核心测试场景,全面验证登录功能。

  • 所有测试步骤自动截图留存,确保登录功能可用、安全、稳定,为小说系统提供可靠的功能保障。

package tests; import common.Utils; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.support.ui.ExpectedConditions; import java.io.IOException; public class loginPage extends Utils { public static String url = "http://8.137.19.140:8080/novel_login.html"; public loginPage() { super(url); } /** * 检查登录页面 */ public void checkPageRight() throws IOException { //检查登录框 driver.findElement(By.cssSelector("#username")); driver.findElement(By.cssSelector("#password")); driver.findElement(By.cssSelector("#submit")); //截图 ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName()); } /** * 登录成功——正确的账号和密码 */ public void LoginSuc() throws IOException { //先清空输入框 driver.findElement(By.cssSelector("#username")).clear(); driver.findElement(By.cssSelector("#password")).clear(); driver.findElement(By.cssSelector("#username")).sendKeys("admin"); driver.findElement(By.cssSelector("#password")).sendKeys("123456"); driver.findElement(By.cssSelector("#submit")).click(); //截图 ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName()); //验证登录成功---退出登录按钮 driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")); } /** * 异常登录 * ————用户名和密码都为空 * ————用户名和密码都错误 * ————用户名不为空,密码为空 * ————用户名为空,密码不为空 * ————错误的用户,正确的密码 * ————正确的用户名,错误的密码 */ //正确的用户名,错误的密码 public void LoginFail() throws IOException { driver.findElement(By.cssSelector("#username")).sendKeys("admin"); driver.findElement(By.cssSelector("#password")).sendKeys("0000"); //截图 ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName()); driver.findElement(By.cssSelector("#submit")).click(); //等待弹窗 wait.until(ExpectedConditions.alertIsPresent()); //处理错误弹窗 Alert alert = driver.switchTo().alert(); alert.accept(); } }

3.小说列表页(listPage)

该类模拟用户访问小说列表页的真实操作,自动检查页面核心模块是否正常加载、小说信息是否完整显示、「查看全文」按钮是否能正常跳转到小说详情页,确保小说列表页的展示功能、跳转功能正常可用,保障用户能正常浏览小说列表并进入内容页阅读。

作用:

  • 先获取列表页的小说标题,作为校验依据;

  • 自动点击「查看全文」按钮,模拟用户跳转操作;

  • 获取跳转后详情页的小说标题,对比两个标题是否一致,验证跳转功能正常、跳转到正确的小说详情页;

  • 记录内容页 URL,供后续测试复用,实现测试数据传递。

package tests; import common.Utils; import org.openqa.selenium.By; public class listPage extends Utils { public static String url = "http://8.137.19.140:8080/novel_list.html"; public listPage() { super(url); } /** * 检查小说列表——三个模块(个人信息模块、小说列表模块、菜单模块) * 小说列表模块 */ public void checknovelList() { //小说题目 String title = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(2) > div.title")).getText(); //小说发布时间 String pushTime = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(2) > div.date")).getText(); //小说内容 String content = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(2) > div.desc")).getText(); //查看小说全文 String button = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(2) > a")).getText(); //校验文本 assert !title.isEmpty(); assert !pushTime.isEmpty(); assert !content.isEmpty(); assert button.equals("查看全文>>"); } /** * 检查查看全文按钮的跳转 */ public void checkBtnJump() { //获取列表页小说题目 String title = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(2) > div.title")).getText(); //点击查看全文按钮实现跳转 driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click(); //获取小说内容题目 String jumpTitle = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText(); //验证 assert jumpTitle.equals(title); detailUrl = driver.getCurrentUrl(); } }

4.小说内容页(detailPage)

该类自动验证小说内容页能否正常打开、小说信息是否完整展示、编辑 / 删除功能按钮是否显示正确,确保用户和管理员可以正常查看、操作小说详情,保障小说系统内容展示与管理功能稳定可用。

作用:

  • 获取页面核心信息:自动定位并读取小说标题、发布时间、正文内容,确认信息可以正常获取;

  • 获取功能按钮:定位并读取编辑按钮、删除按钮的显示文字,确认按钮显示正常;

  • 断言验证:

    • 标题、发布时间、小说正文不为空 → 页面内容正常展示、没有空白;

    • 编辑按钮显示 “编辑”、删除按钮显示 “删除” → 功能按钮正常显示;

  • 整体验证:只要所有断言通过,就代表小说内容页加载完整、展示正常、管理功能可用。

package tests; import common.Utils; import org.openqa.selenium.By; public class detailPage extends Utils { public static String url = detailUrl; public detailPage() { super(url); } /** * 验证内容页是否加载 */ public void CheckPageRight() { //题目 String title = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText(); //发布时间 String pushTime = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.date")).getText(); String content = driver.findElement(By.cssSelector("#detail")).getText(); /* //内容 String content = driver.findElement(By.cssSelector("#detail > p")).getText();*/ //编辑按钮 String edit = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)")).getText(); //删除按钮 String del = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).getText(); //校验 assert !title.isEmpty(); assert !pushTime.isEmpty(); assert !content.isEmpty(); assert edit.equals("编辑"); assert del.equals("删除"); } }

5.发布小说页(sendPage)

该类模拟管理员发布小说的完整操作,自动生成标题、填写内容、点击发布,最后校验小说是否成功出现在列表页,确保小说发布功能可用、数据能正常保存并展示。

  • 生成唯一标题使用时间戳生成不重复的小说名,避免重复发布导致测试失败。

  • 自动填写发布表单

    • 自动在标题框输入标题

    • 双击富文本编辑器输入小说内容(适配编辑器需要双击激活的特性)

  • 执行发布操作自动点击「发布」按钮,提交小说数据。

  • 验证发布结果(最关键)

    • 发布后自动跳转到小说列表页

    • 获取列表中最新一条小说的标题

    • 与发布时的标题做对比

    • 断言一致 = 发布成功

  • 全流程闭环测试从填写 → 发布 → 校验展示,完整验证小说发布功能。

package tests; import common.Utils; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import java.text.SimpleDateFormat; import java.util.List; public class sendPage extends Utils { public static String url = "http://8.137.19.140:8080/novel_send.html"; public sendPage() { super(url); } /** * 正常发布小说 */ public void sendSuc() throws InterruptedException { //创建的小说标题随机生成 SimpleDateFormat sim = new SimpleDateFormat("HHmmssSS"); String titleTime = sim.format(System.currentTimeMillis()); String title = "自动创建小说-"+titleTime; //找到标题输入框并输入内容 driver.findElement(By.cssSelector("#title")).sendKeys(title); // //找到内容区域并输入内容 // driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")).sendKeys("我的小说"); Actions action = new Actions(driver); action.doubleClick(ele).sendKeys("小说内容").perform(); Thread.sleep(500); // action.click(ele).sendKeys("喜欢看小说").perform(); //点击发布 driver.findElement(By.cssSelector("#submit")).click(); //验证是否发布成功 //发布成功之后跳转到列表页,找是否存在刚创建的小说————小说标题 List<WebElement> novel = driver.findElements(By.cssSelector("body > div.container > div.right > div")); String titleAfter = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child("+blogs.size()+") > div.title")).getText(); assert titleAfter.equals(title); } }

6.runTest

该类它不负责具体的页面元素操作,只负责按顺序启动并执行所有页面的测试用例,一键完成从登录→列表→详情→发布的全流程自动化测试。

package tests; import java.io.IOException; public class runTests { public static void main(String[] args) throws IOException, InterruptedException { //登录用例 LoginPage login = new loginPage(); login.checkPageRight(); login.LoginFail(); login.LoginSuc(); //列表页用例 ListPage list = new listPage(); list.checknovelList(); list.checkBtnJump(); //内容页用例 DetailPage detail = new detailPage(); detail.CheckPageRight(); //发布页用例 SendPage send = new sendPage(); send.sendSuc(); } }

7. 总结

小说系统自动化测试围绕核心模块开展,完成脚本开发、用例执行、缺陷跟踪等工作,达成预设测试目标,测试用例全部通过。

http://www.jsqmd.com/news/669792/

相关文章:

  • Spring with AI (): 评估答案——UnitTest引入
  • MySQL中如何使用UPPER转大写字母_MySQL文本格式化函数
  • RMBG-2.0功能体验:蒙版查看、一键下载,完整操作流程
  • LeetCode 594题‘磁带利用率’详解:从背包DP到贪心交换,附C++完整代码与三大易错点
  • 5分钟部署Qwen2.5-VL-7B视觉模型:Ollama让多模态AI触手可及
  • 用了5款降AI率工具后,到底哪个好?真实排名告诉你
  • Fish Speech 1.5语音合成AB测试:不同temperature下自然度主观评分对比
  • 忍者像素绘卷入门必看:5分钟完成Python环境安装与首次调用
  • 第32篇:AI数据标注——隐藏在巨头身后的百亿级市场与入门指南(概念入门)
  • Qwen3-VL-2B与HuggingFace模型对比:本地部署体验差异
  • 降AI率工具哪个好用?看完这篇手把手教你3步选对
  • 零代码体验NaViL-9B:上传图片自动问答,多模态AI快速上手
  • 避坑指南:STM32CubeMX配置FMC驱动LCD时常见的5个低级错误(附ILI9488调试记录)
  • Vision Transformer (ViT) 技术解析
  • 关于explorer.exe报错,及原因
  • YOLO12问题解决:常见报错处理,服务重启与参数调整指南
  • 基于springboot的性格测试系统
  • 下载命令参数或标志(-e等)
  • 告别VSCode!用Vim + NERDTree + cscope打造Linux内核开发者的专属IDE
  • C++哈希扩展:位图与布隆过滤器实战
  • 手把手教你用PyTorch 2.9镜像:从环境搭建到第一个AI程序
  • Pixel Aurora Engine 生成交互原型:将产品需求文档转化为可点击的UI流程图
  • 终极指南:3步在华硕路由器上快速部署AdGuardHome,打造无广告家庭网络
  • 为什么AI读脸术部署总失败?OpenCV DNN轻量模型避坑指南
  • 降AI率工具哪个好?教你3分钟判断工具是否靠谱
  • 前端八股文面经大全:携程前端一面(2026-04-17)·面经深度解析
  • 基于springboot的摄影约拍跟拍预定管理系统
  • GLM-TTS场景应用:有声书配音制作,AI语音合成实战分享
  • 给嵌入式新手的LCD扫盲课:别再只盯着RGB,搞懂HS、VS、DE和DCLK信号才算入门
  • AudioSeal问题解决:音频水印添加失败?常见格式与密钥问题排查指南