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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
# Copyright 2009-2010 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Test helpers for common AJAX widgets."""
__metaclass__ = type
__all__ = [
'FormPickerWidgetTest',
'InlineEditorWidgetTest',
'InlinePickerWidgetButtonTest',
'InlinePickerWidgetSearchTest',
'OnPageWidget',
'search_and_select_picker_widget',
'search_picker_widget',
]
from windmill.authoring import WindmillTestClient
from lp.testing.windmill import (
constants,
lpuser,
)
class OnPageWidget:
"""A class that represents and interacts with an on-page JavaScript widget.
The widget is assumed to be a YUI widget controlled by yui-X-hidden classes.
"""
def __init__(self, client, widget_name):
"""Constructor.
:param client: A WindmillTestClient instance for interacting with pages.
:param widget_name: The class name of the YUI widget, like 'yui3-picker'.
"""
self.client = client
self.widget_name = widget_name
@property
def xpath(self):
"""The XPath of this widget, not including the hidden or visible state.
"""
# We include a space after the widget name because @class matches the
# /beginning/ of text strings, not whole words!
return u"//div[contains(@class, '%s ')]" % self.widget_name
@property
def visible_xpath(self):
"""The XPath of the widget when it is visible on page."""
subs = dict(name=self.widget_name)
# We include a space after the widget name because @class matches the
# /beginning/ of text strings, not whole words!
return (u"//div[contains(@class, '%(name)s ') "
"and not(contains(@class, '%(name)s-hidden'))]" % subs)
@property
def hidden_xpath(self):
"""The XPath of the widget when it is hidden."""
# We include a space after the widget name because @class matches the
# /beginning/ of text strings, not whole words!
subs = dict(name=self.widget_name)
return (u"//div[contains(@class, '%(name)s ') "
"and contains(@class, '%(name)s-hidden')]" % subs)
def should_be_visible(self):
"""Check to see if the widget is visible on screen."""
self.client.waits.forElement(xpath=self.visible_xpath,
timeout=constants.FOR_ELEMENT)
def should_be_hidden(self):
"""Check to see if the widget is hidden on screen."""
self.client.waits.forElement(xpath=self.hidden_xpath,
timeout=constants.FOR_ELEMENT)
class SearchPickerWidget(OnPageWidget):
"""A proxy for the yui3-picker widget from lazr-js."""
def __init__(self, client):
"""Constructor.
:param client: A WindmillTestClient instance.
"""
super(SearchPickerWidget, self).__init__(client, 'yui3-picker')
self.search_input_xpath = (
self.visible_xpath + "//input[@class='yui3-picker-search']")
self.search_button_xpath = (
self.visible_xpath +
"//div[@class='yui3-picker-search-box']/button")
def _get_result_xpath_by_number(self, item_number):
"""Return the XPath for the given search result number."""
item_xpath = "//ul[@class='yui3-picker-results']/li[%d]/span" % item_number
return self.visible_xpath + item_xpath
def do_search(self, text):
"""Enter some text in the search field and click the search button.
:param text: The text we want to search for.
"""
self.client.waits.forElement(xpath=self.search_input_xpath,
timeout=constants.FOR_ELEMENT)
self.client.type(xpath=self.search_input_xpath, text=text)
self.client.click(xpath=self.search_button_xpath,
timeout=constants.FOR_ELEMENT)
def click_result_by_number(self, item_number):
"""Click on the given result number in the results list.
:param item_number: The item in the results list we should click on.
"""
item_xpath = self._get_result_xpath_by_number(item_number)
self.client.waits.forElement(xpath=item_xpath,
timeout=constants.FOR_ELEMENT)
self.client.click(xpath=item_xpath)
class WidgetTest:
"""A base class to provide logon capability."""
def getLoggedInClient(self):
"""Return a new client, and the url that it has loaded."""
client = WindmillTestClient(self.suite_name)
email = self.user.email
password = self.user.password
client.open(url=lpuser.get_basic_login_url(email, password))
client.waits.forPageLoad(timeout=constants.PAGE_LOAD)
client.open(url=self.url)
client.waits.forPageLoad(timeout=constants.PAGE_LOAD)
return client
class InlineEditorWidgetTest(WidgetTest):
"""Test that the inline editor widget is working properly on a page."""
def __init__(self, url, widget_id, expected_value, new_value, name=None,
suite_name='inline_editor', user=lpuser.NO_PRIV,
widget_tag='h1'):
"""Create a new InlineEditorWidgetTest.
:param url: The URL to the page on which the widget lives.
:param widget_id: The HTML id of the widget.
:param expected_value: The current expected value of the widget.
:param new_value: The value to change the field to.
:param suite: The suite in which this test is part of.
:param user: The user who should be logged in.
:param widget_tag: Element tag the widget is inside of.
"""
self.url = url
if name is None:
self.__name__ = ('test_%s_inline_edit'
% widget_id.replace('-', '_'))
else:
self.__name__ = name
self.widget_id = widget_id
self.expected_value = expected_value
self.new_value = new_value
self.suite_name = suite_name
self.user = user
self.widget_tag = widget_tag
def __call__(self):
"""Tests the widget is hooked and works properly.
The test:
* opens the url;
* asserts that the widget is initialized to the expected value;
* uses the inline editor to change to the new value;
* asserts that the page was updated with the new value;
* reloads and verifies that the new value sticked.
"""
client = self.getLoggedInClient()
widget_base = u"//%s[@id='%s']" % (self.widget_tag, self.widget_id)
client.waits.forElement(
xpath=widget_base + '/a', timeout=constants.FOR_ELEMENT)
client.asserts.assertText(
xpath=widget_base + '/span[1]', validator=self.expected_value)
client.click(xpath=widget_base + '/a')
client.waits.forElement(
xpath=widget_base + '/a', timeout=constants.FOR_ELEMENT)
client.waits.forElement(
xpath=widget_base + '//textarea', timeout=constants.FOR_ELEMENT)
client.type(
xpath=widget_base + '//textarea', text=self.new_value)
client.click(xpath=widget_base + '//button[last()]')
client.waits.forElement(
xpath=widget_base + '/span[1][text()="' + self.new_value + '"]',
timeout=constants.FOR_ELEMENT)
def search_picker_widget(client, search_text):
"""Search using an on-page picker widget."""
picker = SearchPickerWidget(client)
picker.should_be_visible()
picker.do_search(search_text)
def search_and_select_picker_widget(client, search_text, result_index):
"""Search using an on-page picker widget and click a search result."""
picker = SearchPickerWidget(client)
picker.should_be_visible()
picker.do_search(search_text)
picker.click_result_by_number(result_index)
class InlinePickerWidgetSearchTest(WidgetTest):
"""Test that the Picker widget edits a value inline."""
def __init__(self, url, activator_id, search_text, result_index,
new_value, name=None, suite_name='inline_picker_search_test',
user=lpuser.FOO_BAR):
"""Create a new InlinePickerSearchWidgetTest.
:param url: The URL to the page on which the widget lives.
:param activator_id: The HTML id of the activator widget.
:param search_text: Picker search value.
:param result_index: Item in picker result to select.
:param new_value: The value to change the field to.
:param name: Override the test name, if necessary.
:param suite: The suite in which this test is part of.
:param user: The user who should be logged in.
"""
self.url = url
if name is None:
self.__name__ = 'test_%s_inline_picker' % (
activator_id.replace('-', '_'),)
else:
self.__name__ = name
self.activator_id = activator_id
self.search_text = search_text
self.result_index = result_index
self.new_value = new_value
self.suite_name = suite_name
self.user = user
def __call__(self):
# Load page.
client = self.getLoggedInClient()
# Click on edit button.
button_xpath = (
u"//span[@id='%s']"
"/button[not(contains(@class, 'yui3-activator-hidden'))]"
% self.activator_id)
client.waits.forElement(
xpath=button_xpath,
timeout=constants.FOR_ELEMENT)
client.click(xpath=button_xpath)
# Search picker.
search_and_select_picker_widget(
client, self.search_text, self.result_index)
# Verify update.
client.waits.sleep(milliseconds=u'2000')
client.asserts.assertText(
xpath=u"//span[@id='%s']//a" % self.activator_id,
validator=self.new_value)
# Reload the page to verify that the selected value is persisted.
client.open(url=self.url)
client.waits.forPageLoad(timeout=constants.PAGE_LOAD)
# Verify update, again.
client.waits.forElement(
xpath=u"//span[@id='%s']//a" % self.activator_id,
timeout=constants.FOR_ELEMENT)
client.asserts.assertText(
xpath=u"//span[@id='%s']//a" % self.activator_id,
validator=self.new_value)
class InlinePickerWidgetButtonTest(WidgetTest):
"""Test custom buttons/links added to the Picker."""
def __init__(self, url, activator_id, button_class, new_value,
name=None, suite_name='inline_picker_button_test',
user=lpuser.FOO_BAR):
"""Create a new InlinePickerWidgetButtonTest.
:param url: The URL to the page on which the widget lives.
:param activator_id: The HTML id of the activator widget.
:param button_class: The CSS class identifying the button.
:param new_value: The value to change the field to.
:param name: Override the test name, if necessary.
:param suite: The suite in which this test is part of.
:param user: The user who should be logged in.
"""
self.url = url
self.activator_id = activator_id
self.button_class = button_class
self.new_value = new_value
self.suite_name = suite_name
self.user = user
if name is None:
self.__name__ = 'test_%s_inline_picker' % (
activator_id.replace('-', '_'),)
else:
self.__name__ = name
def __call__(self):
# Load page.
client = self.getLoggedInClient()
# Click on edit button.
button_xpath = (
u"//span[@id='%s']"
"/button[not(contains(@class, 'yui3-activator-hidden'))]"
% self.activator_id)
client.waits.forElement(xpath=button_xpath, timeout=u'25000')
client.click(xpath=button_xpath)
# Click on remove button.
remove_button_xpath = (
u"//div[contains(@class, 'yui3-picker ') "
"and not(contains(@class, 'yui3-picker-hidden'))]"
"//*[contains(@class, '%s')]" % self.button_class)
client.waits.forElement(xpath=remove_button_xpath, timeout=u'25000')
client.click(xpath=remove_button_xpath)
client.waits.sleep(milliseconds=u'2000')
# Verify removal.
client.asserts.assertText(
xpath=u"//span[@id='%s']/span[@class='yui3-activator-data-box']"
% self.activator_id,
validator=self.new_value)
# Reload the page to verify that the selected value is persisted.
client.open(url=self.url)
client.waits.forPageLoad(timeout=u'25000')
# Verify removal, again.
client.waits.forElement(
xpath=u"//span[@id='%s']/span[@class='yui3-activator-data-box']"
% self.activator_id,
timeout=u'25000')
client.asserts.assertText(
xpath=u"//span[@id='%s']/span[@class='yui3-activator-data-box']"
% self.activator_id,
validator=self.new_value)
class FormPickerWidgetTest(WidgetTest):
"""Test that the Picker widget edits a form value properly."""
def __init__(self, url, short_field_name, search_text, result_index,
new_value, name=None, suite_name='form_picker',
user=lpuser.FOO_BAR):
"""Create a new FormPickerWidgetTest.
:param url: The URL to the page on which the widget lives.
:param short_field_name: The name of the Zope attribute. For example,
'owner' which has the id 'field.owner'.
:param search_text: Picker search value.
:param result_index: Item in picker result to select.
:param new_value: The value to change the field to.
:param name: Override the test name, if necessary.
:param suite: The suite in which this test is part of.
:param user: The user who should be logged in.
"""
self.url = url
if name is None:
self.__name__ = 'test_%s_form_picker' % (
short_field_name.replace('-', '_'),)
else:
self.__name__ = name
self.search_text = search_text
self.result_index = result_index
self.new_value = new_value
self.suite_name = suite_name
self.user = user
self.choose_link_id = 'show-widget-field-%s' % short_field_name
self.field_id = 'field.%s' % short_field_name
def __call__(self):
# Load page.
client = self.getLoggedInClient()
# Click on "Choose" link to show picker for the given field.
client.waits.forElement(
id=self.choose_link_id, timeout=constants.PAGE_LOAD)
client.click(id=self.choose_link_id)
# Search picker.
search_and_select_picker_widget(
client, self.search_text, self.result_index)
# Verify value.
client.asserts.assertProperty(
id=self.field_id, validator=u"value|%s" % self.new_value)
|