商品信息管理自动化测试

目录

前言

一、思维导图

二、代码编写

1.在pom.xml文件中添加相关依赖

         2.自动化代码编写

三、代码测试

小结


前言

1. 针对商品信息管理项目进行测试,商品信息管理项目主要有商品列表页、部门列表页、员工列表页,主要功能:对商品信息的增删改查的功能。对于商品信息管理的测试主要就是对主要功能进行测试,按照页面以及测试用例的思维导图进行自动化脚本的编写及测试。

2.自动化测试一般步骤:

1)使用Xmind编写web自动化测试用例

2)创建自动化项目,根据测试用例实现脚本的编写及测试

一、思维导图

web自动化测试用例思维导图

二、代码编写

  1. 根据思维导图进行测试用例的代码编写,每个页面一个测试类,然后在各个测试类中进行测试用例的编写。
  2. 注意公共属性需要单独放一个类,方便进行代码复用。
  3. 创建启动以及截图会频繁进行复用,所以创建方法进行封装。
  4. 注意添加隐式等待,为了确保页面正确加载显示

1.在pom.xml文件中添加相关依赖

<!--引入驱动管理依赖-->
<dependency>
     <groupId>io.github.bonigarcia</groupId>
     <artifactId>webdrivermanager</artifactId>
     <version>5.8.0</version>
     <scope>test</scope>
     </dependency>
<!--引入selenium依赖-->
<dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.0.0</version>
</dependency>
<!--引入屏幕截图依赖-->
<dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>
</dependency>

2.自动化代码编写

代码只展示主要部分

1.创建驱动对象和截屏会频繁使用,将其封装成方法


// 创建驱动对象
void createDriver() {
     if (driver == null) {
         // 1. 使用驱动管理程序打开谷歌浏览器驱动;
         WebDriverManager.chromedriver().setup();
         //  添加浏览器配置-创建的驱动对象允许浏览器访问所有链接
         ChromeOptions options = new ChromeOptions();
         options.addArguments("--remote-allow-origins=*");

         driver = new ChromeDriver(options);
     }
}

/**
 * 截屏并保存到指定目录下
 * 文件名格式:./src/test/images/2025-01-29/FirstTest_130725.png
 */
private void getScreenshot(String str) throws IOException {
     SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");// 日期 2025-01-29
     SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");// 时间戳 13时07分25秒25毫秒

     String dirTime = sim1.format(System.currentTimeMillis());// 日期目录 2025-01-29
     String fileTime = sim2.format(System.currentTimeMillis());// 时间戳 13时07分25秒25毫秒
//     ./src/test/images/2025-01-29/FirstTest_130725.png
     String fileName = "./src/test/images/"+dirTime + "/"+str + "_" +fileTime + ".png";// 文件名
     System.out.println("截图文件名(fileName):" + fileName);

//    拍截屏文件并保存到指定目录下
     File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
     FileUtils.copyFile(srcFile, new File(fileName));
}

2. 测试商品搜索功能

  • 测试页面能否正常打开
  • 测试能否依据名字查询商品信息
  • 测试能否依据品牌查询商品信息
/**
 * 测试 商品根据名字进行搜索的功能
 * @throws IOException
 */
void retrieveProduct() throws IOException, InterruptedException {
     createDriver();
     driver.get("http://localhost:90/#/product");
     // 获取商品页面中的输入框元素
     WebElement element = driver.findElement(By.cssSelector("body > div > section > section > main > form > div:nth-child(1) > div > div > input"));
     // 输入框输入内容 床上桌
     element.sendKeys("床上桌");
     // 点击搜索按钮
     driver.findElement(By.cssSelector("body > div > section > section > main > form > div:nth-child(2) > div > button")).click();
     Thread.sleep(1000);
     // 截屏并保存到指定目录下
     getScreenshot("ProductPageTest");

     //显示商品的详细信息,点击确定按钮
     driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(2) > div > div.el-dialog__footer > div > button.el-button.el-button--primary")).click();

     // 删除输入框中的内容 床上桌
     element.clear();

     driver.quit();
}

3.测试新增商品功能

  • 测试新增商品按钮功能能否正常使用
  • 测试填写新增商品的所有信息能否新增成功
  • 测试填写部分新增商品的信息能否新增成功
  • 测试不填写新增商品信息能否新增成功
/**
 * 测试 新增商品
 * @throws IOException
 * @throws InterruptedException
 */
