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

【软件测试】理论杂记 + Selenium

文章目录

  • 测试用例万能公式
  • 基于测试对象
  • 黑盒测试
    • 方法
  • 白盒测试
  • Selenium
    • 选择器
      • CSS选择器
      • XPath选择器
    • 等待
    • 常用API
      • 浏览器操作

测试用例万能公式

功能,界面,易用,兼容,安全,性能,网络
在这里插入图片描述

在这里插入图片描述

基于测试对象

在这里插入图片描述

界面测试
在这里插入图片描述

兼容性测试

  • 系统自身版本的兼容,用户已有数据的兼容
  • 测试与应用环境的兼容,如操作系统,应用平台,浏览器(浏览器版本)
  • 测试与第三方系统以及第三方数据的兼容性(不同APP)

安装卸载测试
在这里插入图片描述

黑盒测试

把程序当作一个黑盒子,不关心内部实现,注重输入输出
缺点:代码覆盖率低
优点:可以测试模块之间的联调

方法有:等价类,边界值,判定表->正交表,场景设计法,错误猜测法(凭经验)

方法

等价类
在这里插入图片描述
例子:要求输入8~16的数字
有效等价类:10
无效等价类:7


边界值
要求输入(8, 15]的值,左开右闭

  • 上点:边界值 — — 8和15
  • 内点:边界范围内的点 — — 10
  • 离点:距离上点最近的点,开区间往区内外找,闭区间往区间外找 — — 9 和 16

判定表
在这里插入图片描述


正交表
充分理解需求 -> 确定因素水平 -> 画正交表 -> 补充正交表 -> 将正交表转化为测试用例

在这里插入图片描述

在这里插入图片描述


场景设计法

在这里插入图片描述

白盒测试

语句覆盖,条件覆盖,判定覆盖,分支覆盖,路径覆盖

Selenium

Selenium是一个广泛使用的开源自动化测试工具,专门用于Web应用程序的测试。它支持多种浏览器和操作系统,使得用户能够编写在不同环境下运行的测试脚本

本篇博客介绍Selenium3
在这里插入图片描述

选择器

CSS选择器

在这里插入图片描述

  • id选择器:#id 例:#kw
WebElement element = webDriver.findElement(By.cssSelector("#kw"));//id选择器
  • 类选择器:.类名 例:.s_ipt
WebElement search = webDriver.findElement(By.cssSelector(".s_ipt"));//CSS——类选择器
  • 标签选择器:标签名 例:input
  • 后代选择器:父类标签 子类标签.... 例:form span input
List<WebElement> elements= webDriver.findElements(By.cssSelector("a em"));

XPath选择器

Idea快捷键:alt + ctrl + v 自动补齐接收函数返回值

  • 绝对路径(不常用)://html/head/title

在这里插入图片描述

  • 相对路径:
  1. 相对路径 + 索引://form/span[1]/input span[1]表示form下的第一个span,下标从1开始
WebElement button = webDriver.findElement(By.xpath("//form/span[2]/input"));
  1. 相对路径 + 属性名://input[@class=\"s_ipt\"]
  2. 相对路径 + 通配符://*[@*=\"kw\"]
  3. 相对路径 + 文本匹配://a[@text=\"新闻\"]
WebElement element = webDriver.findElement(By.xpath("//a[text()=\"新闻\"]"));

等待

浏览器的加载会随着网速,网页渲染,网页元素数量而变化,大部分情况我们都需要等网页加载好才进行下一步操作。通过等待实现

静态等待
调用Thread.sleep()进行强制等待,单位是毫秒,3000毫秒等于3秒

public static native void sleep(long millis) throws InterruptedException;

智能等待
智能等待有隐式等待和显示等待。
二者都可以设置最长等待时间,隐式等待结束等待的条件是页面加载完成,而显示等待可以自定义结束条件

  • 隐式等待
//隐式等待一天
webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.DAYS);
  • 显示等待
WebDriverWait wait = new WebDriverWait(webDriver, 10);
//显示等待标题是否为指定内容
wait.until(ExpectedConditions.titleIs("520_百度搜索"));
//显示等待该元素可见
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#\\31  > div > div._content-border_zc167_4.content-border_r0TOp.cu-border.sc-aladdin.sc-cover-card > div > div:nth-child(2) > div > div > div:nth-child(1) > div > div > div > div > div > div._info-mask_bo7t2_25")));

常用API

  • 查找返回单个元素
WebElement findElement(By var1);//参数是选择器
  • 查找返回多个元素
