How to make find robust - another example

Asked by Beth Griffin

I am testing a Visual Studio plug-in, and want to make sure a debug session stops at the correct location. What I am finding is that if I do NOT sRegion.highlight(2) first, sRegion.exists(myImage) returns a match at the wrong location. Is there something I'm missing that highlight does?

        # look for source tab and make sure it has focus
        if not exists("SobelFilterKernelSourceTab.png"):
            SFTest.updateFailures(messagePrefix, "debugger did not stop at the correct location", "testBreakpoint")
            status = 0
        else:
            tab = Region(getLastMatch())
            click(tab)
            # set the source view to the expected break line
            type("g", KEY_CTRL)
            type("103\n")
            # narrow down search region
            sRegion = tab.below()
            # sRegion may not contain where we really want to search; blame MS Visual Studio
            # making region wider
            oldX = sRegion.getX()
            newX = oldX - 200
            oldW = sRegion.getW()
            newW = oldW + 400
            sRegion.setX(newX)
            sRegion.setW(newW)
            # confirms that I've got the right search region
            sRegion.highlight(3)
            # look for the line number in sRegion
            # this works if I do the above highlight, but gets a wrong match if I don't
            # Image is just white background and blue number 103
            # looking for the text "103" seems to get the wrong match, too
            sLine = sRegion.exists("SobelFilter_103_sobel_filter.png",10)
            if not sLine:
                SFTest.updateFailures(messagePrefix, "Sikuli did not find the breakpoint location", "testBreakpoint")
                status = 0
            else:
               # just making sure I got the right line
                sLine.highlight(3)
                # codeRegion is in the source code view
                codeRegion = sLine.right(300)
               # arrowRegion is where VS displays the Next Statement arrow
                arrowRegion = sLine.left(75)
               # just making sure I made the regions the right size
                codeRegion.highlight(3)
                arrowRegion.highlight(3)
                print "Checking for arrow"
                if not arrowRegion.exists("NextStatementArrow.png"):
                    SFTest.updateFailures(messagePrefix, "debugger stopped at an unexpected source line", "testBreakpoint")
                    status = 0
                print "Checking for source text"
                if not codeRegion.exists("{", 5):
                    SFTest.updateFailures(messagePrefix, "debugger stopped at the wrong source line", "testBreakpoint")
                    status = 0

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
Beth Griffin (beth-griffin) said :
#1

Hmmmm

I played around and narrowed the sRegion even more, and now I don't need to highlight first, but it was odd that with the larger region highlight made a difference.

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

... but the false match should have been in sregion too !?
if not, this would really be some bug.

So I guess it was in sregion: might have been a timing problem, that does not happen with the additional waiting time produced by the highlight.

And this finally confirms: for critical matches it is vital to have:
1. a capture that concentrates on the region, that makes it really distinct (as little background as possible)
2. narrow the search region as much as possible

From the last question about your situation I know, that the MS VS GUI is very bubbly ;-)

Since I do not think, that highlighting a region has any effect on a subsequent find operation in the highlighted region, besides adding additional waiting time, it would be helpful, to confirm this (without confirmation, this could lead others in the wrong direction ;-)

Revision history for this message
Beth Griffin (beth-griffin) said :
#3

What I thought was the solution actually wasn't. In the end, I think the real fix was doing

if not sRegion.exists(Pattern("SobelFilter_103_sobel_filter.png").similar(0.95)):
    ...

If I ran things repeatedly, sometimes the match was right, other times it wasn't. So far, using Pattern.similar seems to give me consistent results.

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

Ok, forgot to mention above:

And this finally confirms: for critical matches it is vital to have:
1. a capture that concentrates on the region, that makes it really distinct (as little background as possible)
2. narrow the search region as much as possible
3. use Pattern().similar(value) with values near 1.0 (to be tested)

Revision history for this message
Beth Griffin (beth-griffin) said :
#5

Thanks RaiMan, that solved my question.