assert not exists keeps failing even though really doesn't exist

Asked by Shawn Robertson

my IDE keeps failing on this line

assert not exists(Pattern("IDNew-2.png").similar(0.81))

even though the image really doesn't exist.

Basically that image does exist and then i perform a save and the image no longer exists because it changes from ID = NEW to ID = 1.

even if click on the image and try all ranges of similarity it still fails.

makes no sense why if the image doesn't exist it still fails.

I do use assert not exists in other scripts and it seems to work fine so I am not sure why i'm having so much trouble with this particular one.

Question information

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

I think we already talked before on these kind of "magics"

For some reason, Sikuli finds something like IDNew-2.png on the screen.

Might be a "bad" image (one, that does not represent the key visual aspects, that would make IDNew-2.png to not be found).

The only chance to debug this:

if exists("IDNew-2.png"):
    getLastMatch().highlight(3)
    assert False
else:
    assert True

and look, what Sikuli is finding.

Revision history for this message
Shawn Robertson (shawn-robertson) said :
#2

i ran my script and it failed on that line again.

i didnt change anything and just opened up a new empty script and copied in

if exists("IDNew-2.png"):
    getLastMatch().highlight(3)
    assert False
else:
    assert True

since ID = 4 was present nothing flashed or highlighted and the message area at the bottom remained empty.

i opened up a new window that contained ID = NEW and ran that blurb again and a red box flashed 3 times around the ID = NEW area.

this tells me your snippet of code works but still leaves me wondering why if your code can detect it fine why cant the simple assert not exist detect it.

i may just try to work around this by asserting something else.

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

-- but still leaves me wondering why if your code can detect it fine why cant the simple assert not exist detect it.
you did not test this case in the "blurb" situation!

So do it again this way:

if exists("IDNew-2.png"):
    getLastMatch().highlight(3)
    assert exists("IDNew-2.png")
else:
    assert not exists("IDNew-2.png")

This should not give an assertion exception in any case you told above.

I do not see any sense to "workaround".
You have to understand and solve this, otherwise a bit later, you get another similar situation and again have to workaround.

The fact that it worked in the clinical situation above, only tells, that there is some problem in your real workflow.
So make the tests with this real workflow.

Revision history for this message
Shawn Robertson (shawn-robertson) said :
#4

i figured out what it was... i didn't have a wait() between the the previous action and the following assert exists() so the assertion was trying to assert before the previous action completed.

i put in a wait(.5) and it now works as expected.

Many thanks for the help as always Raiman!

Revision history for this message
Shawn Robertson (shawn-robertson) said :
#5

Thanks RaiMan, that solved my question.