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

【JavaAPI搜索引擎】自动化测试报告

API搜索引擎自动化测试报告

  • 前言
  • 脑图
  • 代码编写
    • 公共类Utils
    • 搜索页面SearchPage
    • AutoBlogTestApplicationTests
  • 进行测试

前言

针对API搜索引擎项目进行测试,个人博客主要由一个搜索页面构成:主要功能包括:输入一个查询词,可以得到若干个搜索结果,同时还可以针对搜索结果的标题进行点击,对于搜索引擎的测试主要就是针对搜索功能进行测试,然后书写测试类。
个人项目地址:http://123.56.249.238:8080/index.html
自动化测试一般步骤:
1)使用脑图编写web自动化测试用例
2)创建自动化项目,根据用例来实现脚本

脑图

根据脑图对主要的搜索功能进行测试:
在这里插入图片描述

代码编写

  1. 创建一个公共包,在这个包下编写公共类:Utils
  2. 在AutoBlogTestApplicationTests中执行测试用例
  3. 创建一个tests包,在这个包中编写SearchPage类测试搜索功能
    在这里插入图片描述

公共类Utils

  1. 创建驱动、保存现场截图
  2. 注意:在保存现场截图的时候命名是按时间来进行文件夹的划分,然后图片的名称要体现出测试类的类名,方便进行问题的追溯。
  3. 注意文件名的动态获取,注意时间格式的设置。
  4. 注意:可以在创建驱动的时候修改默认的有头模式or无头模式
package common;import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;public class Utils {public static WebDriver driver;public static WebDriver  createDriver(){if(driver == null){WebDriverManager.chromedriver().setup();ChromeOptions options = new ChromeOptions();//允许访问所有的连接options.addArguments("--remote-allow-origins=*");driver = new ChromeDriver(options);//设置为无头模式:options.addArguments("-headless");//隐式等待driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));}return driver;}public Utils(String url){//调用driver对象driver = createDriver();//访问URLdriver.get(url);}//进阶版本的屏幕截图public void getScreenShot(String str) throws IOException {//     .src/test/image///                     /2024-7-17///                                /test01-170430.png//                                /test02-180430.png//                     /2024-7-18///                                /test01-170430.png//                                /test02-180430.png//第一个格式是年月日的设计 /2024-7-17/SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");//第二个格式是文件名称日期的设计:/test01-170430.pngSimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");//第一个获取到当前年月日时间戳String dirTime = sim1.format(System.currentTimeMillis());//第二个获取到当前时分秒的时间戳String fileTime  = sim2.format(System.currentTimeMillis());String filename = "./src/test/image/" +dirTime + "/"+str +"-" + fileTime +".png";File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);//src放到指定的位置FileUtils.copyFile(srcFile, new File(filename));}
}

搜索页面SearchPage

  1. 创建驱动,并打开页面
  2. 测试页面是否正常打开
  3. 测试输入框是否可以输入内容
  4. 输入"Arrays",之后测试搜索按钮是否可以正常点击
  5. 测试搜索结果是否正确,是否含有结果"Arrays"
  6. 测试点击Arrays之后是否可以跳转到Arrays页面
  7. 测试Arrays页面上的元素是否可以正常点击
  8. 清空内容之后搜索框是否可以再次输入内容
package tests;import common.Utils;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Set;public class SearchPage extends Utils {public static String url = "http://123.56.249.238:8080/index.html";public SearchPage() {super(url);}//对搜索框的搜索功能进行测试:public  void testSearch() throws IOException {createDriver();//找到搜索框,输入关键词Arraysdriver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("Arrays");//然后点击搜索按钮,进行搜索操作driver.findElement(By.cssSelector("#search-btn")).click();//检查页面上的Arrays是否可以正常点击driver.findElement(By.cssSelector("body > div > div.result > div:nth-child(2) > a")).click();//找到当前这个driver:String curHandle = driver.getWindowHandle();//进行driver的切换Set<String> allHandles = driver.getWindowHandles();for(String handle : allHandles){if(handle != curHandle){driver.switchTo().window(handle);}}driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);//srcFile放到指定的位置FileUtils.copyFile(srcFile, new File("my.png"));//点击Arrays的页面是否可以跳转driver.findElement(By.cssSelector("body > header > nav > div.fixedNav > div.topNav > ul > li:nth-child(8) > a")).click();driver.close();driver.switchTo().window(curHandle);driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).clear();driver.findElement(By.cssSelector("body > div > div.header > input[type=text]")).sendKeys("list");driver.findElement(By.cssSelector("#search-btn")).click();driver.quit();}}

AutoBlogTestApplicationTests

@SpringBootTest
class AutoBlogTestApplicationTests {public static void main(String[] args) throws IOException {SearchPage searchPage = new SearchPage();searchPage.testSearch();}}

进行测试

查看控制台,没有出现报错,测试通过:

在这里插入图片描述

相关文章:

  • React---day11
  • llama-factory微调大模型环境配置避坑总结
  • Html实现图片上传/裁剪/马赛克/压缩/旋转/缩放
  • 【Dv3Admin】系统视图菜单管理API文件解析
  • 阿里云服务器 篇十七:网站悬浮球
  • centos开启samba服务
  • 超低成本U型光电开关红外对射管检测电路
  • RLHF vs RLVR:对齐学习中的两种强化方式详解
  • Python 如何在Python 3.6上安装PIP
  • 【Proteus仿真】【32单片机-A011】HX711电子秤系统设计
  • 解决网页导出PDF部分内容被遮挡问题
  • Ubuntu 20.04 联网设置指南
  • wifi改ip地址有什么用?wifi改ip地址怎么改
  • 【MySQL基础】MySQL表操作全面指南:从创建到管理的深度解析
  • Linux系统:进程间通信-匿名与命名管道
  • ZYNQ学习记录FPGA(二)Verilog语言
  • MCU ADC硬件设计注意事项
  • vulnyx Blogger writeup
  • Linux学习
  • 机器学习×第五卷:线性回归入门——她不再模仿,而开始试着理解你
  • django做网站好吗/网络营销的五大特点
  • 做诈骗网站/合肥今日头条最新消息
  • 公司新建网站公安备案填哪个/济南seo整站优化厂家
  • 美食网站建设前的市场分析/谈谈你对seo概念的理解
  • 加强政府网站建设管理的重要性/四川seo选哪家
  • 网站做进一步优化/sem竞价广告