当前位置: 首页 > 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();}}

进行测试

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

在这里插入图片描述

http://www.xdnf.cn/news/946441.html

相关文章:

  • 【触想智能】工业平板电脑在无人巡检设备上的应用优势
  • 【Elasticsearch】Elasticsearch 在大数据生态圈的地位 实践经验
  • Nosql之Redis集群
  • 突破原生整数范围限制:C++高精度乘法算法模板的实现与优化
  • 信号的诞生:Linux进程信号的启示与奥秘
  • Spring Boot 与 Kafka 的深度集成实践(一)
  • AI编程--插件对比分析:CodeRider、GitHub Copilot及其他
  • 大模型——基于Docker+DeepSeek+Dify :搭建企业级本地私有化知识库超详细教程
  • Shell 解释器​​ bash 和 dash 区别
  • AWS中国云的定时任务(AWS EventBridge+AWS Lambda)
  • 中医有效性探讨
  • spdlog 介绍与使用指南
  • lambda的惰性求值方法与及早求值方法
  • Vue3 PC端 UI组件库我更推荐Naive UI
  • go 里面的指针
  • [蓝桥杯 2024 国 Java B] 美丽区间
  • pymilvus
  • VRFF: Video Registration and FusionFramework 论文详解
  • 启动已有小程序项目
  • 详解K8s 1.33原地扩缩容功能:原理、实践、局限与发展
  • 【K8S】Kubernetes从入门到实战:全面指南
  • 云原生K8s+Docker+KubeSphere+DevOps
  • K8S认证|CKS题库+答案| 9. 网络策略 NetworkPolicy
  • 上位机开发过程中的设计模式体会(1):工厂方法模式、单例模式和生成器模式
  • AspectJ 在 Android 中的完整使用指南
  • 博睿数据×华为, 共筑智慧金融新未来
  • UE5 学习系列(一)创建一个游戏工程
  • 机器学习监督学习实战六:五种算法对新闻组英文文档进行文本分类(20类),词频统计和TF-IDF 转换特征提取方法理论和对比解析
  • 【安全篇】金刚不坏之身:整合 Spring Security + JWT 实现无状态认证与授权
  • 让 Kubernetes (K8s) 集群 使用 GPU