Reputation: 243
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.
Upvotes: 19
Views: 3167
Reputation: 1370
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");
}
Upvotes: 2
Reputation: 1824
The error message says that is the program looking a .PNG file, and in your code your are putting a .JPG file.
Upvotes: 1