首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动化测试上传文件之Sikuli图片识别代替AutoIT

自动化测试上传文件之Sikuli图片识别代替AutoIT

作者头像
软测小生
发布2019-07-05 15:15:39
1.5K0
发布2019-07-05 15:15:39
举报
文章被收录于专栏:软测小生软测小生
自动化过程中对于Windows弹出框,一般使用AutoIT制作脚本进行操作, 之前写过章介绍,可以复习一下:

Java+Selenium2+AutoIt实现右键文件另存为功能

上传也是同样的方法,使用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或者其他工具会更胜一筹。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软测小生 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Java+Selenium2+AutoIt实现右键文件另存为功能
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档