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