exists function not working after defining region

Asked by Alex Yi

Here is my code:
    def test_36VerifyCycleStockValueFromAllBar(self):
        getLastMatch().highlight(2)
        CurrentBarRegion = Region(479,476,80,199)
        if CurrentBarRegion.exists:
            CurrentBarRegion.hover("CurrentCycleStockBarFromALL.png")
            CurrentBarRegion.getLastMatch().highlight(2)
            if exists(Pattern("CurrentCycleStockHoverValue.png").similar(0.80)):
                wait(1)
                print getLastMatch()
                getLastMatch().highlight(2)
            else:
                popup("It's failing for weird reason. Why?")
                print getLastMatch()
                getLastMatch().highlight(2)
        assert (exists(Pattern("CurrentCycleStockHoverValue.png").similar(0.80)))

I have the exact same code elsewhere and they seem to work fine and they have similarity level at 95 percent.
But this one eventhough CurrentCycleStockHoverValue.png exists it keeps failing. I'm not even getting the lastMatch so it seems like it's not doing anything. I have no idea why this behavior is occuring. When I click on the image to set the similarity level, I see there is one match even at 99% level but when I run the test, it fails.

Any idea?

Thanks,

Alex Yi

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
j (j-the-k) said :
#1

Apparently the image ist not found on the screen.
I cannot see a reason why though, your script seems to be okay.
Are you using more than one screen maybe?

Unrelated to your Problem: "if CurrentBarRegion.exists:" makes no sense since this will never be False because "exists" is a method.

Revision history for this message
Alex Yi (ayi) said :
#2

I don't either. When I click the image from sikuli IDE though and try to change the similarity level, it shows me with the match. IT is just weird.

I have that if region.exists statement because I was trying to debug this issue. Was wondering if the region that I defined was not getting set correctly or something.

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

This is valid Python, but does not make any sense:

        CurrentBarRegion = Region(479,476,80,199)
        if CurrentBarRegion.exists:

--- CurrentBarRegion.exists
only returns the method object of the method exists, which is always true, since not null.

exists() always needs an image or a pattern to search for in the given region CurrentBarRegion.

and CurrentBarRegion "exists" in that sense, that you just created a Region object using Region(479,476,80,199), which is just a rectangle somewhere on the screen, which does not know anything about its visual content.

BTW: it seems, you are searching inside CurrentBarRegion, but
exists(Pattern("CurrentCycleStockHoverValue.png").similar(0.80)) searches the whole screen.

At this point I agree with j-the-k: for some reason the image CurrentCycleStockHoverValue.png is not found in that moment.

this should be enough, to get nearer to the reason:

   def test_36VerifyCycleStockValueFromAllBar(self):
        CurrentBarRegion = Region(479,476,80,199)
        test = None
        if CurrentBarRegion.exists("CurrentCycleStockBarFromALL.png"):
            CurrentBarRegion.getLastMatch().highlight(2)
            test = exists(Pattern("CurrentCycleStockHoverValue.png").similar(0.80))
            if test: test.highlight(2)
        assert test # only for debug
# assert (exists(Pattern("CurrentCycleStockHoverValue.png").similar(0.80)))

Revision history for this message
Alex Yi (ayi) said :
#4

It is so weird. The same issue is happening on the other dashboard that I am testing.
Here are some sime codes:
#test_010 - Verify datapoint for EIO Target
    #@unittest.skip("Skipping for Now!")
    def test_010VerifyEIOTargetHoverValuefor80SL(self):
        getLastMatch().highlight(2)
        DataPointRegion = Region(602,669,23,18)
        hover(DataPointRegion.getLastMatch())
        assert exists("EIOTargetsValueFor80SL.png")

#test_011 - Verify datapoint for EIO Target
    #@unittest.skip("Skipping for Now!")
    def test_011VerifyEIOTargetHoverValuefor85SL(self):
        getLastMatch().highlight(2)
        DataPointRegion = Region(731,520,22,18)
        if DataPointRegion.exists("EIOTargetDataPoint.png"):
            hover(DataPointRegion.getLastMatch())
        assert exists(Pattern("EIOTargetsValueFor85SL.png").similar(0.95))