List<WebElement> findElements(By var1);//参数是选择器
  • 点击
void click();//通常使用这个

void submit();//非form标签内的元素使用会报错
  • 输入文字
void sendKeys(CharSequence... var1);
  • 清空输入
void clear();
  • 获取数据
String getAttribute(String var1);//获取标签属性

String getText();//获取输入框的内容

String getCurrentUrl();//获取当前URL

String getTitle();//获取当前页面的Title

浏览器操作

  • 页面回退
void back();
webDriver.navigate().back();
  • 页面刷新
void refresh();
webDriver.navigate().refresh();
  • 页面前进
void forward();
webDriver.navigate().forward();
  • 滑动条滑动,需要使用JS
((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
  • 页面最大化
webDriver.manage().window().maximize();
  • 页面充满屏幕
webDriver.manage().window().fullscreen();
  • 设置页面大小
webDriver.manage().window().setSize(new Dimension(700, 900));
  • 浏览器关闭
webDriver.quit();
webDriver.close();

使用quit会将整个浏览器关闭
使用close会将webDirver管理的那个页面关闭

  • 键盘操作
//Ctrl + V
webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
  • 鼠标右击
//找到图片
sleep(2000);
WebElement webElement = webDriver.findElement(By.cssSelector("#\\31  > div > h3 > a > div > div > p > span > span"));
sleep(2000);
//鼠标对象
Actions actions = new Actions(webDriver);
//鼠标移到图片
actions.moveToElement(webElement);
//右击
actions.contextClick().perform();
  • 切换 frame
webDriver.switchTo().frame("f1");
webDriver.findElement(By.cssSelector("body > div > div > a")).click();
  • 下拉框,下标从0开始
WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
Select select = new Select(webElement);
//select.selectByIndex(3);//选择下标为3的元素
select.selectByValue("12.51");//通过f12里的value值选择
  • 弹窗
// alert弹窗取消
webDriver.switchTo().alert().dismiss();
// 在alert弹窗中输入内容
webDriver.switchTo().alert().sendKeys("alert Test");
// alert弹窗确认
webDriver.switchTo().alert().accept();
  • 选择文件

选择文件是通过弹窗选择的,可以通过输入框选择文件
在这里插入图片描述

//输入框通过"input"定位
webDriver.findElement(By.cssSelector("input")).sendKeys("D:\\untitled");
  • 切换 webDriver 驱动管理的页面
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com/");
//点击百度的“新闻”
webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
sleep(3000);

// 通过getWindowHandle获取的驱动当前管理的页面窗口句柄
System.out.println(webDriver.getWindowHandle());
// 通过getWindowHandles获取所有的窗口句柄
Set<String> handles = webDriver.getWindowHandles();
String target_handle = "";
for(String handle:handles) {
    target_handle = handle;
}
//通过句柄切换
webDriver.switchTo().window(target_handle);
  • 截图
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com/");
//搜索"软件测试"
webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
webDriver.findElement(By.cssSelector("#su")).click();
sleep(3000);
//截图,并保存
File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("D://20230521jietu.png"));

以上就是本篇博客的所有内容,感谢你的阅读
如果觉得本篇文章对你有所帮助的话,不妨点个赞支持一下博主,拜托啦,这对我真的很重要。
在这里插入图片描述


http://www.kler.cn/news/357236.html

相关文章:

  • Linux——Harbor: 容器镜像的存储
  • Linux Docker配置镜像加速
  • 【Unity(2)】unity开发的基本框架和经典的 MVC 架构模式
  • 深⼊理解指针(2)
  • Android Framework AMS(07)service组件分析-1(APP到AMS流程解读)
  • C++深入探寻二叉搜索树:数据管理的智慧之选
  • uniapp,获取头部高度
  • Elasticsearch 实战应用与优化策略研究
  • 一些关于FMEA在供应链风险管理中的实际应用案例_SunFMEA
  • 游戏逆向基础-找释放技能CALL
  • 【工具】使用perf抓取火焰图
  • 【4046倍频电路】2022-5-15
  • freeswitch-esl 实现保持通话功能
  • 微服务架构 --- 使用RabbitMQ进行异步处理
  • presence_of_element_located() takes 1 positional argument but 2 were given
  • [LeetCode] 542. 01矩阵
  • Frp 在云服与内网服务器间实现端口映射
  • 【Codeforces】CF 2009 F
  • 【云原生】Docker 部署 Nacos使用详解
  • DFS算法经典题目: Leetcode 51.N皇后