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.
Sikuli IDE provides two methods to capture screen images. The first method is to click on the camera button in the toolbar. This will bring you to the screen capturing mode. The second method is to press a hot-key (Ctrl + Shift + 2).
SikuliX uses Python as the scripting language. Python scripting is supported by the Sikuli-IDE. Sikuli's features are also available in Java programs. Sikuli is a visual approach to search and automate graphical user interface using screenshots.
Go to properties->java build path->Add external jar->Select the path of “sikulixapi” jar file->click on apply button. Below example is to automate windows based calculator operations. Sikuli identify windows elements by identifying calculator , buttons we are performing multiplication here.
Execute a mouse double click action on a target. To define a double-click action, write the word double click in a text box. You can also write aliases including double-click or doubleclick . Then, draw a rectangle around the target.
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");
}
The error message says that is the program looking a .PNG file, and in your code your are putting a .JPG file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With