QA/Execution/Web Testing/Selenium Python

From MozillaWiki
< QA‎ | Execution‎ | Web Testing
Revision as of 02:22, 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 Python code with Selenium calls.

  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.  Because the script has classes based on the Python script file name, the file name should be limited to "class name safe" characters such as letters, numbers and underscores.
  3. Edit the Python script as needed in Python IDLE or a text editor.
  • 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 when starting the script. For more information see Selenium's Learning the API.
  • While, WhileEnd, Goto, GotoIf and other commands that are not Selenium built-ins are converted as a call to the "sel" class and marked as a comment. The commands can be found by searching for "# sel."
  • Reminders regarding While loops - Syntax is "while <conditional>:". Statements in the while block are indented. No explicit end-while statment. End of code blocks indicated by a descreased indention.
  • GotoIf and Goto commands in IDE were perhaps substituting for 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 command parameter to a string, which in Python may not create the results originally intended. Uses of storedVars['myVar'] will not reference the variable myVar.  Although get_eval will still execute JavaScript, it generally should be rewritten in Python.
  • Review print( ) calls. Although the IDE 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.