1,072
edits
| (9 intermediate revisions by the same user not shown) | |||
| Line 12: | Line 12: | ||
#Open the .htm script in Selenium IDE.<br> | #Open the .htm script in Selenium IDE.<br> | ||
#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.<br> | #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. A dash will create an invalid class name, as will other 'operator' characters. <br> | ||
#Edit the Python script as needed in Python IDLE or a text editor.<br> | #Edit the Python script as needed in Python IDLE or a text editor.<br> | ||
<blockquote> | <blockquote> | ||
*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 [http://seleniumhq.org/docs/05_selenium_rc.html#starting-the-browser Starting the Browser].<br> | *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 [http://seleniumhq.org/docs/05_selenium_rc.html#starting-the-browser Starting the Browser].<br> | ||
*Comments on how specific IDE commands are converted to Python can be found below. | *Comments on how specific IDE commands are converted to Python can be found below. | ||
</blockquote> | </blockquote> | ||
==== Method B - Build a Python script from scratch. ==== | |||
The [http://docs.python.org/library/unittest.html Python unit testing framework] is the foundation of the tests. | |||
*The Python documentation has a [http://docs.python.org/library/unittest.html#basic-example basic example] that demonstrates its usage. | |||
*Selenium org's RC documentation has an example of a [http://seleniumhq.org/docs/05_selenium_rc.html#python Python script], which is the same structure created by the IDE exporter to Python. | |||
The Python unit testing framework is separate from the Selenium class which interacts with the browser. | |||
*The Python code [http://seleniumhq.org/docs/05_selenium_rc.html#starting-the-browser here] are the commands that create the selenium instance and start the browser. | |||
*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. Generally every Selenium IDE command has similarly named command in Python, although there are exceptions as noted in the next point. | |||
*The Selenium IDE can generate Python code for individual SIDE commands by exporting a "script" to Python that contains only one or a few commands. The exporter does not require a fully operational script. An single SIDE command may be translated to one or more Python functions. For example, the verifyXYZ IDE commands are generally translated to a get_xyz( ) passed to an assertion function. | |||
==== Shared Libraries ==== | |||
[[Code libraries for AMO pages]] are available to help centralize code and simplify python scripts. They contain functions that execute features and locators for page elements.<br> | |||
<br> | <br> | ||
=== Setting Up Selenium RC === | === Setting Up Selenium RC === | ||
| Line 64: | Line 77: | ||
*To start a script in the debugger enter<br> | *To start a script in the debugger enter<br> | ||
<blockquote>python -m pdb ''myscript''<br> where ''myscript'' is the script file. More information about the debugger [http://docs.python.org/library/pdb.html here]. </blockquote> | <blockquote>python -m pdb ''myscript''<br> where ''myscript'' is the script file. More information about the debugger [http://docs.python.org/library/pdb.html here]. </blockquote> | ||
<br> | <br> | ||
=== Things we don't understand yet === | === Things we don't understand yet === | ||
| Line 82: | Line 95: | ||
*'''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. Note that indention is in spaces and not tabs. Mixing them causes errors. | *'''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. Note that indention is in spaces and not tabs. Mixing them causes errors. | ||
<blockquote> | <blockquote> | ||
*The Python IDLE editor has a simple block indention command: 1) highlight the lines to indent, 2) select from the menu Format > Indent Region. The Format menu also has Dedent, which is a reverse indent. | |||
</blockquote> <blockquote> | |||
*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 | *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 | ||
<blockquote><pre>'<,'>s/^/ /</pre></blockquote> <blockquote>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.</blockquote></blockquote> <blockquote> | <blockquote><pre>'<,'>s/^/ /</pre></blockquote> <blockquote>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.</blockquote></blockquote> <blockquote> | ||
edits