Search code examples
pythontestingpython.netgui-testingranorex

How do you use ranorex with python?


It seems that Ranorex stopped support for python with version 3.x, which was back in 2011. I only found old documentation about how to use it with IronPython.

Can you provide a minimal example how to get ranorex running, best would be in standard CPython (and not IronPython)


Solution

  • Good news: Ranorex still works with .NET for Python. And this will still be in the future as long as C# or VB APIs are supported by ranorex and as long as dotnet for python is still supported (which it is, last commit is a few days ago at the time of this post).

    Now, it is a bit tricky to get Ranorex remote control running in Python. This setup was from pure trial-and-error and combining different pieces of information online:

    1. Install python for .net: pip3 install pythonnet
    2. Find out the DLL folder of ranorex. In my case it was C:\Program Files (x86)\Ranorex 8.0\Bin\x64\. In this folder you should see several DLLs (75 DLLs on my machine) including Ranorex.Core.dll

    Now Ranorex should be ready to be used in Python. Here is a small example (you'd need to replace the sys.path.extend() with the path you got from step 2.

    import sys
    import clr
    
    # make Ranorex module available, needs before the `import Ranorex`
    sys.path.append('C:\\Program Files (x86)\\Ranorex 8.0\\Bin\\x64\\')
    clr.AddReference('Ranorex.Core') 
    import Ranorex
    
    Ranorex.Host.Local.RunApplication('C:\\path\\to\\my_app.exe')
    
    apps = [c for c in Ranorex.Host.Local.Children if "My App" in c.ToString()]
    if len(apps) != 1:
        print("starting of 'My App' somehow failed, quitting now")
        sys.exit(1)
    
    app = apps[0]
    app.PressKeys('{LMenu down}{Fkey}{LMenu up}') # presses Alt-F -> e.g. opens the file menu
    

    To develop your python scripts you best set up the test in ranorex recorder and then generate the C# code over Export -> Generate C# Code (Ctrl-G). Then you'd need to translate the C# code to python but that's relatively easy.