Unlike the traditional Ranorex API, the new Web Automation API is natively provided only for .NET languages. However, there are two easy ways how you can still use Python with Ranorex Web Automation.
The first option is IronPython, a new implementation of the Python programming language for the .NET framework. Basically, this is a new Python compiler producing .NET intermediate language code. It claims to be fully compatible with the Python Language Reference, but code written for the original CPython may not produce the same output if run with IronPython (see Differences between IronPython 1.0 and CPython 2.4.3).
Python for .NET
The second option is the one I will concentrate on in this blog: Python for .NET. This package provides a bridge for CPython to .NET assemblies (framework as well as user written ones). Python users continue to use the CPython interpreter and classes they are familiar with while additionally gaining access to classes written in any .NET language, e.g the classes in the Ranorex Web Automation API.
Unfortunately, since the latest release of Python for .NET more than half a year has gone by and some crucial bugs have been fixed meanwhile. That’s why we use the latest version from the SVN repository instead.
Check out and build the latest version of Python for .NET from the SVN repository. You need to set the Conditional compilation symbols in the Properties of the Python.Runtime project to match your CPython version (e.g. “PYTHON24,UCS2” for CPython version 2.4 and “PYTHON25,UCS2” for version 2.5).
NOTE: For CPython version 2.3, 2.4, or 2.5 we have done that step for you, so you can download built binaries from the Ranorex web server.
Copy the generated/downloaded Python.Runtime.dll file to your CPython installation folder (e.g. C:Python24) and the clr.pyd file to the DLLs folder of the CPython installation (e.g. C:Python24DLLs).
(Optional) Copy the RanorexCore.dll, RanorexNet.dll, RanorexSpy.dll, SHDocVw.dll, and Microsoft.mshtml.dll files from the binNet2.0-Pro folder of your Ranorex installation into the DLLs folder of the CPython installation. Moreover, copy the RanorexPython.dll from binPython2.X-Pro folder (where ‘X’ corresponds to the version of your CPython installation) of your Ranorex installation to the same DLLs folder of the CPython installation and rename it to RanorexPython.pyd. If you omit this step, these six files need to reside in the same directory as the Python scripts you execute.
In order to access Ranorex .NET from your Python scripts, you need to add the following statements at the beginning of each script (in the exact order as shown below):
import RanorexPython import clr clr.AddReference("RanorexNet") from Ranorex import * from Ranorex.Web import *
You can now access all classes and methods in the Ranorex and RanorexWeb namespaces using the RanorexNet API. Consequently, please refer to the RanorexNet API documentation for the available classes and methods. To get an introduction to Ranorex Web automation please read the corresponding chapter in the Ranorex User Guide.
The following sample code opens the Ranorex web site and navigates to the web automation chapter in the Ranorex User Guide. The RanoreXPaths in this sample have been generated by using the RanorexWebSpy.
document = WebDocument.OpenDocument("www.ranorex.com", True) # click on 'User Guide' on main page top menu Mouse.MoveToWebElement(document.FindSingle("//a[text()='Support']")) Mouse.ClickWebElement(document.FindSingle("//a[text()='User Guide']"), MouseButtonType.LeftButton, Alignment.CenterLeft) # click on 'Web Testing' in left menu document.WaitForDocumentLoaded() Mouse.ClickWebElement(document.FindSingle("//li[@class='NO']/a[text()='Web Testing']")) # set background color of Web Testing header and move mouse to it document.WaitForDocumentLoaded() header = document.FindSingle("//h2[text()='Ranorex Web Testing']") header.SetStyleAttribute("background-color", "red") Mouse.MoveToWebElement(header)
For the API documentation refer to the RanorexNet API documentation.