void createProduct() throws IOException, InterruptedException {
     createDriver();
     driver.get("http://localhost:90/#/product");
     // 点击新增按钮
     driver.findElement(By.cssSelector("body > div > section > section > main > div.el-row > button")).click();

//     获取 名字输入框元素
     WebElement nameInput = driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(4) > div > div.el-dialog__body > form > div:nth-child(1) > div > div > input"));
     // 输入内容 插头
     nameInput.clear();
     nameInput.sendKeys("插头");

     //  获取 价格输入框元素
     WebElement priceInput = driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(4) > div > div.el-dialog__body > form > div:nth-child(2) > div > div > input"));
     // 输入内容 100
     priceInput.clear();
     priceInput.sendKeys("100");

     // 获取 品牌输入框元素
     WebElement descInput = driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(4) > div > div.el-dialog__body > form > div:nth-child(3) > div > div > input"));
     // 输入内容
     descInput.clear();
     descInput.sendKeys("公牛");

     // 获取库存输入框元素
     WebElement stockInput = driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(4) > div > div.el-dialog__body > form > div:nth-child(4) > div > div > input"));
     // 输入内容 100
     stockInput.clear();
     stockInput.sendKeys("100");

     // 点击确定按钮
     driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(4) > div > div.el-dialog__footer > div > button.el-button.el-button--primary")).click();

     // 显示等待1秒,直到确定按钮被点击,然后刷新页面
     WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
     wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(4) > div > div.el-dialog__footer > div > button.el-button.el-button--primary")));
     // 刷新页面
     driver.navigate().refresh();
//     Thread.sleep(1000);
     // 截屏并保存到指定目录下
     getScreenshot("ProductPageTest");
     // 断言新增的商品是否存在
//        WebElement product = driver.findElement(By.xpath("//td[contains(text(),'插头')]"));
//     assert product != null;

     driver.quit();
}

4.测试修改商品信息功能

  • 测试编辑按钮功能能否正常使用
  • 测试修改商品的所有信息能否修改成功
  • 测试修改商品的部分信息能否修改成功
/**
 * 测试 修改商品的价格
 * @throws IOException
 * @throws InterruptedException
 */
void updateProduct() throws IOException, InterruptedException {
     createDriver();
     driver.get("http://localhost:90/#/product");
//     Thread.sleep(1000);
//     隐式等待1秒
     driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000));
     // 点击编辑按钮
        driver.findElement(By.xpath("/html/body/div/section/section/main/div[4]/div[3]/table/tbody/tr[1]/td[5]/div/button[1]")).click();
     // 获取 价格输入框元素
     WebElement priceInput = driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(7) > div > div.el-dialog__body > form > div:nth-child(2) > div > div > input"));
     // 修改价格为 20
     priceInput.clear();
     priceInput.sendKeys("20");
     // 点击确定按钮
     driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(7) > div > div.el-dialog__footer > div > button.el-button.el-button--primary")).click();
     // 显示等待1秒,直到确定按钮被点击,然后刷新页面
     WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
     wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(7) > div > div.el-dialog__footer > div > button.el-button.el-button--primary")));
     // 刷新页面
     driver.navigate().refresh();
     // 截屏并保存到指定目录下
     getScreenshot("ProductPageTest");
     // 断言修改后的价格是否正确
//     WebElement product = driver.findElement(By.xpath("//td[contains(text(),'20')]"));
//     assert product != null;

     driver.quit();
}

5.测试删除商品功能

  • 测试删除按钮是否能够正常使用
  • 测试点击删除按钮再确认删除能否删除成功
  • 测试点击删除按钮再取消删除能否取消删除成功
  • 测试删除成功后刷新页面是否能够显示非删除数据
/**
 * 测试 删除商品
 * @throws IOException
 * @throws InterruptedException
 */
void deleteProduct() throws IOException, InterruptedException {
     createDriver();
     driver.get("http://localhost:90/#/product");
     // 点击删除按钮
     driver.findElement(By.xpath("/html/body/div/section/section/main/div[4]/div[3]/table/tbody/tr[1]/td[5]/div/button[2]")).click();
     // 点击确定按钮
//     driver.findElement(By.cssSelector("body > div:nth-child(2) > section > section > main > div:nth-child(6) > div > div.el-dialog__footer > div > button.el-button--primary")).click();
     // 显示等待1秒,直到确定按钮被点击,然后刷新页面
     WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
     wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div/section/section/main/div[4]/div[3]/table/tbody/tr[1]/td[5]/div/button[2]")));
     // 刷新页面
     driver.navigate().refresh();
     // 截屏并保存到指定目录下
     getScreenshot("ProductPageTest");
     // 断言删除后的商品是否不存在
//     WebElement product = driver.findElement(By.xpath("//td[contains(text(),'头')]"));
//     assert product == null;

     driver.quit();
}

三、代码测试

所有测试用例通过,但发现测试耗时有些长,说明性能还有优化的空间。

小结

  1. 一定要关注测试用例的执行顺序问题
  2. 对于页面的检查一定要到位,如检查元素是否存在确保页面的正确性
  3. 驱动关闭的位置要注意,只有最后一个用例结束之后才进行关闭
  4. 为了把所有的用例的执行结果保存下来,方便后续查错或查看,此时就需要进行该方法的定义即封装一个屏幕截图的方法
  5. 注意屏幕截图保存的方式:动态时间戳并进行时间格式化,然后期望按照某种维度(天、周)以文件夹的方式进行保存
  6. 在定位元素时,当复制的cssSelector的值比较长时建议复制xpath,因为cssSelector的值比较长就可能会导致定位元素失败
  7. 可以适当关注用例执行时间,如果时间过长就需要考虑是我们自己写的测试用例的问题还是程序真的有性能问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值