在Sikuli X Java中查找图像

19

我在尝试屏幕上找图片时遇到了问题,我尝试了两种不同的方法,但似乎都不起作用。 我正在使用运行在IOS模拟器上的Appium,它显示在屏幕上,所以我不认为这是截图被拍摄的问题。

我正在运行MAC OSX El Capitan 我已经在我的项目中导入了Sikuli X Java API

我需要同时导入MAC Sikuli Library jar吗?

这是我尝试过的:

1.

Screen s = new Screen();
Pattern test = new Pattern("/Users/ealiaj/Desktop/Automation/workspace/WheelsUp - IOS/screenshot.jpg");
try {
    s.find(test);
} catch (FindFailed e) {

}

2.

->

2.

Screen s = new Screen();
try {
    s.find("screenshot.jpg");
} catch (FindFailed e) {

}

我一直收到“找不到”错误。

错误信息:

FindFailed: can not find /Users/ealiaj/Desktop/Automation/workspace/WheelsUp - IOS/screenshot1.jpg in S(0)[0,0 1440x900] Line 2189, in file Region.java

寻找的图像 这是屏幕上的图像,大的红色矩形是我创建截图并尝试查找的图像,但出现了那个错误。

我唯一能成功找到的就是那个灰色矩形,或者至少它没有报错。


1
你可以不使用Sikuli来完成这个任务。在Appium中创建一个函数,它可以截取特定元素(你想要验证的)的屏幕截图,并在运行时将其保存在你的系统中。然后使用Java代码与你的基础图像文件进行匹配。 - Sadik Ali
1
请问您能否提供示例代码?谢谢。 - Elsid
你能发一下你看到的确切错误吗? - eis
@SadikAli,我也对你在评论中展示的方法很感兴趣。你能否添加一个示例或链接到任何(文档)来源? - Slavo
有人知道如何在纯Java或Sikuli中实现这个吗? - Elsid
显示剩余3条评论
2个回答

2
你可以使用这种方法来验证图片:
@Test
public void verifyImages() {    

    //WebElement img = driver.findElementByClassName("android.widget.ImageView");

   //take screen shot
    File screen = ((TakesScreenshot) driver)
                        .getScreenshotAs(OutputType.FILE);


    //capture image of searched contact icon
    List<WebElement > imageList = driver.findElementsByXPath("//*[@class='android.widget.ImageView' and @index='0']");
    System.out.println(imageList.size());

    System.out.println(i);
    WebElement image = imageList.get(1);
    Point point = image.getLocation();

    //get element dimension
    int width = image.getSize().getWidth();
    int height = image.getSize().getHeight();

    BufferedImage img = ImageIO.read(screen);
    BufferedImage dest = img.getSubimage(point.getX(), point.getY(), width,
                                                                 height);
    ImageIO.write(dest, "png", screen);
    File file = new File("Menu.png");
    FileUtils.copyFile(screen, file);

    //verify images
    verifyImage("Menu.png", "Menu.png" );
}



public void verifyImage(String image1, String image2) throws IOException{
    File fileInput = new File(image1);
    File fileOutPut = new File(image2);

    BufferedImage bufileInput = ImageIO.read(fileInput);
    DataBuffer dafileInput = bufileInput.getData().getDataBuffer();
    int sizefileInput = dafileInput.getSize();                     
    BufferedImage bufileOutPut = ImageIO.read(fileOutPut);
    DataBuffer dafileOutPut = bufileOutPut.getData().getDataBuffer();
    int sizefileOutPut = dafileOutPut.getSize();
    Boolean matchFlag = true;
    if(sizefileInput == sizefileOutPut) {                         
       for(int j=0; j<sizefileInput; j++) {
             if(dafileInput.getElem(j) != dafileOutPut.getElem(j)) {
                   matchFlag = false;
                   break;
             }
        }
    }
    else                            
       matchFlag = false;
    Assert.assertTrue(matchFlag, "Images are not same");    
 }

是的,它确实找到了那两张完全相同的图片。我在我的一侧使用它来测试2张图片,它有效。谢谢。 - Gaurav
嗨,Gaurav,为了明确起见,我有一个名为image1的整个屏幕图像和一个名为image2的按钮图像,这将在image1中查找image2? - Elsid
测试一下,如果有问题运行不了,我会帮你的。谢谢。 - Gaurav
谢谢,我只需要使用VerifyImage来纠正单个图像吗?还是我需要同时使用两种方法? - Elsid
嘿,我看到这个代码是截取元素的屏幕截图,如果我已经在本地存储了文件,我该如何使用它呢?谢谢。 - Elsid
显示剩余9条评论

1
错误信息显示程序正在寻找一个 .PNG 文件,而在你的代码中你却使用了一个 .JPG 文件。

抱歉,那是复制的,当我将文件类型更改为 .PNG 时,它实际上是 .PNG。 - Elsid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接