forked from ccnmtl/mediathread
-
Notifications
You must be signed in to change notification settings - Fork 1
/
terrain.py
385 lines (325 loc) · 14.1 KB
/
terrain.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
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
# -*- coding: utf-8 -*-
from lettuce.django import django_url
from lettuce import before, after, world, step
from django.test import client
import sys, os, time
from selenium.webdriver.common.keys import Keys
from mediathread.projects.models import Project
from mediathread.structuredcollaboration.models import Collaboration, CollaborationPolicyRecord
import time
try:
from lxml import html
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import selenium
except:
pass
@before.harvest
def setup_database(variables):
try:
os.remove('lettuce.db')
except:
pass #database doesn't exist yet. that's ok.
os.system("echo 'create table test(idx integer primary key);' | sqlite3 lettuce.db")
os.system('./manage.py syncdb --settings=settings_test --noinput')
os.system('./manage.py migrate --settings=settings_test --noinput')
os.system("echo 'delete from django_content_type;' | sqlite3 lettuce.db")
os.system('./manage.py loaddata mediathread_main/fixtures/sample_course.json --settings=settings_test')
@before.all
def setup_browser():
ff_profile = FirefoxProfile()
ff_profile.set_preference("webdriver_enable_native_events", False)
world.firefox = webdriver.Firefox(ff_profile)
world.client = client.Client()
world.using_selenium = False
# Make the browser size at least 1024x768
world.firefox.execute_script("window.moveTo(0, 1); window.resizeTo(1024, 768);");
@after.all
def teardown_browser(total):
world.firefox.quit()
@step(u'Using selenium')
def using_selenium(step):
world.using_selenium = True
@step(u'Finished using selenium')
def finished_selenium(step):
world.using_selenium = False
@before.each_scenario
def clear_selenium(step):
world.using_selenium = False
Project.objects.all().delete()
Collaboration.objects.exclude(title="Sample Course").delete()
CollaborationPolicyRecord.objects.all().delete()
os.system("echo 'delete from projects_project_participants;' | sqlite3 lettuce.db")
os.system("echo 'delete from projects_projectversion;' | sqlite3 lettuce.db")
os.system("echo 'delete from threadedcomments_comment;' | sqlite3 lettuce.db")
os.system("echo 'delete from django_comments;' | sqlite3 lettuce.db")
os.system("echo 'delete from django_comment_flags;' | sqlite3 lettuce.db")
@step(r'I access the url "(.*)"')
def access_url(step, url):
if world.using_selenium:
world.firefox.get(django_url(url))
else:
response = world.client.get(django_url(url))
world.dom = html.fromstring(response.content)
@step(u'I am ([^"]*) in ([^"]*)')
def i_am_username_in_course(step, username, course):
if world.using_selenium:
world.firefox.get(django_url("/accounts/logout/"))
world.firefox.get(django_url("accounts/login/?next=/"))
username_field = world.firefox.find_element_by_id("id_username")
password_field = world.firefox.find_element_by_id("id_password")
form = world.firefox.find_element_by_name("login_local")
username_field.send_keys(username)
password_field.send_keys("test")
form.submit()
title = world.firefox.title
assert username in world.firefox.page_source, world.firefox.page_source
step.given('I access the url "/"')
step.given('I am in the %s class' % course)
step.given('I am at the Home page')
else:
world.client.login(username=username,password='test')
@step(u'I am not logged in')
def i_am_not_logged_in(step):
if world.using_selenium:
world.firefox.get(django_url("/accounts/logout/"))
else:
world.client.logout()
@step(u'I log out')
def i_log_out(step):
if world.using_selenium:
world.firefox.get(django_url("/accounts/logout/"))
else:
response = world.client.get(django_url("/accounts/logout/"),follow=True)
world.response = response
world.dom = html.fromstring(response.content)
@step(u'I am at the ([^"]*) page')
def i_am_at_the_name_page(step, name):
if world.using_selenium:
# Check the page title
try:
title = world.firefox.title
assert title.find(name) > -1, "Page title is %s. Expected something like %s" % (title, name)
except:
time.sleep(1)
title = world.firefox.title
assert title.find(name) > -1, "Page title is %s. Expected something like %s" % (title, name)
@step(u'there is a sample assignment')
def there_is_a_sample_assignment(step):
os.system('./manage.py loaddata mediathread_main/fixtures/sample_assignment.json --settings=settings_test')
@step(u'there is a sample assignment and response')
def there_is_a_sample_assignment_and_response(step):
os.system('./manage.py loaddata mediathread_main/fixtures/sample_assignment_and_response.json --settings=settings_test')
@step(u'I type "([^"]*)" for ([^"]*)')
def i_type_value_for_field(step, value, field):
if world.using_selenium:
selector = "input[name=%s]" % field
input = world.firefox.find_element_by_css_selector(selector)
assert input != None, "Cannot locate input field named %s" % field
input.send_keys(value)
@step(u'I click the ([^"]*) button')
def i_click_the_value_button(step, value):
if world.using_selenium:
elt = find_button_by_value(value)
assert elt, "Cannot locate button named %s" % value
elt.click()
@step(u'there is not an? "([^"]*)" link')
def there_is_not_a_text_link(step, text):
if not world.using_selenium:
for a in world.dom.cssselect("a"):
if a.text:
if text.strip().lower() in a.text.strip().lower():
href = a.attrib['href']
response = world.client.get(django_url(href))
world.dom = html.fromstring(response.content)
assert False, "found the '%s' link" % text
else:
try:
link = world.firefox.find_element_by_partial_link_text(text)
assert False, "found the '%s' link" % text
except:
pass # expected
@step(u'there is an? "([^"]*)" link')
def there_is_a_text_link(step, text):
if not world.using_selenium:
for a in world.dom.cssselect("a"):
if a.text:
if text.strip().lower() in a.text.strip().lower():
href = a.attrib['href']
response = world.client.get(django_url(href))
world.dom = html.fromstring(response.content)
return
assert False, "could not find the '%s' link" % text
else:
try:
link = world.firefox.find_element_by_partial_link_text(text)
assert link.is_displayed()
except:
try:
time.sleep(1)
link = world.firefox.find_element_by_partial_link_text(text)
assert link.is_displayed()
except:
world.firefox.get_screenshot_as_file("/tmp/selenium.png")
assert False, link.location
@step(u'I click the "([^"]*)" link')
def i_click_the_link(step, text):
if not world.using_selenium:
for a in world.dom.cssselect("a"):
if a.text:
if text.strip().lower() in a.text.strip().lower():
href = a.attrib['href']
response = world.client.get(django_url(href))
world.dom = html.fromstring(response.content)
return
assert False, "could not find the '%s' link" % text
else:
try:
link = world.firefox.find_element_by_partial_link_text(text)
assert link.is_displayed()
link.click()
except:
try:
time.sleep(1)
link = world.firefox.find_element_by_partial_link_text(text)
assert link.is_displayed()
link.click()
except:
world.firefox.get_screenshot_as_file("/tmp/selenium.png")
assert False, link.location
@step(u'I am in the ([^"]*) class')
def i_am_in_the_coursename_class(step, coursename):
if world.using_selenium:
course_title = world.firefox.find_element_by_id("course_title")
assert course_title.text.find(coursename) > -1, "Expected the %s class, but found the %s class" % (coursename, course_title.text)
@step(u'there is an? ([^"]*) button')
def there_is_a_value_button(step, value):
try:
elt = find_button_by_value(value)
assert elt, "Cannot locate button named %s" % value
except:
time.sleep(1)
elt = find_button_by_value(value)
assert elt, "Cannot locate button named %s" % value
@step(u'there is not an? ([^"]*) button')
def there_is_not_a_value_button(step, value):
elt = find_button_by_value(value)
assert elt == None, "Found button named %s" % value
@step(u'I wait (\d+) second')
def then_i_wait_count_second(step, count):
n = int(count)
time.sleep(n)
@step(u'I see "([^"]*)"')
def i_see_text(step, text):
try:
assert text in world.firefox.page_source, world.firefox.page_source
except:
time.sleep(1)
assert text in world.firefox.page_source, "I did not see %s in this page" % text
@step(u'I do not see "([^"]*)"')
def i_do_not_see_text(step, text):
assert text not in world.firefox.page_source, world.firefox.page_source
@step(u'I dismiss a save warning')
def i_dismiss_a_save_warning(step):
time.sleep(1)
alert = world.firefox.switch_to_alert()
alert.accept()
time.sleep(1)
@step(u'there is an? ([^"]*) column')
def there_is_a_title_column(step, title):
elts = world.firefox.find_elements_by_tag_name("h2")
for e in elts:
if e.text and e.text.strip().lower().find(title.lower()) > -1:
return
assert False, "Unable to find a column entitled %s" % title
@step(u'there is not an? ([^"]*) column')
def there_is_not_a_title_column(step, title):
elts = world.firefox.find_elements_by_tag_name("h2")
for e in elts:
if e.text and e.text.strip().lower().startswith(title.lower()):
assert False, "Found a column entitled %s" % title
@step(u'there is help for the ([^"]*) column')
def there_is_help_for_the_title_column(step, title):
elts = world.firefox.find_elements_by_tag_name("h2")
for e in elts:
if e.text and e.text.strip().lower().startswith(title.lower()):
help = e.parent.find_element_by_css_selector("div.helpblock.on")
if help:
return
assert False, "No help found for %s" % title
@step(u'I\'m told ([^"]*)')
def i_m_told_text(step, text):
alert = world.firefox.switch_to_alert()
assert alert.text.startswith(text), "Alert text invalid: %s" % alert.text
alert.accept()
@step(u'the most recent notification is "([^"]*)"')
def the_most_recent_notification_is_text(step, text):
try:
list = world.firefox.find_element_by_id("parent-clumper")
except:
time.sleep(1)
list = world.firefox.find_element_by_id("parent-clumper")
elts = list.find_elements_by_css_selector("div.asset_title")
assert len(elts) > 0, "Found 0 notifications. Expected at least one."
link = elts[0].find_element_by_tag_name("a")
assert link != None, "Found no notification links. Expected at least one"
assert link.text.strip() == text, "Notification text is [%s]. Expected [%s]" % (link.text.strip(), text)
@step(u'I select "([^"]*)" as the owner in the ([^"]*) column')
def i_select_name_as_the_owner_in_the_title_column(step, name, title):
column = get_column(title)
if not column:
time.sleep(1)
column = get_column(title)
assert column, "Unable to find a column entitled %s" % title
menu = column.find_element_by_css_selector("div.switcher_collection_chooser")
menu.find_element_by_css_selector("a.switcher-top").click()
owners = menu.find_elements_by_css_selector("a.switcher-choice.owner")
for o in owners:
if o.text == name:
o.click()
return
assert False, "Unable to find owner %s" % name
@step(u'the owner is "([^"]*)" in the ([^"]*) column')
def the_owner_is_name_in_the_title_column(step, name, title):
column = get_column(title)
if not column:
time.sleep(1)
column = get_column(title)
assert column, "Unable to find a column entitled %s" % title
menu = column.find_element_by_css_selector("div.switcher_collection_chooser")
owner = menu.find_element_by_css_selector("a.switcher-top span.title")
if owner.text != name:
time.sleep(1)
owner = menu.find_element_by_css_selector("a.switcher-top span.title")
assert owner.text == name, "Expected owner title to be %s. Actually %s" % (name, owner.text)
# Local utility functions
def get_column(title):
elts = world.firefox.find_elements_by_tag_name("h2")
for e in elts:
if e.text and e.text.strip().lower().find(title.lower()) > -1:
return e.parent
return None
def find_button_by_value(value, parent = None):
if not parent:
parent = world.firefox
elts = parent.find_elements_by_css_selector("input[type=submit]")
for e in elts:
if e.get_attribute("value") == value:
return e
elts = parent.find_elements_by_css_selector("input[type=button]")
for e in elts:
if e.get_attribute("value") == value:
return e
elts = world.firefox.find_elements_by_tag_name("button")
for e in elts:
if e.get_attribute("type") == "button" and e.text == value:
return e
# try the links too
elts = parent.find_elements_by_tag_name("a")
for e in elts:
if e.text and e.text.strip() == value:
return e
return None
world.find_button_by_value = find_button_by_value