抽奖系统Selenium自动化测试流程解析
🌈感谢大家的阅读、点赞、收藏和关注
💕希望大家喜欢我本次的讲解💕
目录👑
一、自动化测试环境与框架核心配置🌟
1. 技术栈与依赖(测试文档 - 环境配置章节)
描述:本次抽奖系统 UI 自动化测试基于 Spring Boot + Selenium 4.0 实现,核心依赖包括 Selenium-Java(浏览器驱动操作)、WebDriverManager(自动管理浏览器驱动版本)、commons-io(截图功能),测试环境为 Java 17,浏览器为 Chrome。
关键代码(pom.xml 核心依赖):
<!-- Selenium核心依赖 --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.0.0</version> </dependency> <!-- 自动管理ChromeDriver版本 --> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.8.0</version> <scope>test</scope> </dependency> <!-- 截图功能依赖 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> 2. 浏览器驱动初始化(测试文档 - 基础工具章节)
描述:封装统一的 Chrome 驱动创建方法,自动下载匹配版本的驱动,配置--remote-allow-origins=*解决跨域问题,设置 6 秒隐式等待确保元素加载,同时封装 WebDriverWait 显式等待(最长 60 秒)适配复杂场景。
关键代码(Utils 类核心方法):
public static WebDriver createDriver() { if(driver == null) { // 自动下载并配置ChromeDriver WebDriverManager.chromedriver().setup(); ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); // 解决跨域限制 driver = new ChromeDriver(options); // 隐式等待:全局等待元素加载 driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(6)); } return driver; } 二、核心工具类(测试文档 - 通用工具章节)❄️
1. 测试数据自动生成(解决测试数据重复问题)
描述:封装 3 类随机测试数据生成方法,避免手动造数,保证测试用例的可重复性:
- 随机手机号:生成符合 11 位规范的手机号(第一位固定为 1,第二位 3-9,后 9 位随机);
- 唯一 QQ 邮箱:基于 UUID 生成绝对唯一的 QQ 邮箱,避免重复注册问题;
- 随机管理员名称:生成 1-6 位字母 + 数字混合字符串。
关键代码:
// 随机手机号生成 public static String generateRandomMobile() { StringBuilder mobile = new StringBuilder("1"); mobile.append(MOBILE_SECOND_DIGITS[RANDOM.nextInt(MOBILE_SECOND_DIGITS.length)]); for (int i = 0; i < 9; i++) { mobile.append(RANDOM.nextInt(10)); } return mobile.toString(); } // 唯一QQ邮箱生成 public static String generateUniqueQQEmail() { String uuidPart = UUID.randomUUID().toString().replace("-", "").substring(0, 8); long uuidNum = Long.parseLong(uuidPart, 16) % 100000000; String qqNumber = String.valueOf(Math.abs(uuidNum)); if (qqNumber.length() < 5) { qqNumber = "12345" + qqNumber; } return qqNumber.substring(0, 8) + "@qq.com"; } 2. 自动化截图(测试失败溯源)
描述:封装截图方法,按 “年月日 / 类名 - 时分秒毫秒.png” 路径存储截图,可在测试用例失败时自动捕获页面状态,便于问题定位。
关键代码:
public static void screenShot(String str) throws IOException { SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sim2 = new SimpleDateFormat("HH_mm_ss_SS"); String fileName = "./src/test/java/image/" + sim1.format(System.currentTimeMillis()) + "/" + str + "-" + sim2.format(System.currentTimeMillis()) + ".png"; File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(srcFile, new File(fileName)); } 三、核心业务模块测试逻辑(测试文档 - 功能测试章节)🍃
1. 登录 / 注册模块(边界值 + 异常场景全覆盖)
描述:覆盖注册 / 登录的全量异常场景,采用 “先异常后正常” 的测试逻辑,验证前端校验和后端逻辑:
- 注册:空值校验→格式错误(邮箱 / 手机号)→已注册数据校验→正常注册;
- 登录:手机号 / 密码错误→手机号正确密码错误→正常登录;
- 核心技术:切换 Alert 弹窗、显式等待弹窗加载、元素定位(CSS Selector/XPath 混合使用)。
关键代码(注册异常场景):
// 空值校验:不填信息直接注册,验证前端提示 driver.findElement(By.cssSelector("#registerForm > button")).click(); String nameErr = driver.findElement(By.cssSelector("#name-error")).getText(); assert !nameErr.isEmpty(); // 格式错误:邮箱填123456,验证格式提示 driver.findElement(By.xpath("//*[@id=\"mail\"]")).sendKeys("123456"); driver.findElement(By.cssSelector("#registerForm > button")).click(); String mailErr = driver.findElement(By.cssSelector("#mail-error")).getText(); assert !mailErr.isEmpty(); // 已注册数据校验:弹窗提示处理 wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); String text = alert.getText(); // 获取弹窗提示(如“手机号已被使用”) alert.accept(); 2. 管理员核心模块(iframe 切换 + 多场景校验)
描述:抽奖系统管理员端核心模块测试,覆盖人员管理、奖品管理、活动管理,解决 iframe 嵌套页面元素定位问题,核心场景:
- 人员管理:注册校验、列表元素验证;
- 奖品管理:奖品创建(价格 0 / 负数 / 正常值校验)、列表数据提取;
- 活动管理:活动创建(奖品 / 人员数量校验)、抽奖流程自动化、中奖结果复制验证。
关键技术点代码:
// 1. iframe切换(解决嵌套页面元素定位) wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("contentFrame"))); // 切入iframe driver.switchTo().defaultContent(); // 切回顶层页面 // 2. 奖品价格校验 driver.findElement(By.xpath("//*[@id=\"price\"]")).sendKeys("0"); // 价格为0 driver.findElement(By.cssSelector("body > div > button")).click(); driver.findElement(By.xpath("//*[@id=\"price\"]")).clear(); driver.findElement(By.xpath("//*[@id=\"price\"]")).sendKeys("-1"); // 价格为负数 driver.findElement(By.cssSelector("body > div > button")).click(); // 3. 抽奖流程自动化(按钮状态切换+循环抽奖) By nextBtnLocator = By.cssSelector("#container > div.opt-box > span.btn.next-btn"); while(true) { WebElement nextBtn = wait.until(ExpectedConditions.visibilityOfElementLocated(nextBtnLocator)); String btnText = nextBtn.getText(); if (btnText.equals("已全部抽完")) break; // 退出条件 nextBtn.click(); // 点击开始抽奖 Thread.sleep(3000); wait.until(ExpectedConditions.textToBe(nextBtnLocator, "点我确定")); nextBtn.click(); // 确定中奖人 wait.until(ExpectedConditions.textToBe(nextBtnLocator, "已抽完,下一步")); nextBtn.click(); // 下一步 } 3. 测试执行入口(全流程自动化)
描述:统一测试执行入口,按 “注册管理员→管理员登录→人员管理→奖品管理→活动管理→抽奖→退出” 流程执行,自动关闭浏览器,实现全流程无人值守测试。
关键代码:
public static void main(String[] args) throws InterruptedException { LoginPage loginPage = new LoginPage(); loginPage.checkRegister(); loginPage.checkLogin(); AdminPage adminPage = new AdminPage(); adminPage.humanAdmin(); adminPage.humanList(); adminPage.createPrize(); adminPage.prizeList(); adminPage.createActivity(); adminPage.activityList(); adminPage.loginout(); Thread.sleep(1500); Utils.quit(); // 关闭浏览器 } 四、关键技术难点与解决方案(测试文档 - 技术难点章节)
| 技术难点 | 解决方案 |
|---|---|
| iframe 嵌套页面定位 | 先等待 iframe 加载完成,切换到 iframe 后再定位元素,操作完成切回顶层页面; |
| 弹窗(Alert)处理 | 显式等待弹窗加载(ExpectedConditions.alertIsPresent()),切换到弹窗后操作; |
| 动态按钮状态切换 | 基于按钮文本变化的显式等待(textToBe),适配抽奖按钮 “开始→确定→下一步” 状态; |
| 测试数据重复 | 基于 UUID / 随机数生成唯一测试数据(手机号 / 邮箱),避免重复注册 / 重复抽奖; |