This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgaymerconnect_test.py
203 lines (184 loc) · 8.57 KB
/
gaymerconnect_test.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
from selenium import webdriver
import sys
import string
import time
import random
import logging
class TestSuite():
"""Defines the tests to be run upon init.
Run tests by calling run() method
"""
def __init__(self):
"""Preload browser, generate user credentials and define tests"""
#set up logging
logging.basicConfig(format="%(asctime)s - %(levelname)s: %(message)s", level=logging.DEBUG)
#spawn a browser
self.browser = self._generate_browser()
self._setup()
def _generate_browser(self):
"""Load Firefox and set the self.browser variable"""
browser = webdriver.Chrome()
logging.debug("Launching browser...")
return browser
def run(self):
"""Runs tests, then runs cleanup.
Returns tuple of dicts, failures and passes.
"""
#run tests
failures = []
try:
for test in self.tests:
(name, func) = test
logging.info("{0} - Starting test...".format(name))
try:
func()
except AssertionError:
logging.warning("{0} - Test failed!".format(name))
failures.append(name)
finally:
self.browser.save_screenshot("{0}.png".format(func.__name__))
html = open("{0}.html".format(func.__name__), 'w')
html.write(self.browser.page_source.encode('ascii', 'ignore'))
html.close()
logging.debug("Sleeping for five seconds")
time.sleep(5)
except:
pass
#run cleanup
try:
for task in self.cleanup:
(name, func) = task
logging.info("{0} - Cleaning up...".format(name))
try:
func()
except:
logging.warning("{0} - Cleanup failed!".format(name))
failures.append(name)
finally:
self.browser.save_screenshot("{0}.png".format(func.__name__))
html = open("{0}.html".format(func.__name__), 'w')
html.write(self.browser.page_source.encode('ascii', 'ignore'))
html.close()
logging.debug("Sleeping for five seconds")
time.sleep(5)
finally:
self.browser.quit()
passes = [x for (x, f) in (self.tests + self.cleanup) if x not in failures]
return (failures, passes)
class GaymerConnectTestSuite(TestSuite):
"""Tests for GaymerConnect.com"""
def _setup(self):
"""Define the tests"""
self.base_url = "http://www.gaymerconnect.com"
self.tests = (('Create User', self.create_user),
('Create Group', self.create_group),
('Create Post', self.create_post),
('Comment on Post', self.comment_on_post))
self.cleanup = (('Delete Comment', self.delete_comment),
('Delete Post', self.delete_post),
('Delete Group', self.delete_group),
('Delete User', self.delete_user))
def _generate_random_characters(self, length):
return ''.join(random.choice(string.ascii_lowercase + string.digits) \
for x in range(length))
def _generate_email(self):
# Generate new email address
random_chars = self._generate_random_characters(12)
email = "{}@somedomain.com".format(random_chars)
logging.debug("Email is {}".format(email))
return email
def create_user(self):
"""Creates new gaymerconnect.com user"""
self.email = self._generate_email()
self.password = "real_password"
self.browser.get(self.base_url + "/users/sign_in")
logging.info("Getting {}".format(self.base_url + "/users/sign_in"))
elem = self.browser.find_element_by_link_text("Create an account")
elem.click()
elem = self.browser.find_element_by_id("user_email")
elem.click()
elem.send_keys(self.email)
logging.debug("Email set")
elem = self.browser.find_element_by_id("user_password")
elem.click()
elem.send_keys(self.password)
logging.debug("Password set")
elem = self.browser.find_element_by_id("user_password_confirmation")
elem.click()
elem.send_keys(self.password)
logging.debug("Password confirmed")
elem.submit()
logging.info("Submitting new user form")
assert "Level Up" in self.browser.page_source.encode('ascii', 'ignore')
def create_group(self):
"""Create new private group"""
self.group_name = self._generate_random_characters(16)
self.browser.get(self.base_url + "/groups/new")
self.browser.find_element_by_id("group_name").clear()
self.browser.find_element_by_id("group_name").send_keys(self.group_name)
logging.debug("Group name set: {}".format(self.group_name))
self.browser.find_element_by_id("group_private").click()
self.browser.find_element_by_name("commit").click()
assert "Group created" in self.browser.page_source.encode('ascii', 'ignore')
def create_post(self):
"""Create a post in the group we just made"""
self.browser.get(self.base_url + "/groups/{0}/posts/new".format(self.group_name))
self.browser.find_element_by_id("group_post_title").clear()
self.browser.find_element_by_id("group_post_title").send_keys("Test Post")
self.browser.find_element_by_id("group_post_content").clear()
self.browser.find_element_by_id("group_post_content").send_keys("Test Message")
self.browser.find_element_by_id("group_post_content").submit()
assert "Post created" in self.browser.page_source.encode('ascii', 'ignore')
def comment_on_post(self):
"""Create a comment in the post we just made"""
self.browser.get(self.base_url + "/groups/{0}".format(self.group_name))
self.browser.find_element_by_link_text("Test Post").click()
self.browser.find_element_by_id("group_comment_content").clear()
self.browser.find_element_by_id("group_comment_content").send_keys("Test Comment")
self.browser.find_element_by_id("group_comment_content").submit()
assert "Comment posted" in self.browser.page_source.encode('ascii', 'ignore')
def delete_comment(self):
"""Delete the comment that we just made"""
self.browser.get(self.base_url + "/groups/{0}".format(self.group_name))
self.browser.find_element_by_link_text("Test Post").click()
self.browser.find_element_by_css_selector("td.span2 > a.btn.btn-danger").click()
self.browser.switch_to_alert().accept()
time.sleep(5)
assert "Comment deleted" in self.browser.page_source.encode('ascii', 'ignore')
def delete_post(self):
"""Delete the post that we just made"""
self.browser.get(self.base_url + "/groups/{0}".format(self.group_name))
self.browser.find_element_by_link_text("Test Post").click()
self.browser.find_element_by_link_text("Delete").click()
self.browser.switch_to_alert().accept()
time.sleep(5)
assert "Post deleted" in self.browser.page_source.encode('ascii', 'ignore')
def delete_group(self):
"""Delete the group that we made"""
self.browser.get(self.base_url + "/groups/{0}".format(self.group_name))
self.browser.find_element_by_link_text("Delete Group").click()
self.browser.switch_to_alert().accept()
time.sleep(5)
assert "Group deleted" in self.browser.page_source.encode('ascii', 'ignore')
def delete_user(self):
"""Delete the user that we made"""
self.browser.get(self.base_url + "/users/edit")
logging.debug('Clicking on Settings Tab')
self.browser.find_element_by_link_text("Settings").click()
logging.debug('Clicking on Cancel my account')
self.browser.find_element_by_link_text("Cancel my account").click()
logging.debug('Accepting alert')
self.browser.switch_to_alert().accept()
time.sleep(5)
assert "Come back soon" in self.browser.page_source.encode('ascii', 'ignore')
if __name__=="__main__":
gaymer_connect_test = GaymerConnectTestSuite()
failures, passes = gaymer_connect_test.run()
print "There were {0} passes".format(len(passes))
print passes
if len(failures) > 0:
print "There were {0} failures".format(len(failures))
print failures
sys.exit(1)
else:
sys.exit(0)