上传也是同样的方法,使用AutoIt去生成exe脚本操作Windows窗体。
Sikuli 上帝之眼的意思。http://www.sikulix.com/
这里介绍一下另外一种解决方案:使用Sikuli进行图片识别,来对Windows弹出框或者其他Windows窗口进行一系列的操作。
原理:在当前页面中识别目标图片,并对目标图片进行点击、输入、等待显示、判断是否存在等操作。
流程:
1、识别文本输入框,并输入文件名;
2、识别Open按钮,点击Open按钮。
依赖包:
sikulixapi 1.1.2
bridj 0.7.0
语言:Java/Python等
Maven配置如下:
<!-- https://mvnrepository.com/artifact/com.sikulix/sikulixapi -->
<dependency>
<groupId>com.sikulix</groupId>
<artifactId>sikulixapi</artifactId>
<version>1.1.2</version>
<exclusions>
<exclusion>
<groupId>com.github.vidstige</groupId>
<artifactId>jadb</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.nativelibs4java/bridj -->
<dependency>
<groupId>com.nativelibs4java</groupId>
<artifactId>bridj</artifactId>
<version>0.7.0</version>
</dependency>
Demo code:
package com.xxx.sikuli;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.openqa.selenium.chrome.ChromeDriver;
public class SikuliDemo {
public static void main(String[] args) throws FindFailed, Exception {
//浏览器版本与Driver版本要对应,不然会报错,无法启动
System.setProperty("webdriver.chrome.driver", "src/test/resources/driver/chromedriver.exe");
String filepath = "src/test/resources/";
String inputFilePath = "src/test/resources/";
Screen screen = new Screen();
Pattern fileInputTextBox = new Pattern(filepath + "FileTextBox.png");
Pattern openButton = new Pattern(filepath + "OpenButton.png");
WebDriver driver;
// Open Chrome browser
driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/upload/");
// Click on Browse button and handle windows pop up using Sikuli
driver.findElement(By.xpath(".//*[@id='uploadfile_0']")).click();
screen.wait(fileInputTextBox, 20);
screen.type(fileInputTextBox, fileInputTextBox.getFileURL().toString());
screen.click(openButton);
// Close the browser
driver.close();
System.out.println("**********1 file has been successfully uploaded.**********");
}
}
总结:
其实Sikuli还可以用作一些其他的图片识别的自动化测试,但是Sikuli对分辨率有一定的要求,基于像素识别,所以指定要找的目标图片很少变动时就可以使用Sikuli,像上面例子中上传文件文本输入框和Open按钮变动就基本不变;但是对于多变动的目标图片还是不适合使用Sikuli,因为变动一次就需要重新截图,对于多变的情况下 使用AutoIT或者其他工具会更胜一筹。