简体   繁体   中英

finding image in Sikuli X Java

I am having an issue trying to find an image on the screen, I tried doing it in two different ways and it doesn't seem to work for me. I am trying to do this with Appium running on IOS simulator which shows up on the screen, so I don't see this being a problem of a screenshot being taken.

I am running MAC OSX El Capitan I have imported the Sikuli X java API in my project

Do I need to also import the MAC Sikuli Library jar?

This is what I have tried so far:

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.

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

}

I keep getting cannot find errors.

error message:

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

试图找到的图像 This is the image on the screen, The large red rectangle is the image I have created a screenshot for and try to find, but get that error.

The only thing I am able to successfully find is that gray rectangle, or at least it doesn't throw an error for.

You can use this method to verify images:

@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");    
 }

错误消息表明该程序正在查找 .PNG 文件,并且在您的代码中放置了一个 .JPG 文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM