i am not able to display the region after find failed

Asked by azhar

I have written logger class as mentioned on this link
 https://github.com/imikemo/Sikuli-and-Robot-Framework-Integration/blob/master/calctest/CalcLib/logger.py
when test case fails I am writing tat result to a file results.doc. My failed function looks like this

def failed(self, msg, *args, **kwargs):
      if self.isEnabledFor(logging.DEBUG):
             if len(getLastFoundImages()) != 0:
                    # source image
                    self.html_img("Source Image", common.cfgImageLibrary + '/' + getLastFoundImage())
      # screenshot
     self.screenshot()
    #addFoundImage("1") #for forcing log to take screenshot of screen
    _lastFoundRegion = (0,0,1280,1024)
   # collectAllLogs()
   if len(args) != 0:
          pass
   print "TestCase Failed"
   fout = open('Output\\Result.doc', 'w')
   fout.write("Find Failed : FAIL")
  fout.write("\n")
  getLastFoundRegion()
  raise common.VerificationFailed(msg)

I am not getting the scrrenshot and the region for failure. my sikuli wrapper class is similar to this link:
http://blog.mykhailo.com/2011/02/how-to-sikuli-and-robot-framework.html

I need to write into the Result.doc when failure occurs and also need to take a screenshot of the whole screen when FIND Failure occurs.

thanks in advance

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

What you are trying to do is in my opinion not suitable for development in Sikuli IDE.

You should use a mature Python/Jython capable IDE like Eclipse, Netbeans or others, to be able to debug your stuff.

Looking into the mentioned logger.py, I do not see any reason, why that should not work with Sikuli (which is not the complexity driver here)

BTW: with newer versions of Sikuli, the import statement should read
from sikuli import *

2ndBTW: I guess Mykhailo Moroz did not use Sikuli IDE, when developing his stuff ;-)

But why this complexity?

If something fails in your script, you should know the region, you are currently working with.

In a failure_reporting def(), use the 3 liner from the _screenshot def() and then simply print this to a file:
"failed in %s with screenshot %s"%lastRegion, screenshot_filename

3rdBTW: another rather easy approach is to use Python's unit testing together with the HTMLTestRunner (see faq 1804)

Revision history for this message
azhar (azharktcs) said :
#2

thanks RaiMan I ll try that.

Revision history for this message
azhar (azharktcs) said :
#3

hi,

I was out of town for a while. it solved my problem partially, by taking a screenshot when failure occurs.
But when image is not found I am not able to get the screen shoe still. I mean when image is not found it goes to sikuli wrapper class and if click operation has failed then in click def it should report failure of that image with a screen shot but this is not happening. the click def is like this :

def click(self, target, modifiers=0):
      setLogEntry("CLICK: " + str(getFilename(target)))
try:
      if isinstance(target, str) or target.__class__.__name__=="Pattern" or isinstance(target, basestring):
                        self.log.html_img("Clicking...", common.cfgImageLibrary + '/' + getFilename(target))
     else:
 if target != None:
  self.log.screenshot(msg="Click Region...", region=(target.x-100, target.y-100, 200, 200))
     return SikuliRegion.click(self, target, modifiers)
except FindFailed, e:
     if isinstance(target, str) or target.__class__.__name__=="Pattern" or isinstance(target, basestring):
 self.log.html_img("Find Failed", common.cfgImageLibrary + '/' + getFilename(target))
 fout = open('\\Output\\Result.doc', 'a')
 fout.write("\n")
 fout.write("Find Failed : FAIL")

   else:
          if target != None:
                     self.log.screenshot(msg="Find Region Failed", region=(target.getX()-100, target.getY()-100, 200, 200))
   self.log.screenshot(msg="Region", region=(self.getX(), self.getY(), self.getW(), self.getH()))
   raise e

I am trying to take a screenshot on find failure of image when click operation fails
and also writing that failure into a file result.doc
both these are not working. can u please tell me the reason.
thanks in advance.

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

There are some non-Sikuli functions, that might produce errors.

On the other hand: this is far too complicated:

img = "targetimage.png"
pat = Pattern(img)

def click(self, targetImage, targetPattern, modifiers=0):
    # now you can use targetImage, whereever you need the filename
    # if Pattern is not null, it is used for finding,otherwise the image
    target = targetPattern if targetPattern else targetImage
    if exists(target):
        click(getLastMatch())
    else:
        #not found actions

Revision history for this message
azhar (azharktcs) said :
#5

I didn't get you. which are the non-sikuli functions ?

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

getFilename(target)
screenshot(...

and not clear, what this is:
SikuliRegion.click(s...

Revision history for this message
azhar (azharktcs) said :
#7

I wrote all of them as in the link and added my changes to them
https://github.com/imikemo/Sikuli-and-Robot-Framework-Integration/blob/master/calctest/CalcLib/logger.py

the SikuliRegion.click(self, target, modifiers) is as written here...
http://blog.mykhailo.com/2011/02/how-to-sikuli-and-robot-framework.html

#get a unique name to the screenshot takken
def _get_unique_name(self, prefix="", suffix=""):
                       now = datetime.datetime.now()
 return prefix + now.strftime('%Y-%m-%d_%H-%M-%S') + suffix

# to take a screen shot of entire region and folder where the screenshot is to be saved
def screenshot(self, msg="", folder="results/screenshots/", region=(0,0,1300,1070)):
 name = self._get_unique_name(suffix=".png")
 img_src = capture(*region)
 shutil.copy(img_src, folder + name)
 self.html_img(msg, folder + name)

# return filename from pattern's target object
def getFilename(target):
          try:
 filename = target.getFilename()
         except:
 filename = target
         return filename

sorry for replying so late.. I don't have proper internet connection

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

as already mentioned in comment #1:
- should work
- not debuggable in Sikuli IDE

The only approach with Sikuli IDE is to check step by step bottom up each function separately that it works.

... or use a mature IDE with Jython support for debugging.

Can you help with this problem?

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

To post a message you must log in.