.. _commands: ======================== twill language reference ======================== The following commands are built into twill. Note that all text after a '#' is ignored as a comment, unless it's in a quoted string. Browsing ======== **go** ** -- visit the given URL. **back** -- return to the previous URL. **reload** -- reload the current URL. **follow** ** -- follow the given link. Note: When used from Python, you get the new URL after executing these commands (after possible redirects) with ``browser.url``. Assertions ========== **code** ** -- assert that the last page loaded had this HTTP status, e.g. ``code 200`` asserts that the page loaded fine. **find** ** [**] -- assert that the page contains the specified regular expression pattern. The variable ``__match__`` is set to the first matching subgroup (or the entire matching string, if no subgroups are specified). When called from Python, the matching string is also returned. Fhe command also accepts some flags: **i**, for case-insensitive matching, **m** for multi-line mode and **s** for dotall matching. The flag **x** uses XPath expressions instead of regular expressions (see below). **find** ** **x** -- assert that the page contains the element matched by the XPath expression. The variable ``__match__`` is set to the first matching element's text content. When called from Python, the matching text is also returned. **not_find** ** [**] -- assert that the page *does not* contain the specified regular expression pattern. This command accepts the same flags as **find**. Alternative spelling: "notfind" **not_find** ** **x** -- assert that the page *does not* contain the element pointed by the XPath expression. **url** ** -- assert that the current URL matches the given regex pattern. The variable ``__match__`` is set to the first matching subgroup (or the entire matching string, if no subgroups are specified). When called from Python, the matching string is returned. **title** ** -- assert that the title of this page matches this regular expression. The variable ``__match__`` is set to the first matching subgroup (or the entire matching string, if no subgroups are specified). When called from Python, the matching string is returned. Display ======= **echo** ** -- echo the string to the screen. **info** -- display information about the current page. **redirect_output** ** -- append all twill output to the given file. **reset_output** -- display all output to the screen. **save_html** *[]* -- save the current page's HTML into a file. If no filename is given, derive the filename from the URL. **show** -- show the current page's HTML. Alternative spelling: "showhtml" or "show html" **show_links** -- show all of the links on the current page. Alternative spellings: "showlinks" or "show links" **show_forms** -- show all of the forms on the current page. Alternative spellings: "showforms" or "show forms" **show_history** -- show the browser history. Alternative spellings: "showhistory" or "show history" **show_cookies** -- show the current cookies. Alternative spellings: "showcookies" or "show cookies" Note: When used from Python, you get these objects using the properties of the browser with the same names, e.g. ``browser.html``, ``browser.links``, ``browser.forms``, ``browser.history`` and ``browser.cookies``. Forms ===== **submit** *[]* -- click the submit button with the given name. Instead of the name of the button you can also specify the number of the form element that shall be used as the submit button. If you do not specify a button name or number, the form is submitted via the last submission button clicked; if nothing had been clicked, use the first submit button on the form. See `details on form handling`_ for more information. In rare cases you may need to specify the form explicitly, you can do so by adding the form name or number as an additional parameter to the button name or number. **form_value** * * --- set the given field in the given form to the given value. For read-only form controls, the click may be recorded for use by **submit**, but the value is not changed unless the 'config' command has changed the default behavior. See 'config' and `details on form handling`_ for more information on the 'form_value' command. For list controls, you can use 'form_value +value' or 'form_value -value' to select or deselect a particular value. Alternative spellings: "formvalue" or "fv" **fv** -- abbreviation for 'form_value'. **form_action** * * -- change the form action URL to the given URL. Alternative spellings: "formaction" or "fa" **fa** -- abbreviation for 'form_action'. **form_clear** -- clear all values in the form. Alternative spelling: "formclear" **form_file** * [ ]* -- attach a file to a file upload button by filename. Alternative spelling: 'formfile'. **show_forms** -- show all of the forms on the current page. Alternative spellings: "showforms" or "show forms" Cookies ======= **save_cookies** ** -- save current cookie jar into a file. **load_cookies** ** -- replace current cookie jar with file contents. **clear_cookies** -- clear all of the current cookies. **show_cookies** -- show all of the current cookies. Alternative spellings: "showcookies" or "show cookies" Debugging ========= **debug** ** ** -- turn on or off debugging/tracing for various functions. The first argument is either 'http' to show HTTP headers, 'equiv-refresh' to show "meta refresh" redirects, or 'commands' to show twill commands. The second argument is '0' for off, '1' for on. Variable handling ================= **set_global** * * -- set variable to value in global dictionary. The value can be retrieved with '$value'. Alternative spelling: "setglobal" **set_local** * * -- set variable to value in local dictionary. The value can be retrieved with '$value'. Alternative spelling: "setlocal" The local dictionary is file-specific, while the global module is general to all the commands. Local variables will override global variables if they exist. Note that you can do variable interpolation in strings with ${var}, e.g. :: setglobal a 1 setglobal b 2 fv thisform thatfield "${a}${b}" Other commands ============== **tidy_ok** -- check to see if HTML Tidy runs on this page without any errors or warnings. This check is very stringent, but you can relax the default configuration by setting ``tidy_*`` configuration options. **exit** *[]* -- exit with the given integer code, if specified. 'code' defaults to 0. **run** ** -- execute the given Python command. **run_file** * [ ... ]* -- execute the given file(s). Alternative spellings: "runfile" or "rf" **rf** -- abbreviation for 'run_file'. **add_cleanup** * [ ... ]* -- add the given cleanup file(s). These will be run after the current file has executed (successfully or not). **agent** -- set the browser's "User-agent" string. **timeout** *[]* -- set browser timeout to given number of seconds. Defaults to 10 seconds. Set to 0 for no timeout. **sleep** *[]* -- sleep the given number of seconds. Defaults to 1 second. **reset_browser** -- reset the browser. **extend_with** ** -- import commands from Python module. This acts like ``from import *`` does in Python, so e.g. a function ``fn`` in ``extmodule`` would be available as ``fn``. See *extras/examples/extend_example.py* for an example. **get_input** ** -- get keyboard input and store it in ``__input__``. When called from Python, this function returns the input value. Alternative spelling: "getinput" **get_password** ** -- get *silent* keyboard input and store it in ``__password__``. When called from Python, this function returns the input value. Alternative spelling: "getpassword" **add_auth** * * -- add HTTP Basic Authentication information for the given realm/URI combination. For example, :: add_auth IdyllStuff http://www.idyll.org/ titus test tells twill that a request from the authentication realm "IdyllStuff" under ``http://www.idyll.org/`` should be answered with username 'titus', password 'test'. If the 'with_default_realm' option is set to True, ignore 'realm'. **config** [** [**]] -- show/set configuration options. Configuration options starting with ``tidy_`` will be used when checking documents with HTML Tidy. For example, :: config tidy_drop_empty_elements no **add_extra_headers** ** ** -- add an extra HTTP header to each HTTP request. **show_extra_headers** -- show the headers being added to each HTTP request. **clear_extra_headers** -- clear the headers being added to each HTTP request. Special variables ================= **__input__** -- result of last **getinput**. **__match__** -- matched text from last **find**, **title**, or **url**. **__password__** -- result of last **getpassword**. **__url__** -- current URL. Details on form handling ======================== .. _details on form handling: Both the `form_value` (or `fv`) and `submit` commands rely on a certain amount of implicit cleverness to do their work. In odd situations, it can be annoying to determine exactly what form field `form_value` is going to pick based on your field name, or what form and field `submit` is going to "click" on. Here is the pseudocode for how `form_value` and `submit` figure out what form to use (function `twill.browser.form`):: for each form on page: if supplied regex pattern matches the form name, select if no form name, try converting to an integer N & using N-1 as an index into the list or forms on the page (i.e. form 1 is the first form on the page). Here is the pseudocode for how `form_value` and `submit` figure out what form field to use (function `twill.browser.form_field`):: search current form for control name with exact match to field_name; if single (unique) match, select. if no match, convert field_name into a number and use as an index, if possible. if no match, search current form for control name with regex match to field_name; if single (unique) match, select. if *still* no match, look for exact matches to submit-button values. if single (unique) match, select. Here is the pseudocode for `submit`:: if a form was _not_ previously selected by form_value: if there's only one form on the page, select it. otherwise, fail. if a field is not explicitly named: if a submit button was "clicked" with form_value, use it. otherwise, use the first submit button on the form, if any. otherwise: find the field using the same rules as form_value finally, if a button has been picked, submit using it; otherwise, submit without using a button