Converting IDE scripts to Python

From MozillaWiki
Jump to: navigation, search

Instructions can be for a full IDE script or, a script with a few commands as a shortcut to creating Python code with Selenium calls.

Converting IDE a script to Python

  1. Open the .htm script in Selenium IDE.
  2. Select File > Export Test Case As > Python - Selenium RC. A .py file extension associates the file with Python.  Because the exporter creates class names based on the Python script file name, the file name should be limited to "safe" characters such as letters, numbers and underscores. A dash will create an invalid class name, as will other 'operator' characters.
  3. Edit the Python script as needed in Python IDLE or a text editor. Comments on how specific IDE commands are converted to Python can be found below.

Required changes to Python script

Stand-alone scripts run on a local client require, at a minimum, a change to the setUp( ) and the parameters in self.selenium = selenium( ) which launches the browser.

  • The 3rd parameter is the browser type, such as '*firefox' or '*iexplore'.
  • The 4th parameter is the base URL when starting the script.

For more information see the Selenium RC doc Starting the Browser.

Scripts run in the Selenium Grid / Hudson environment require changes to the test case class definition and setUp( ) as described here.
See the comments below regarding non-native Selenium statements (ex: while, goto) and Verify statements.

Comments on the SIDE script exporter for Python

  • Indent the body of the script.  A code block in Python is defined by indention rather than delimiters such as { }.  While the exporter correctly indents the Python testing functions that drive the script, most of the converted IDE commands are not.  Those statements need to be indented to the same level as the sel = self.selenium statement.  Note that indention is in spaces and not tabs. Mixing them causes errors.
  • The Python IDLE editor has a simple block indention command: 1) highlight the lines to indent, 2) select from the menu Format > Indent Region.  The Format menu also has Dedent, which is a reverse indent.
  • The VI editor can insert with spaces as follows: 1) in visual mode type capital V and highlight the lines to indent. 2) in command mode enter
'<,'>s/^/        /
which inserts at the beginning of each highlighted line the spaces between the last two slashes.  Identation within functions, while-loops, if-then statements, etc is normally 4 spaces.
  • The VI editor can be set to use 4 spaces in place of a tab with the following entered in command  mode:
set tabstop=4 expandtab shiftwidth=4 softtabstop=4
(can someone explains what this means?)
  • while, whileEnd, goto, gotoIf, label and other commands that are not Selenium built-ins are converted as a call to the selenium instance and marked as a comment. They can be found by searching for "# sel." All comments that begin this way will likely need a Python equivalent.
  • gotoIf and goto commands in an IDE script were perhaps a substitute for a if-else statement or other high-level language control flow.
  • Replace sel.get_eval( ) with appropriate Python statements.  The IDE exporter converts the 1st parameter of an storeEval, store or storeExpression command to a string for the Python get_eval( ) which executes the string as JavaScript. It may not create the same results as in IDE and generally will be simpler in Python anyway.  In particular, storedVars['myVar'] will not reference the variable myVar.
  • Review print( ) calls. Although the IDE exporter seperates literal text from variables names, the '+' operator may not always be desired.
  • wait_for_page_to_load( ) is given a string value.  It makes sense for it to be a numeric value, perhaps a constant.
  • verifyXYZ( ) IDE commands are generally converted to failUnless, failIf or assertEqual functions that incorporate a get_XYZ function to retrieve the text, attribute, location, etc, and compare it to the pattern parameter in the IDE command.
  • the Python statement may not have the same verification functionality as the IDE command since the IDE pattern parameter is converted without parsing the prefix (ex: regexpi: ).  The Python module for regular expressions is named re.
  • changing the failXYZ calls to assertABC could make the script easier to read.
  • element verification errors are reported by a try-except statement that surrounds each assert/fail call and appends an error message to a verificationErrors list.  At the end of the script, in the tearDown function, the list is dumped and the Python traceback is called if the list is not null.