QA/Execution/Web Testing/Selenium Python

From MozillaWiki
< QA‎ | Execution‎ | Web Testing
Revision as of 20:51, 2 February 2010 by Twsmith (talk | contribs)
Jump to navigation Jump to search

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. A .py file extension associates the file with Python.  Because the exporter creates classe names based on the Python script file name, the file name should be limited to "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 is the call that launches the browser. The 3rd parameter is the browser type. The 4th parameter is the base URL when starting the script. For more information see the Selenium RC doc Starting the Browser.
  • While, WhileEnd, Goto, GotoIf and other commands that are not Selenium built-ins are converted as a call to the selenium 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 block is indicated by a decreased indention.
  • GotoIf and Goto commands in an IDE script perhaps a substitute for a high-level language control flow, which Python is likely has a command for.
  • Review sel.get_eval( ) calls. The IDE exporter converts the 1st parameter of an storeEval, store or storeExpression command to a string in a get_eval( ) call.  Although get_eval will still execute the string as JavaScript, it may not create the same results as in IDE.  For example, storedVars['myVar'] will not reference the variable myVar.  The JavaScript should generally be rewritten in Python.
  • Review print( ) calls. Although the IDE exporter seperates literal text from variables names, the '+' operator may not always be desired.

Method B - Build a Python script from scratch.

Selenium org's RC documentation contains an example of a script in Python which uses the Python unit testing framework and includes the call that starts the browser. It uses the same structure created by the IDE exporter.

The Python selenium class documentation is on the client in the RC directory at selenium-remote-control-1.0.1\selenium-python-client-driver-1.0.1\doc\index.html


Setting Up Selenium RC

  1. Download the Selenium RC zip file from the Selenium org 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. From the command prompt enter 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. When a script with Selenium runs the server window will display a few more lines and return to 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.  The script appears in another window.
  3. Check code syntax by selecting Run > Check Module.
  4. Run the script by selecting Run > Run Module. Results appear in the Python Shell window.


Things we don't understand yet

...and would like to know. (Well, the author doesn't know. Maybe someone else knows)

If someone has clues to the following mysteries, you are welcome to update the wiki page or contact Truman.

  • What is the difference between running a script in IDLE and Python command window?
  • In IDLE what causes the Callback traces at the end of an apparently successful run?

Answer? Appears to be an issue with IDLE which is supposedly fixed in later versions.  http://bugs.python.org/issue2821

  • In IDLE why does the prompt "Do you want to exit altogether?" appear?