Java自动化测试(特殊元素处理 26)

时间:2022-07-24
本文章向大家介绍Java自动化测试(特殊元素处理 26),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

时间控件

可输入

driver.get("https://www.fliggy.com/?ttid=seo.000000574&seoType=origin");
WebElement element = driver.findElement(By.xpath("//div[@class='search-field']//div[@class='calendar-input-wrap']//input[@placeholder='yyyy-mm-dd']"));
element.sendKeys("aaa");

飞猪

不可输入

html中有readonly的时间控件

readonly

方案一:在控件弹框中点击

driver.get("https://www.12306.cn/index/");
driver.findElement(By.id("train_date")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//div[@class='cal']//div[8]")).click();

12306

方案二:使用js修改页面

driver.get("https://www.12306.cn/index/");
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("document.getElementById('train_date').removeAttribute('readonly')");
WebElement train_date = driver.findElement(By.id("train_date"));
train_date.clear();
Thread.sleep(1000);
train_date.sendKeys("2020-11-11");
Thread.sleep(3000);

executeScript定位元素的方法

不传参

可以全部使用JavaScript的语法定位

jsExecutor.executeScript("document.getElementById('train_date').removeAttribute('readonly')");

传参

也可以使用定位到的元素来进行定位

driver.get("https://www.12306.cn/index/");
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
WebElement train_date = driver.findElement(By.id("train_date"));
jsExecutor.executeScript("arguments[0].removeAttribute('readonly')", train_date);

可以替换多个内容

jsExecutor.executeScript("arguments[0].removeAttribute(arguments[1])", train_date, "readonly");

常见使用场景

  • 设置,去除元素属性
  • 页面滚动
windows.scrollTo(0,document.body.scrollHeight) 滚动到页面最底部
Element.scrollIntoViewIfNeeded() 滚动到指定元素的位置

文件上传

直接输入

测试页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
<form>
    <input type="file" name="" id="file"/>
</form>
</body>
</html>
driver.get("src/test/resources/file.html");
driver.findElement(By.id("file")).sendKeys("aaa");

无法直接输入

针对不是 input 类型的元素,我们可以使用第三方的自动化工具,比如:Auto,对 windows 控件元素进行操作

以下是其官网介绍:

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys).

翻译过来就是:

AutoIT 是类似于 Basic 脚本语言的免费软件,利用它我们可以实现对 windows 的 GUI 界面进行自动化操作,balabala…

官网地址:https://www.autoitscript.com/site/autoit/

强烈建议先去看官方文档:https://www.autoitscript.com/autoit3/docs/,对工具的使用和脚本编写语法描述的非常详细

step1:下载安装

下载页面在这里:https://www.autoitscript.com/site/autoit/downloads/

点击下载即可,下载完下一步直到安装完毕

image.png

安装完毕会有如下几个应用:

image.png

其中我们用得到的有:

  • AutoIT Window Info 识别 Windows 元素信息
  • Complie Script to .exe 将 AutoIT 编写的脚本编译成 exe 可执行文件
  • Run Script 运行 AutoIT 脚本
  • SciTE Script Editor 编写 AutoIT 脚本

注意:官方推荐使用 X86 版本,这样兼容性问题会少些

step2:使用 AutoIT

  1. 将上传的 Windows 窗口打开
  2. 打开 AutoIT Window Info 工具,Finder Tool 下的图标一直按住,选择窗口中要识别的元素(文件名后面的输入框以及打开按钮),分别记录下此时的 Tile、Class 等信息

image.png

  1. 打开 SciTE Script Editor,开始进行脚本编写(注意元素的定位是由 Class 和 Instance 进行拼接的,如 Class 为 Edit,Instance 为 1,那么定位表达式为 Edit1)
;等待“打开”窗口
WinWaitActive("打开")
;休眠2秒
Sleep(2000)
;在输入框中写入上传文件的路径
ControlSetText("打开", "", "Edit1", "d:java_autotest.png")
;休眠2秒
Sleep(2000)
;点击打开按钮
ControlClick("打开", "","Button1");
  1. 选择工具栏上面的 Tools-Go 先去运行下脚本,试运行 OK 之后将脚本保存,后缀为 au3
  2. 选择 Complie Script to .exe 工具把脚本编译为 exe 文件
  3. Java 代码本地执行 exe 文件
ChromeDriver driver = new ChromeDriver();
driver.get("D:\fileupload.html");
Thread.sleep(2000);
driver.findElement(By.id("fu")).click();
//Java运行时对象
Runtime runtime = Runtime.getRuntime();
try {
    //执行
    runtime.exec("D:\upload.exe");
}catch (IOException e){
    e.printStackTrace();
}

运行效果

image.png

验证码

  • 去除验证码-测试环境
  • 自动识别,OCR
  • 万能验证码,在后端开一个后门