QA/Execution/Web Testing/Selenium Python: Difference between revisions
No edit summary |
No edit summary |
||
| Line 93: | Line 93: | ||
*'''verifyXYZ( ) IDE commands''' are generally converted to [http://docs.python.org/library/unittest.html?highlight=unittest#testcase-objects 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. | *'''verifyXYZ( ) IDE commands''' are generally converted to [http://docs.python.org/library/unittest.html?highlight=unittest#testcase-objects 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. | ||
<blockquote> | <blockquote> | ||
*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 [http://docs.python.org/library/re.html?highlight=regular%20expressions#module-contents re]. | *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 [http://docs.python.org/library/re.html?highlight=regular%20expressions#module-contents re]. | ||
*changing the failXYZ calls to assertABC could make the script easier to read. | *changing the failXYZ calls to assertABC could make the script easier to read. | ||
</blockquote> | </blockquote> | ||
*'''Verification | *'''Verification error reporting''', from converted verifyXYZ IDE commands, are stored in a list and dumped at the end of the script. Specifically: | ||
<blockquote> | <blockquote>the assert or fail function is encompassed in a ''try-except'' statement. The ''except'' part appends the error message into the list verificationErrors. | ||
the assert or fail function is encompassed in a ''try-except'' statement. The ''except'' part appends the error message into the list verificationErrors. | *the error text it either null or a simple "''value1'' != ''value2''". TBD: other information to include to make the message useful. | ||
*the error text it either null or a simple "''value1'' != ''value2''". TBD: | *in the tearDown function, automatically called when the script is finished, if verificationsErrors list is not null the Python traceback is called and the list is dumped. TBD: whether the script should print the verifcation errors for the runtime environment to process.<br> | ||
*in the tearDown function, | * | ||
</blockquote> | </blockquote> <blockquote></blockquote> | ||
<blockquote></blockquote> | |||
Revision as of 22:41, 4 February 2010
- Python org's download.
- The Selenium org's documentation on configuring Selenium RC for Python.
- Selenium RC requires Java. Download page here.
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.
- Open the .htm script in Selenium IDE.
- 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.
- 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.
- Comments on how specific IDE commands are converted to Python can be found below.
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
- Download the Selenium RC zip file from the Selenium org downloads page. Unzip to a convenient location.
- In the selenium-remote-control-1.0.1\selenium-python-client-driver-1.0.1 directory find the file selenium.py.
- 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
- 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.
- 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
Using Python IDLE
- Launch Python IDLE, which creates a Python Shell window.
- From the menu select File > Open and select the script from the file browser. The script appears in another window.
- Check code syntax by selecting Run > Check Module.
- Run the script by selecting Run > Run Module. Results appear in the Python Shell window.
From command window
- Note: The Python installer does not change the path environment variable which allows the python to run from anywhere. Update from Start > Control Panel > System > Advanced tab > Environment Variables button. From the System Variables list highlight path and select Edit. Add your Python installation directory, for example c:\Python26 for the default installation, remembering to include semi-colon ( ; ) between it and the existing list. Select OK in the dialogs to save. Value applies to new command windows.
- To run a script enter
python myscript
where myscript is the script file. More info on Python command line options here.
- To start a script in the debugger enter
python -m pdb myscript
where myscript is the script file. More information about the debugger here.
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?
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 - 8 spaces. Note that indention is in spaces and not tabs. Mixing them causes errors.
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.
- while, whileEnd, goto, gotoIf, label 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."
- gotoIf and goto commands in an IDE script were perhaps a substitute for a if-else statement or other high-level language control flow.
- Review sel.get_eval( ) calls since most can be simplified. The IDE exporter converts the 1st parameter of an storeEval, store or storeExpression command to a string for the Python get_eval( ). Although get_eval stills executes the string as JavaScript, it may not create the same results as in IDE and generally should be rewritten 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.
- Verification error reporting, from converted verifyXYZ IDE commands, are stored in a list and dumped at the end of the script. Specifically:
the assert or fail function is encompassed in a try-except statement. The except part appends the error message into the list verificationErrors.
- the error text it either null or a simple "value1 != value2". TBD: other information to include to make the message useful.
- in the tearDown function, automatically called when the script is finished, if verificationsErrors list is not null the Python traceback is called and the list is dumped. TBD: whether the script should print the verifcation errors for the runtime environment to process.