Testing a Delphi desktop app using SikuliX and Python scripting

Asked by CCIOLA

Hi,

Is it possible to do object orientation in SikuliX? I am trying to do a main file get a method from another file but it's not working.

main.py
- - -

import shutil
from geraCPF import geraCPF

class main():
   myApp = App("Chrome")
   App.open("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe")
   myApp.focus()
   while not exists("1520001990649.png"):
      observe()
      wait(1)
   click("1520001996809.png")
   type(str(cpf_funcional_func()))
   wait(1)
   exit()

geraCPF.py
- - -

import random

class geraCPF():
   def __init__(self):
      def cpf_funcional_func(self):
          n = [random.randrange(10) for i in xrange(9)]

          s = sum(x * y for x, y in zip(n, range(10, 1, -1)))
          d1 = 11 - s % 11
          if d1 >= 10:
              d1 = 0
          n.append(d1)

          s = sum(x * y for x, y in zip(n, range(11, 1, -1)))
          d2 = 11 - s % 11
          if d2 >= 10:
              d2 = 0
          n.append(d2)
          return "%d%d%d%d%d%d%d%d%d%d%d" % tuple(n)

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

An OO approach does not depend on SikuliX, but only on the used language (Python in this case) and your implementation concept.

... but I cannot see any OO concept, just the ide, to use a function from some other imported script.

detailed comments:

--- main.py
# usually not OO, but simply procedural (workflow)

import shutil
#from geraCPF import geraCPF
import geraCPF # just so

#class main(): # does not makes sense and the implementation is not Python-like

myApp = App("Chrome")
App.open("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe")
myApp.focus()
while not exists("1520001990649.png"):
   observe() # not sure, what this should do???
   wait(1)
click("1520001996809.png")
type(geraCPF.cpf_funcional_func()) # the function returns str() anyway
# you have to say module.function()
wait(1)
#exit() # not needed

geraCPF.py
- - -
import random
def cpf_funcional_func():
          n = [random.randrange(10) for i in xrange(9)]
          s = sum(x * y for x, y in zip(n, range(10, 1, -1)))
          d1 = 11 - s % 11
          if d1 >= 10:
              d1 = 0
          n.append(d1)
          s = sum(x * y for x, y in zip(n, range(11, 1, -1)))
          d2 = 11 - s % 11
          if d2 >= 10:
              d2 = 0
          n.append(d2)
          return "%d%d%d%d%d%d%d%d%d%d%d" % tuple(n)

I think, you have to step back, to rethink your concept.

Until now I cannot see any need to think in OO, it is just procedural with using functions.

Revision history for this message
CCIOLA (carol-ciola) said :
#2

Hi @RaiMan,

Here is the thing: I need to test a complex desktop application that runs in Delphi and the only way to automate this test is using SikuliX. Other softwares, like Winium, did not work for me.

I will have to use OO to separete the alternative fluxes of this application in classes (a bunch!), calling the methods when needed.

I've been looking on many websites and reunited on this script what I thought it would be the closest way to do this.

Comments with ">>":

--- main.py
import shutil
#from geraCPF import geraCPF
import geraCPF # just so >> if I do this, it does not work for me.

#class main(): # does not makes sense and the implementation is not Python-like >> ok.

myApp = App("Chrome")
App.open("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe")
myApp.focus()
while not exists("1520001990649.png"):
   observe() # not sure, what this should do??? >> it's an implicit wait: while the image.png does not appear, SikuliX just 'observe' and keep running without doing nothing.
   wait(1)
click("1520001996809.png")
type(geraCPF.cpf_funcional_func()) # the function returns str() anyway >> ok.
# you have to say module.function() >> not sure if it will work, I'll try.
wait(1)
#exit() # not needed >> I have to exit from the script execution and return to SikuliX.

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

I am not here, to teach you whatever programming techniques, but your approach as shown above is neither obviously needed nor is it correct or runnable.

first at your comments
import geraCPF # just so >> if I do this, it does not work for me.

what is the problem? import only makes the contents of a Python module available in this context, contained code at indent level 0 is executed once at import time. nothing else happens here. In SikuliX a Python module can be another script xxx.sikuli. The contents of a module are named using the dotted notation of OO: moduleName.function() calls a function() that is defined at indent level 0 in module modulename.

observe() # not sure, what this should do???
observe() without any active onXXX() defined for the respective region (SCREEN in this case) does not do anything, simply comes back instantly (read: http://sikulix-2014.readthedocs.io/en/latest/region.html#observing-visual-events-in-a-region)

#exit() # not needed >> I have to exit from the script execution and return to SikuliX.
exit() is only needed (like break or continue) if you want to terminate the scriptrun from somewhere inside the code (usually at error situations). If a script comes to the point of not having any more statements, it ends anyways.
Be aware: exit() ends the complete scriptrun, even if it is called somewhere deep in a function call chain, hence it DOES NOT return to the caller.

--- the only way to automate this test is using SikuliX
might well be, but the headline here is not SikuliX, but testing.
And every programmatical test is an automation by itself: it drives an application through some steps and checks the results against expectations (assertion). For this we have a world of testing frameworks.
If you want to stick with Python scripting, you might simply use unit test, where you define testcases, that are run one after the other, consist of a sequence setup/test(do something - assert)/teardown and report about success. This might be augmented by using the HTMLTestRunner.
Another option might be RobotFramework, which has a nice wrapper for SikuliX features (https://github.com/rainmanwy/robotframework-SikuliLibrary)

--- I need to test a complex desktop application that runs in Delphi
no need to invent the world again.

Can you help with this problem?

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

To post a message you must log in.