码同学抖音 :小码哥聊软件测试
在UI自动化中,我们经常会遇到上传文件操作。处理上传事件是一个比较麻烦的操作,因为点击上传控件会弹出Windows窗口供用户选择文件,但是Windows窗口是浏览器之外的组件,所以selenium本身无法处理这个windows窗口。这里给大家几个处理思路,我们先看一下下面这个HTML。
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />
<title>test</title>
<script language=”javaScript”>
function toAlert()
{
alert(“hello continue…”);
}
</script>
</head>
<body>
<form>
<table >
<tr>
<td>
<input name=”file” type=”file”/>
</td>
</tr>
</table>
</form>
</body>
</html>
用notepad++ 打开,将它存成一个 autotest.html文件,打开之后,只有一个上传按钮,我们来看一下怎么处理上传事件。
1
直接调用selenium自带的sendkeys进行操作,将需要上传的文件路径直接传递进上传控件。
2.import org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
/**
* Description:
* Author: ChrisMa
* Date: 2019-05-15
*/
public class testupload2 {
public static void main(String ags[]) throws InterruptedException {
//初始化webdriver
WebDriver driver = new ChromeDriver();
//打开本地html
driver.get(“
file:///D:/UI/autotest1.html”);
// 将文件所在路径传递给上传文件控件
driver.findElement(By.name(“file”)).sendKeys(“D:text.txt”);
// 等待看到结果
Thread.sleep(10000);
//关闭webdriver
driver.quit();
}
}
这种方案可以解决大部分的上传操作,可是对于一些上传框禁止输入的就无法操作了,这时候我们就要考虑其他方案。
免费领取 码同学软件测试 课程笔记+超多学习资料+完整视频+最新面试题,可以转发文章 + 私信获取资料哦
2
针对无法输入的,我们可以考虑采用AutoIT来进行上传。
AutoIt目前最新是v3版本,这是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows GUI(图形用户界面)中进行自动化操作。它利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动化任务。
官方 站:
https://www.autoitscript.com/site/
从 站上下载AutoIt并安装,安装完成在菜单中会看到下图的目录:
AutoIt Windows Info 用于帮助我们识Windows控件信息。
Compile Script to.exe 用于将AutoIt生成 exe 执行文件。
Run Script 用于执行AutoIt脚本。
SciTE Script Editor 用于编写AutoIt脚本。
我们打开html 页,然后点击一下上传按钮:
下面我们看一下怎么用autoIT来处理这个上传。
? 打开AutoIT Window Info,然后点击Finder Tool,不松开左键,等鼠标变成瞄准器类型时,将鼠标挪动到文件上传框需要识别的控件上松开左键。
识别出对象之后所有的信息会显示在AutoIT windows info里
经过识别窗口的title为“Open”,标题的Class为“#32770”。
文件名输入框的class 为“Edit”,Instance为“1” ,所以ClassnameNN为“Edit1”。
打开按钮的class 为“Button”,Instance为“1” ,所以ClassnameNN为“Button1”。
我们打开SciTE Script Editor,然后将下列代码填入:
;ControlFocus(“title”,”text”,controlID) Edit1=Edit instance 1
ControlFocus(“Open”, “”,”Edit1″)
; Wait 10 seconds for the Upload window to appear
WinWait(“[CLASS:#32770]”,””,10)
; Set the File name textmargin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; color: rgb(51, 51, 51); font-family: -apple-system-font, system-ui, “Helvetica Neue”, “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei UI”, “Microsoft YaHei”, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; text-align: left; text-indent: 39pt;”>
ControlSetText(“Open”, “”, “Edit1”, “D: est.txt”)
Sleep(2000)
; Clickmargin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; color: rgb(51, 51, 51); font-family: -apple-system-font, system-ui, “Helvetica Neue”, “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei UI”, “Microsoft YaHei”, Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; text-align: left; text-indent: 39pt;”>
ControlClick(“Open”, “”,”Button1″);
将script在SciTE Script Editor保存之后,打开上传窗口,在SciTE Script Editor中选择Tools->go, 来查看一下文件是否可以上传。
确认脚本运行正常, 我们将这个脚本保存成Script.au3, 然后打开Compile Script to.exe,将Script.au3文件转换为Script.exe:
这个时候,我们打开上传文件控件,双击Script.exe文件,可以看到文件上传事件已经处理成功。
免费领取 码同学软件测试 课程笔记+超多学习资料+完整视频+最新面试题,可以转发文章 + 私信获取资料哦
接下来,就是使用java来调用该EXE文件:
import org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!