QA/Execution/Web Testing/Selenium Python

From MozillaWiki
< QA‎ | Execution‎ | Web Testing
Revision as of 02:02, 30 January 2010 by Twsmith (talk | contribs)
Jump to navigation Jump to search

Python org's download.
The Selenium org's documentation on configuring Selenium RC for Python.

These notes are based on Selenium RC 1.0.1

Creating a Selenium script in Python

Method A - Convert an Selenium IDE script to Python

Use for a full IDE script or, a script with a few commands as a shortcut to creating Selenium/Python commands.

  1. Open the .htm script in Selenium IDE.
  2. Select File > Export Test Case As > Python - Selenium RC. Naming with a .py file extension associates the file with Python.  Since the SIDE file name is used to create classes in the Python script, recommend limiting the file name to letters, numbers and underscores.
  3. Open the new file in the Python IDLE or a text editor.
  4. Most scripts will require manual editing
  • The setUp def may require changes to parameters in 'self.selenium = selenium( )' which launches the browser. The 3rd parameter is the browser to launch. The 4th parameter is the base URL. For more information see Selenium's Learning the API.
  • While, WhileEnd, Goto, GotoIf and other IDE commands that are not built-in are converted as a call to the "sel" class and marked as a comment. These commands can be found by searching for '# sel.'
  • Reminders regrarding While loops - Syntax is 'while <conditional>:'. Encompassed lines are indented under the While. No explicit end-while statment. End of code blocks indicated by a descreased indention.
  • GotoIf and Goto commands in IDE were perhaps in place of a high-level language control flow, which Python is likely has a command for.
  • Review sel.get_eval( ) calls. The exporter simply converts the 2nd IDE command parameter to a string, which may not create the originally intended results in Python. Uses of storedVars['<var>'] will not reference the variable <var>. JavaScript should be redefined with Python statements.
  • Review print( ) calls. Although the exporter seperates literal text from variables names, the '+' operator may not always be appropriate.

Method B - Build a Python script from scratch.

  • The Selenium org documentation has an example here. It uses the same structure as what's created by the IDE exporter and contains a few comments.
  • The Python Unit Testing Framework is described here.
  • The method that launches the browser is described here.
  • The Python selenium class documentation is in the client RC directory at selenium-remote-control-1.0.1/selenium-python-client-driver-1.0.1/doc/index.html


Settting Up Selenium RC

  1. Download the Selenium RC zip file from the Selenium org RC downloads page. Unzip to a convenient location.
  2. In the selenium-remote-control-1.0.1\selenium-python-client-driver-1.0.1 directory find the file selenium.py.
  3. Copy selenium.py to the Python installation's \Lib directory. For a default Python 2.6 installation this would be c:\Python26\Lib.


Starting the RC server

In Windows

  1. Open a command window and change directories to selenium-remote-control-1.0.1/selenium-server-1.0.1 directory. It should contain selenium-server.jar.
  2. Enter the command "java -jar selenium-server.jar"

If the server is running correctly it will print a few lines and *not* return to the command line prompt. Whenever a script with Selenium runs the server window will display a few more lines and continue waiting.


Running the Selenium script in Python

In Windows

  1. Launch Python IDLE, which creates a Python Shell window.
  2. From the menu select File > Open and select the script from the file browser.  This is expected to open another window containing the script.
  3. From the script window select Run > Run Module. Results of the run will appear in the Python Shell window.


Things we don't know yet.

(or, things the author doesn't know)

if you have some clues to the following mysteries, you are welcome to update the wiki page or contact Truman.

  • What causes the Callback traces at the end of a successful run?
  • How to re-direct the RC runtime log to a file.