Web Automation with Python for .NET

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.

IronPython

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.

Installation

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.

  1. 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.
  2. 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:\Python24\DLLs).
  3. (Optional) Copy the RanorexCore.dll, RanorexNet.dll, RanorexSpy.dll, SHDocVw.dll, and Microsoft.mshtml.dll files from the bin\Net2.0-Pro folder of your Ranorex installation into the DLLs folder of the CPython installation. Moreover, copy the RanorexPython.dll from bin\Python2.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.

Scripting

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):

  1. import RanorexPython   
  2. import clr   
  3. clr.AddReference("RanorexNet")   
  4. from Ranorex import *   
  5. 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.

  1. document = WebDocument.OpenDocument("www.ranorex.com"True)                         
  2.   
  3. #  click on 'User Guide' on main page top menu   
  4. Mouse.MoveToWebElement(document.FindSingle("//a[text()='Support']“))   
  5. Mouse.ClickWebElement(document.FindSingle(”//a[text()='User Guide']“), MouseButtonType.LeftButton, Alignment.CenterLeft)                         
  6.   
  7. # click on ‘Web Testing’ in left menu   
  8. document.WaitForDocumentLoaded()   
  9. Mouse.ClickWebElement(document.FindSingle(”//li[@class='NO']/a[text()='Web Testing']“))                         
  10.   
  11. # set background color of Web Testing header and move mouse to it   
  12. document.WaitForDocumentLoaded()   
  13. header = document.FindSingle(”//h2[text()='Ranorex Web Testing']“)   
  14. header.SetStyleAttribute(”background-color”, “red”)   
  15. Mouse.MoveToWebElement(header)  

See Also

For more information on Python for .NET visit the project homepage and Readme page.

For the API documentation refer to the RanorexNet API documentation.

你可能感兴趣的:(automation)