Both of them are not working for some reason.
The top one I am hovering over to a point in a region using just the region where the second one I created a region and then I'm hovering over to an image within that image. Here is the link to the screenshots of these image.
https://www.dropbox.com/sh/dk2it0o2so8fso5/hlRe6raDv8

You'll notice two screenshots are the image in my assert statements from two test cases above.
The whole dashboard image shows the whole dashboard and you'll notice there are two graphs with 5 datapoints on each. The top graph(blue line) has little star like image that when you hover over it pops out an image with dollar value. Basically, I'm trying to test that image so that image contains the correct value. As you can see those two images are identical, however, my test is failing even with defaul similarity level.

The weird things is this is happening on random cases.
I have no idea why.

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

I just made a test with the image EIOTargetsValueFor80SL.png on the screenshot WholeDashboardRegionHoveredOverToDataPoint.png and I get similarities between 0.95 and 1.0.

Even the image EIOTargetsValueFor85SL.png still matches with 0.72 on the same screenshot.

So I do not understand your problem.

What I cannot check are your absolute regions, which I never use.
I always try to define regions relative to some other fixed aspects of the screen.

Since you are driving tests for an app, you should know the relative position of the area, where you expect the popup to appear.
You need a concept for the different areas of your app window.

Revision history for this message
Alex Yi (ayi) said :
#6

That's exactly what was happening to me too.
It only fails when the test is ran and randomly(sometimes works and sometimes it doesn't)
I will need to play around with it.

Revision history for this message
Alex Yi (ayi) said :
#7

OK,

I've figured out one of the problem The one with the EIOTargetValueFor80SL.png is working now. It's kind of stupid because the reason it was failing was because of the previous test case. In my previous test case, I'm doing the same kind of test on the point right next to this one and when I hover over to that point, that popup was covering the image that I was looking for in the next test case. So I just added a hover(someotherimage) statement to put the mouse pointer somewhere else to make that popup dissappear. So now, that part is working.

However, there is still the original issue that I don't understand and can't seem to figure out. I've tried the following and narrowed the issue down and I guess I need explanation on why this behavior is occuring:
1. recaptured the comparison image
2. put a wait statement
3. hover over to somewhere else then come back
4. Put a if exists statement and then print out the similarity level of that image and then highlight that image before assertion.

So here is my code:
    def test_063VerifySafetyStockHoverValueForComparisonDollar(self):
        getLastMatch().highlight(2)
        hover("ScenarioComparisonSummaryLabel.png")
        if exists(Pattern("ComparisonSafetyStockBarDollar.png").similar(0.98)):
            hover(getLastMatch())
            wait(3)
        if exists("ComparisonSafetyStockHoverValueDollar.png"):
            print getLastMatch()
            getLastMatch().highlight(2)
        assert exists(Pattern("ComparisonSafetyStockHoverValueDollar.png").similar(0.96))

Basically, the print statement returns random similarity levels between 0.78 - 1.00. And thus, the assertion statement passes sometimes and fails sometimes. This image is a straight screenshot and so it should be at least .99 in similarity level.
I have screenshots in the following dropbox link. Basically, the whole image screenshot is the whole image with red bar on the right is the bar that I am testing when hovered.
https://www.dropbox.com/sh/dk2it0o2so8fso5/hlRe6raDv8
Any idea on this random behavior?

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

Since the highlight is animated and takes some time to vanish (which happens asynchronously), you have to either
- forego the last highlight() and rely on the print (highlight() + print does not make sense anyway, because the highlight() itself prints a log message with the same information)
- put a wait(0.5) (should be enough, but test the correct value) after the getLastMatch(), when you repeat the same search directly afterwards
- use an alternative for the assertion:

principally:
if exists():
    highlight()
    assert True
else:
    assert False

Revision history for this message
Alex Yi (ayi) said :
#9

Still same results. Sometimes works and sometimes it doesn't.
I'll just have to leave it at 75% similarity level for now and figure it out later.

Thanks for the proposal.

sincerely,

Alex

Can you help with this problem?

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

To post a message you must log in.