问题
I have a testing server which runs headless. One test I want is to check that an image served off a particular URL matches some reference image.
Is there an API in Sikuli which can directly accept an image as a stream and compare it with some other image taken from the local resource file? Unfortunately, there is no complete tutorial on Sikuli's Java API, all I've found is tutorials that assume that there is a display available.
I'll be glad to see any examples or at least links to the needed parts of Sikuli javadocs. Also, suggestions for other approaches are welcome.
回答1:
To use Sikuli you need
- A base image on which the other image will be searched.
- The image which will be searched within the other image.
If image 1 is your local resource image, you can create a org.sikuli.Finder instance with the path to the image and the Region of this image which will be searched. Example (java level):
finder = new Finder("path/to/image", new Region(0, 0, <imgwidth>, <imgheight>));
If image 1 is your stream, you have to make a BufferedImage out of it somehow (I do not know the best way to do this). Then you can make a org.sikuli.ScreenImage from this BufferedImage with the help of an java.awt.Rectangle and an org.sikuli.Region.
finder = new Finder(new ScreenImage(new Rectangle(0,0,<imgwidth>,<imgheight>), bufferedImage), new Region(0,0,<imgwidth>,<imgheight>))
After you created the finder from image 1, you can search image 2 within this image.
Again, you have two possibilities. If the second image is your local resource image, you can create an org.sikuli.Pattern object with the file location:
pattern = new Pattern("path/to/image.png");
Else, if this is your stream, you have to make a BufferedImage out of the stream somehow. You can then create a pattern from this image:
pattern = new Pattnern(bufferedImage);
As a last step, you can now run the finder to search for the pattern:
finder.find(pattern);
You can check if the finder found anything with:
finder.hasNext();
And you should be able to iterate all findings with:
for (Match m : finder):
//do something with the match
I hope I could help you although your question is already some weeks old.
回答2:
Below code helps for asserting images
//take screenshots
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
//copy it some location
FileUtils.copyFile(scrFile, new File("C:\\screenshot.png"));
Finder f = new Finder("C:\\screenshot.png");
System.out.println("abc");
f.find("C:\\chrome3.png", 0.95);
while(f.hasNext()){
System.out.println("found");
Match m= f.next();
f.destroy();
}
}
catch (IOException e)
{
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/13229705/is-it-possible-to-use-sikuli-to-assert-that-images-are-the-same-in-gui-less-mode