|
1 """cubicweb extensions for twill""" |
|
2 |
|
3 import re |
|
4 from urllib import quote |
|
5 |
|
6 from twill import commands as twc |
|
7 |
|
8 # convenience / consistency renaming |
|
9 has_text = twc.find |
|
10 hasnt_text = twc.notfind |
|
11 |
|
12 |
|
13 # specific commands |
|
14 _LINK = re.compile('<a.*?href="(.*?)".*?>(.*?)</a>', re.I | re.S) |
|
15 |
|
16 def has_link(text, url=''): |
|
17 browser = twc.get_browser() |
|
18 html = browser.get_html() |
|
19 if html: |
|
20 for match in _LINK.finditer(html): |
|
21 linkurl = match.group(1) |
|
22 linktext = match.group(2) |
|
23 if linktext == text: |
|
24 # if url is specified linkurl must match |
|
25 if url and linkurl != url: |
|
26 continue |
|
27 return |
|
28 raise AssertionError('link %s (%s) not found' % (text, url)) |
|
29 |
|
30 |
|
31 def view(rql, vid=''): |
|
32 """ |
|
33 >> view 'Project P' |
|
34 |
|
35 apply <vid> to <rql>'s rset |
|
36 """ |
|
37 if vid: |
|
38 twc.go('view?rql=%s&vid=%s' % (quote(rql), vid)) |
|
39 else: |
|
40 twc.go('view?rql=%s' % quote(rql)) |
|
41 |
|
42 def create(etype): |
|
43 """ |
|
44 >> create Project |
|
45 |
|
46 go to <etype>'s creation page |
|
47 """ |
|
48 twc.go('view?etype=%s&vid=creation' % etype) |
|
49 |
|
50 def edit(rql): |
|
51 """ |
|
52 >> edit "Project P WHERE P eid 123" |
|
53 |
|
54 calls edition view for <rql> |
|
55 """ |
|
56 twc.go('view?rql=%s&vid=edition' % quote(rql)) |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 def setvalue(formname, fieldname, value): |
|
62 """ |
|
63 >> setvalue entityForm name pylint |
|
64 |
|
65 sets the field's value in the form |
|
66 <forname> should either be the form's index, the form's name |
|
67 or the form's id |
|
68 """ |
|
69 browser = twc.get_browser() |
|
70 form = browser.get_form(formname) |
|
71 if form is None: |
|
72 # try to find if one of the forms has <formname> as id |
|
73 for index, form in enumerate(browser._browser.forms()): |
|
74 # forms in cubicweb don't always have a name |
|
75 if form.attrs.get('id') == formname: |
|
76 # browser.get_form_field knows how to deal with form index |
|
77 formname = str(index+1) |
|
78 break |
|
79 else: |
|
80 raise ValueError('could not find form named <%s>' % formname) |
|
81 eid = browser.get_form_field(form, 'eid').value |
|
82 twc.formvalue(formname, '%s:%s' % (fieldname, eid), value) |
|
83 |
|
84 |
|
85 def submitform(formname, submit_button=None): |
|
86 """ |
|
87 >> submitform entityForm |
|
88 |
|
89 Submit the form named entityForm. This is useful when the form is pre-filed |
|
90 and we only want to click on submit. |
|
91 (The original submit command chooses the form to submit according to the last |
|
92 formvalue instruction) |
|
93 """ |
|
94 browser = twc.get_browser() |
|
95 form = browser.get_form(formname) |
|
96 if form is None: |
|
97 # try to find if one of the forms has <formname> as id |
|
98 for form in browser._browser.forms(): |
|
99 # forms in cubicweb don't always have a name |
|
100 if form.attrs.get('id') == formname: |
|
101 break |
|
102 else: |
|
103 raise ValueError('could not find form named <%s>' % formname) |
|
104 browser._browser.form = form |
|
105 browser.submit(submit_button) |
|
106 |
|
107 |
|
108 # missing actions: delete, copy, changeview |