This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terrain.py
184 lines (153 loc) · 5.37 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
# -*- coding: utf-8 -*-
from lettuce.django import django_url
from lettuce import before, after, world, step
from django.test import client
import sys
import os
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
def skip_selenium():
return (os.environ.get('LETTUCE_SKIP_SELENIUM', False)
or (hasattr(settings, 'LETTUCE_SKIP_SELENIUM')
and settings.LETTUCE_SKIP_SELENIUM))
@before.harvest
def setup_browser(variables):
world.using_selenium = False
if skip_selenium():
world.browser = None
world.skipping = False
else:
ff_profile = FirefoxProfile()
ff_profile.set_preference("webdriver_enable_native_events", False)
world.firefox = webdriver.Firefox(ff_profile)
world.using_selenium = False
world.client = client.Client()
@after.harvest
def teardown_browser(total):
if not skip_selenium():
world.firefox.quit()
@before.harvest
def setup_database(_foo):
# make sure we have a fresh test database
os.system("rm -f lettuce.db")
os.system("cp test_data/test.db lettuce.db")
@after.harvest
def teardown_database(_foo):
os.system("rm -f lettuce.db")
@before.each_scenario
def clear_data(_foo):
pass
@step(u'Using selenium')
def using_selenium(step):
if skip_selenium():
world.skipping = True
else:
world.using_selenium = True
@step(u'Finished using selenium')
def finished_selenium(step):
if skip_selenium():
world.skipping = False
else:
world.using_selenium = False
@before.each_scenario
def clear_selenium(step):
world.using_selenium = False
@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 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 am taken to a login screen')
def i_am_taken_to_a_login_screen(step):
assert len(world.response.redirect_chain) > 0
(url,status) = world.response.redirect_chain[0]
assert status == 302, status
assert "/login/" in url, "URL redirected to was %s" % url
@step(u'there is not an? "([^"]*)" link')
def there_is_not_a_link(step, text):
found = False
for a in world.dom.cssselect("a"):
if a.text and a.text.strip() == text:
found = True
assert not found
@step(u'there is an? "([^"]*)" link')
def there_is_a_link(step, text):
found = False
for a in world.dom.cssselect("a"):
if a.text and a.text.strip() == text:
found = True
assert found
@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 fill in "([^"]*)" in the "([^"]*)" form field')
def i_fill_in_the_form_field(step, value, field_name):
# note: relies on input having id set, not just name
if not world.using_selenium:
assert False, "this step needs to be implemented for the django test client"
world.firefox.find_element_by_id(field_name).send_keys(value)
@step(u'I submit the "([^"]*)" form')
def i_submit_the_form(step, id):
if not world.using_selenium:
assert False, "this step needs to be implemented for the django test client"
world.firefox.find_element_by_id(id).submit()
@step('I go back')
def i_go_back(self):
""" need to back out of games currently"""
if not world.using_selenium:
assert False, "this step needs to be implemented for the django test client"
world.firefox.back()
@step(u'I wait for (\d+) seconds')
def wait(step,seconds):
time.sleep(int(seconds))
@step(r'I see the header "(.*)"')
def see_header(step, text):
if world.using_selenium:
assert text.strip() == world.firefox.find_element_by_css_selector(".jumbotron>h1").text.strip()
else:
header = world.dom.cssselect('.jumbotron>h1')[0]
assert text.strip() == header.text_content().strip()
@step(r'I see the page title "(.*)"')
def see_title(step, text):
if world.using_selenium:
assert text == world.firefox.title
else:
assert text == world.dom.find(".//title").text