1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """cubicweb extensions for twill |
|
19 |
|
20 """ |
|
21 |
|
22 import re |
|
23 from urllib import quote |
|
24 |
|
25 from twill import commands as twc |
|
26 |
|
27 # convenience / consistency renaming |
|
28 has_text = twc.find |
|
29 hasnt_text = twc.notfind |
|
30 |
|
31 |
|
32 # specific commands |
|
33 _LINK = re.compile('<a.*?href="(.*?)".*?>(.*?)</a>', re.I | re.S) |
|
34 |
|
35 def has_link(text, url=''): |
|
36 browser = twc.get_browser() |
|
37 html = browser.get_html() |
|
38 if html: |
|
39 for match in _LINK.finditer(html): |
|
40 linkurl = match.group(1) |
|
41 linktext = match.group(2) |
|
42 if linktext == text: |
|
43 # if url is specified linkurl must match |
|
44 if url and linkurl != url: |
|
45 continue |
|
46 return |
|
47 raise AssertionError('link %s (%s) not found' % (text, url)) |
|
48 |
|
49 |
|
50 def view(rql, vid=''): |
|
51 """ |
|
52 >> view 'Project P' |
|
53 |
|
54 apply <vid> to <rql>'s rset |
|
55 """ |
|
56 if vid: |
|
57 twc.go('view?rql=%s&vid=%s' % (quote(rql), vid)) |
|
58 else: |
|
59 twc.go('view?rql=%s' % quote(rql)) |
|
60 |
|
61 def create(etype): |
|
62 """ |
|
63 >> create Project |
|
64 |
|
65 go to <etype>'s creation page |
|
66 """ |
|
67 twc.go('view?etype=%s&vid=creation' % etype) |
|
68 |
|
69 def edit(rql): |
|
70 """ |
|
71 >> edit "Project P WHERE P eid 123" |
|
72 |
|
73 calls edition view for <rql> |
|
74 """ |
|
75 twc.go('view?rql=%s&vid=edition' % quote(rql)) |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 def setvalue(formname, fieldname, value): |
|
81 """ |
|
82 >> setvalue entityForm name pylint |
|
83 |
|
84 sets the field's value in the form |
|
85 <forname> should either be the form's index, the form's name |
|
86 or the form's id |
|
87 """ |
|
88 browser = twc.get_browser() |
|
89 form = browser.get_form(formname) |
|
90 if form is None: |
|
91 # try to find if one of the forms has <formname> as id |
|
92 for index, form in enumerate(browser._browser.forms()): |
|
93 # forms in cubicweb don't always have a name |
|
94 if form.attrs.get('id') == formname: |
|
95 # browser.get_form_field knows how to deal with form index |
|
96 formname = str(index+1) |
|
97 break |
|
98 else: |
|
99 raise ValueError('could not find form named <%s>' % formname) |
|
100 eid = browser.get_form_field(form, 'eid').value |
|
101 twc.formvalue(formname, '%s:%s' % (fieldname, eid), value) |
|
102 |
|
103 |
|
104 def submitform(formname, submit_button=None): |
|
105 """ |
|
106 >> submitform entityForm |
|
107 |
|
108 Submit the form named entityForm. This is useful when the form is pre-filed |
|
109 and we only want to click on submit. |
|
110 (The original submit command chooses the form to submit according to the last |
|
111 formvalue instruction) |
|
112 """ |
|
113 browser = twc.get_browser() |
|
114 form = browser.get_form(formname) |
|
115 if form is None: |
|
116 # try to find if one of the forms has <formname> as id |
|
117 for form in browser._browser.forms(): |
|
118 # forms in cubicweb don't always have a name |
|
119 if form.attrs.get('id') == formname: |
|
120 break |
|
121 else: |
|
122 raise ValueError('could not find form named <%s>' % formname) |
|
123 browser._browser.form = form |
|
124 browser.submit(submit_button) |
|
125 |
|
126 |
|
127 # missing actions: delete, copy, changeview |
|