/usr/share/pyshared/twill/extensions/formfill.py is in python-twill 0.9-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | """
Extension functions for easier form filling.
(This module is a dumping ground for features that may ultimately get
added into the main twill command set.)
Commands:
* fv_match -- fill in *all* fields that match a regexp (unlike 'formvalue'
which will complain about multiple matches). Useful for forms
with lots of repeated fieldnames -- 'field-1', 'field-2', etc.
* fv_multi -- fill in multiple form fields at once, e.g.
fv_multi <formname> field1=value1 field2=value2 field3=value3
* fv_multi_sub -- same as 'fv_multi', followed by a 'submit'.
"""
import twill, twill.utils
import re
__all__ = [ 'fv_match', 'fv_multi_match', 'fv_multi', 'fv_multi_sub' ]
def fv_match(formname, regexp, value):
"""
>> fv_match <formname> <field regexp> <value>
Set value of *all* form fields with a name that matches the given
regular expression.
(Unlike 'formvalue' or 'fv', this will not complain about multiple
matches!)
"""
state = twill.get_browser()
form = state.get_form(formname)
if not form:
print 'no such form', formname
return
regexp = re.compile(regexp)
matches = [ ctl for ctl in form.controls if regexp.search(str(ctl.name)) ]
if matches:
print '-- matches %d' % (len(matches),)
n = 0
for control in matches:
state.clicked(form, control)
if control.readonly:
continue
n += 1
twill.utils.set_form_control_value(control, value)
print 'set %d values total' % (n,)
def fv_multi_match(formname, regexp, *values):
"""
>> fv_multi_match <formname> <field regexp> <value> [<value> [<value>..]]
Set value of each consecutive matching form field with the next specified
value. If there are no more values, use the last for all remaining form
fields
"""
state = twill.get_browser()
form = state.get_form(formname)
if not form:
print 'no such form', formname
return
regexp = re.compile(regexp)
matches = [ ctl for ctl in form.controls if regexp.search(str(ctl.name)) ]
if matches:
print '-- matches %d, values %d' % (len(matches), len(values))
n = 0
for control in matches:
state.clicked(form, control)
if control.readonly:
continue
try:
twill.utils.set_form_control_value(control, values[n])
except IndexError, e:
twill.utils.set_form_control_value(control, values[-1])
n += 1
print 'set %d values total' % (n,)
def fv_multi(formname, *pairs):
"""
>> fv_multi <formname> [ <pair1> [ <pair2> [ <pair3> ]]]
Set multiple form fields; each pair should be of the form
fieldname=value
The pair will be split around the first '=', and
'fv <formname> fieldname value' will be executed in the order the
pairs are given.
"""
from twill import commands
for p in pairs:
fieldname, value = p.split('=', 1)
commands.fv(formname, fieldname, value)
def fv_multi_sub(formname, *pairs):
"""
>> fv_multi_sub <formname> [ <pair1> [ <pair2> [ <pair3> ]]]
Set multiple form fields (as with 'fv_multi') and then submit().
"""
from twill import commands
for p in pairs:
fieldname, value = p.split('=', 1)
commands.fv(formname, fieldname, value)
commands.submit()
|