19
votes

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

Image trying to find 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.

2
You can do this without using Sikuli. Create function in appium which capture screen shot of particular element (what you want to verify ) save it in you system at run time. And match with your base image file using Java code.Sadik Ali
Can you please provide sample code? ThanksElsid
can you post the exact error you're seeing?eis
updated in original postElsid
@SadikAli I am also interested in the way you showed in your comment. Can you please add a sample or link to any (documentation) source ?Slavo

2 Answers

2
votes

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

The error message says that is the program looking a .PNG file, and in your code your are putting a .JPG file.