English|Deutsch
Subscribe Ranorex Announcements Feed Ranorex LinkedIn Ranorex twitter Ranorex Facebook

Python Testing

This document describes how to use Ranorex in a Python script and how to automate a simple control. You can find Python samples in the Scripts directory of Ranorex.

Prerequisites

Python Interpreter

If you want to use Ranorex from a Python script, than you must have the Python interpreter installed.
All versions of the Python Interpreter can be downloaded from http://www.python.org.
Ranorex supports the following Python versions:

Python 2.3

  1. Download the interpreter from: http://www.python.org/2.3.4/

  2. Install Python-2.3.4 on your local machine.

  3. Copy the binaries RanorexCore.dll and RanorexPython.dll from the Ranorex installation folder 'Bin\Python2.3' into the directory of the Python installation (e.g. C:\Python23\DLLs).

Python 2.4

  1. Download the interpreter from: http://www.python.org/2.4.3/

  2. Install  Python-2.4.3 on your local machine.

  3. Copy the binaries RanorexCore.dll and RanorexPython.dll from the Ranorex installation folder 'Bin\Python2.4' into the directory of the Python installation (e.g. C:\Python24\DLLs).

Python 2.5

NOTE: Supported only in RanorexPro
  1. Download the interpreter from: http://www.python.org/2.5/

  2. Install Python-2.5 on your local machine.

  3. NOTE: Extension modules (like RanorexPython) for 2.5 can no longer have the '.dll' extension, they must have a '.pyd' extension.

    Copy the binaries RanorexCore.dll and RanorexPython.dll from the Ranorex installation folder 'Bin\Python2.5' into the directory of the Python installation (e.g. C:\Python25\DLLs). Rename RanorexPython.dll to RanorexPaython.pyd.


Optional: If you haven’t a Python editor yet, download and install the freeware Python editor PythonWin from Sourceforge.

Writing a simple Python script

Open a Python editor, insert the following code and run the script
Python
"""CalcTest2.py -- demonstrates automated GUI testing of calc.exe with Ranorex.
 
Usage:  CalcTest2-en [sleeptime]
 
  sleeptime ... Integer value between 1 and 10000, indicating the interval (in milliseconds) you want the
  script process to be inactive between the commands (default=500).
 
CalcTest2.py demonstrates searching of forms, finding of controls by Id and
clicking of buttons
"""
 
__version__ = 1, 0, 0
 
import sys
import RanorexPython as Ranorex
 
TESTED_APPLICATION_PATH = 'calc.exe'
TESTED_APPLICATION_CLASS = 'SciCalc'
 
def printerror(*args):
    msg = ' '.join(args)
    sys.stderr.write(msg)
    sys.stderr.write("\n")
 
def main():
    if len(sys.argv) > 2:
        printerror("Too many arguments!\n\n" + __doc__)
        return 1
 
    sleeptime = 500
    # Reading sleeptime
    if len(sys.argv) == 2:
        sleeptime = int(sys.argv[1])
        if sleeptime < 1 or sleeptime > 10000:
            printerror("Arguments error, use a value between 1 and 10000!\n\n" + __doc__)
            return 2           
 
    print '-----------------------------------------------------------------'
    print ' General functions'
    print '-----------------------------------------------------------------'
    Ranorex.SetSleepTime(sleeptime)
    sleeptime = Ranorex.GetSleepTime()
    print '  SleepTime=' + str(sleeptime)
    Ranorex.Sleep(100)
 
    print '-----------------------------------------------------------------'
    print ' Activating class: ' + TESTED_APPLICATION_CLASS
    print '-----------------------------------------------------------------'
    form = Ranorex.FormFindClassName(TESTED_APPLICATION_CLASS)
    if form == 0:
        print '   Form not found, starting application ' + TESTED_APPLICATION_PATH
        ret=Ranorex.ApplicationStart(TESTED_APPLICATION_PATH)
        if ret != 0:
            print '\nERROR: Cannot start application, please start the tested application calc.exe and start the script again'
            return 3
        form=Ranorex.FormFindClassName(TESTED_APPLICATION_CLASS,1,1,2000)
        if form == 0:
            print '   Error: Form not found'       
            return 4
    print '   Form found, form=' + hex(form)
 
    print '-----------------------------------------------------------------'
    print ' Searching and testing buttons'
    print '-----------------------------------------------------------------'
    print '   searching button 2 by text'
    button2=Ranorex.FormFindChildControlId(form, 126)
    if button2 == 0:
        print 'ERROR: button button2 not found'
        return 5
    print '      button2=' + hex(button2)
    if Ranorex.MouseClickControl(button2) != 0:
        print 'ERROR: pressing button2'
        return 5          
 
    print '   searching button * by text'
    buttonx=Ranorex.FormFindChildControlId(form, 91)
    print '      buttonx=' + hex(buttonx)
    if Ranorex.MouseClickControl(buttonx) != 0:
        print 'ERROR: pressing buttonx'
        return 5          
 
    print '   searching button 3 by text'
    button3=Ranorex.FormFindChildControlId(form, 127)
    if button3 == 0:
        print 'ERROR: button button3 not found'
        return 5
    print '      button3=' + hex(button3)
    if Ranorex.MouseClickControl(button3) != 0:
        print 'ERROR: pressing button3'
        return 5          
 
    print '   searching button = by text'
    buttoni=Ranorex.FormFindChildControlId(form, 112)
    if buttoni == 0:
        print 'ERROR: button buttoni not found'
        return 5
    print '      buttoni=' + hex(buttoni)
    if Ranorex.MouseClickControl(buttoni) != 0:
        print 'ERROR: pressing buttoni'
        return 5          
 
    print '----------------------------------------------------------------'
    print ' Closing form '
    print '----------------------------------------------------------------'
    Ranorex.FormClose(form)
    Ranorex.Sleep(1000)
    print 'End'
 
if __name__ == "__main__":
    ret = main()
    sys.exit(ret)