QA/Execution/Web Testing/Selenium Python
Python language download can be found here.
The Selenium site documentation on Python is here.
These notes are based on Selenium RC 1.0.1
Set Up Selenium RC
1. Download the Selenium RC zip from the Selenium HQ downloads page. Unzip to a
convenient location.
2. In the selenium-remote-control\selenium-python-client-driver directory find the file selenium.py. Copy it to the Python installation's \Lib directory. For a default Python 2.6 installation this is c:\Python26\Lib
Creating Selenium script in Python.
Method A - Convert an Selenium IDE script to Python.
Can be used on a full IDE script, or a script with a few commands as a shortcut to creating Python commands.
1. Open the .htm script in Selenium IDE.
2. Select File > Export Test Case As > Python - Selenium RC. Naming it with .py file extension associates the file with Python.
3. Open the new file in the Python IDLE or a text editor.
4. Most scripts will require manual editing
- Some characters in the SIDE file name ( ex: dashes ) can cause invalid Python code when converted into object names in the new script.
- 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 [1] here.
- IDE commands that are not recognized by the exporter are converted as a call to the "sel" class and marked as a comment. These commands can be found by searching for '# sel.' This happens with While, WhileEnd, Goto, GotoIf, since they are not built-in Selenium commands.
- 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 documentation has an example here. It's similiar to the structure created by the IDE exporter but contains a few comments.
Both use the Python Unit Testing Framework described
here.
The function that launches the browser is described here.
The complete documentation for the Python selenium class is on the client in the RC directory: on the client at selenium-remote-control-1.0.1/selenium-python-client-driver-1.0.1/doc/index.html
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 or Krupa.
- What causes the Callback traces at the end of a successful run?
- How to re-direct the RC runtime log to a file.