forked from cos-archives/osf-ui-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_wiki_tests.py
296 lines (212 loc) · 8.34 KB
/
project_wiki_tests.py
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
"""
Selenium tests for user account creation. Tests valid account creation,
as well as various ways to do it wrong (mismatched passwords, invalid
email addresses, etc.).
"""
import re
import time
import unittest
# Selenium imports
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
# Project imports
import util
import config
class ProjectWikiTests(unittest.TestCase):
# Setup / teardown functions
@classmethod
def setUpClass(cls):
# Launch Selenium
cls.driver = util.launch_driver()
# Create test account
cls.user_data = util.create_user(cls.driver)
# Login to test account
util.login(
cls.driver,
cls.user_data['username'],
cls.user_data['password']
)
@classmethod
def tearDownClass(cls):
# Close Selenium
cls.driver.close()
def setUp(self):
# Create test project
self.project_url = util.create_project(self.driver)
# Browse to project page
util.goto_project(self.driver)
def tearDown(self):
# Delete test project
util.delete_project(self.driver)
# Utility functions
def _edit_wiki(self):
edit_button = self.driver.find_element_by_link_text('Edit')
edit_button.click()
def _get_wiki_input(self):
return self.driver.find_element_by_id('wmd-input')
def _add_wiki_text(self, text):
self._get_wiki_input().send_keys(text)
def _clear_wiki_text(self):
util.clear_text(self._get_wiki_input())
def _submit_wiki_text(self):
""" Click submit button. """
self.driver.find_element_by_xpath(
'//div[@class="wmd-panel"]//input[@type="submit"]'
).click()
def _get_wiki_version(self):
""" Get current wiki version. """
# Extract version text
version = self.driver\
.find_element_by_xpath('//dt[text()="Version"]/following-sibling::*')\
.text
# Strip (current) from version string
version = re.sub('\s*\(current\)\s*', '', version, flags=re.I)
# Return version number or 0
try:
return int(version)
except ValueError:
return 0
def _get_wiki_par(self):
""" Get <p> containing wiki text. """
# Set implicitly_wait to short value: text may not
# exist, so we don't want to wait too long to find it
self.driver.implicitly_wait(0.1)
# Extract wiki text
# Hack: Wiki text element isn't uniquely labeled,
# so find its sibling first
try:
wiki_par = self.driver.find_element_by_xpath(
'//div[@id="addContributors"]/following-sibling::div//p'
)
except NoSuchElementException:
wiki_par = None
# Set implicitly_wait to original value
self.driver.implicitly_wait(config.selenium_wait_time)
# Return element
return wiki_par
def _get_wiki_text(self):
""" Get text from wiki <p>. """
# Get <p> containing wiki text
wiki_par = self._get_wiki_par()
# Extract text
if wiki_par is not None:
return wiki_par.text
return ''
def _get_wiki_preview(self):
"""
"""
return self.driver\
.find_element_by_id('wmd-preview')\
.text
def _edit_wiki_setup(self):
"""
"""
# Browse to wiki page
self.driver.find_element_by_link_text('Wiki').click()
# Get original version and text
orig_version = self._get_wiki_version()
orig_text = self._get_wiki_text() if orig_version else ''
# Click edit button
self._edit_wiki()
return orig_version, orig_text
def _edit_wiki_teardown(self, expected_version, expected_text):
"""
"""
# Test preview text
preview_text = self._get_wiki_preview()
self.assertEqual(preview_text, expected_text)
# Click submit button
self._submit_wiki_text()
# Get updated version and text
new_version = self._get_wiki_version()
new_text = self._get_wiki_text() if new_version else ''
# Test version and text
self.assertEqual(new_version, expected_version)
self.assertEqual(new_text, expected_text)
# Check version on dashboard
self._check_dashboard(expected_version)
def _test_wiki_delete(self):
# Get original values and open edit box
orig_version, orig_text = self._edit_wiki_setup()
# Clear text
self._clear_wiki_text()
# Update expected version and text
expected_version = orig_version + 1
expected_text = ''
# Submit changes and check results
self._edit_wiki_teardown(expected_version, expected_text)
def _test_wiki_edit(self, new_text):
# Get original values and open edit box
orig_version, orig_text = self._edit_wiki_setup()
# Enter text
self._add_wiki_text(new_text)
# Update expected version and text
expected_version = orig_version + 1
expected_text = orig_text + new_text
# Submit changes and check results
self._edit_wiki_teardown(expected_version, expected_text)
def _check_dashboard(self, expected_version):
# Browse to project page
util.goto_project(self.driver)
# Get latest version update
version = self.driver.find_element_by_tag_name('dd').text
# Assert that expected version is in version string
self.assertTrue('version %d' % (expected_version) in version)
# Browse to wiki page
self.driver.find_element_by_link_text('Wiki').click()
def test_wiki_batch(self):
""" Test entering and deleting wiki text. """
self._test_wiki_edit('entry 1')
self._test_wiki_edit('entry 2')
self._test_wiki_delete()
self._test_wiki_edit('entry 3')
def _test_wiki_format(self, new_text, expected_text, action):
"""Test bolding wiki text.
Args:
"""
# Setup and get starting values
orig_version, orig_text = self._edit_wiki_setup()
# Enter text
if new_text:
self._add_wiki_text(new_text)
# Set text to boldface
if action == 'bold':
self.driver.execute_script(
'$("#wmd-input").select();'
)
else:
util.select_partial(self.driver, 'wmd-input', 2, len(expected_text) + 2)
# Click bold button
self.driver.find_element_by_id('wmd-bold-button').click()
# Get appropriate wrap string and bold counter function
if action == 'bold':
expected_wrap = '**%s**' % expected_text
strong_fun = lambda x: x > 0
else:
expected_wrap = expected_text
strong_fun = lambda x: x == 0
# Assert that wiki text has been wrapped in **
self.assertEqual(
self._get_wiki_input().get_attribute('value'),
expected_wrap
)
# Assert that there are <strong> elements in the preview
preview_bold_elements = self.driver.find_elements_by_css_selector(
'#wmd-preview strong'
)
self.assertTrue(strong_fun(len(preview_bold_elements)))
# Submit and check results
expected_version = orig_version + 1
self._edit_wiki_teardown(expected_version, expected_text)
# Assert that there are <strong> elements in the wiki text
wiki_par = self._get_wiki_par()
bold_elements = wiki_par.find_elements_by_tag_name('strong')
self.assertTrue(strong_fun(len(bold_elements)))
def test_wiki_format_batch(self):
""" Test bold / unbold functions. """
self._test_wiki_format('make this bold', 'make this bold', 'bold')
self._test_wiki_format('', 'make this bold', 'unbold')
# Run tests
if __name__ == '__main__':
unittest.main()