[Java] JUnit: How create assertTrue: Image exists or not ?

Asked by Madhu

I have Sikuli, Junit, ANT working fine.

Now I need assertTrue as part of each test case. Otherwise all test cases are passing.
I can assert whether an image exists or not? Or I can check if some text exists or not.
Since I am using it in Eclipse Junit envionronment I need the code in Java.

1. How to check whether an image exists ?
2. How to use assertTrue statements ?

I like Sikuli and tests are running. But all tests pass because there is no checkpoint. I need assertTrue as part of this. How to assert an image exists. I could not find java code for this.

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Supposing you are fully on the Java level:

use
Match m = Screen.exists("some-image.png")
or
Match m = Region.exists("some-image.png")

m is set to null, if the image could not be found.

Revision history for this message
Vince (melekal) said :
#2

Can I ask for some clarification on this answer?

I'm in the same boat here and using match is always coming back with a match whether or not the image appears on the screen. I never get a null result. My console is always showing results such as:

Match[247,102 157x16] score 1.00, target=center

Revision history for this message
RaiMan (raimund-hocke) said :
#3

the height of the match lets me think you are searching for some text on the screen?

If it is really an image, you are looking for, than in this case it is there at 247,102 157x16.

(exception: totally black and totally white images, there we have a bug)

So what are you doing?

Revision history for this message
Vince (melekal) said :
#4

I'm using Sikuli in Eclipse to simply double click on an icon and verify the application launches. I've purposely set it up to verify a completely incorrect image comes up as I want to see it fail but it never does. It launches the application correctly but always passes the ensureExists method no matter what image I try to match.

I created the following method to verify the failure as the following:

public boolean ensureExists(String image) {

 Match m = s.exists(image);
 System.out.println("Result of m is " + s.exists(image));

 if (s.exists(image) != null) {
  return true;
 }
 return false;
}

This is the test that pulls in that method

Screen s = new Screen();

@Test
public void testLaunchApp() throws FindFailed {
 s.doubleClick(img, 0);
 Assert.assertTrue(ensureExists(screenshot));
}

Revision history for this message
RaiMan (raimund-hocke) said :
#5

No idea, seems ok.

But I would write it:

public boolean ensureExists(String image) {

 Match m = s.exists(image);

 if (m != null) {
 System.out.println("Result of m is " + m);
 return true;
 }
 return false;
}

Only to be sure: screenshot is a string containing a filename ending with .png?

Revision history for this message
Vince (melekal) said :
#6

Yes the image name is a filename ending with .png extension.

Strange enough after modifying my code to match what you wrote it all started failing. After putting it back it to what I had it now works correctly with the right things passing and the wrong things failing. When that starts happening I feel a case of testers tourrets coming on.

Thanks for the help.

Can you help with this problem?

Provide an answer of your own, or ask Madhu for more information if necessary.

To post a message you must log in.