-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional_tests.py
203 lines (173 loc) · 8.01 KB
/
functional_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
"""File: functional_tests.py
https://www.obeythetestinggoat.com/book/chapter_01.html
https://www.guru99.com/selenium-python.html
https://docs.djangoproject.com/en/3.1/intro/tutorial05/#tests-don-t-just-identify-problems-they-prevent-them
Test order: https://stackoverflow.com/q/4095319/888033
Remember! In order to run this suite of tests, you must
first set up dev server in the directory of this file
(Django project root) with command :
$ python manage.py runserver
Then run this script (as a script, still in root) with command:
$ python functional_tests.py
Alternatively, run as a module, (exactly?) the same outcome:
$ python -m functional_tests
Note: "-m" but no ".py"
"""
# Simulated web browser user
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
### https://selenium-python.readthedocs.io/api.html#selenium.webdriver.support.wait.WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
# Test runner (deploying the WebDriver)
import unittest
class SelectStudentTest(unittest.TestCase):
"""Goat book example
https://www.obeythetestinggoat.com/book/chapter_02_unittest.html
ResourceWarning: https://github.com/deepmind/pysc2/issues/243
- The warning is a warning, no error. Keep calm and carry on.
- Appears in conjunction with a "timeout.py". Could it be that the web driver is too slow,
e.g. to close down window??
"""
def setUp(self, student_id=7):
self.browser = webdriver.Firefox(
firefox_binary='/home/morten/firefox/firefox')
self.browser.get('http://localhost:8000')
self.wait = WebDriverWait(self.browser, 20)
# Find INPUT field of type 'radio' in FORM.
# The field must have ID == student_id.
# selenium.webdriver.common.by.By
# https://selenium-python.readthedocs.io/api.html#locate-elements-by
first_button = self.browser.find_element(
By.CSS_SELECTOR,
f'input[type="radio"][id="elev{student_id}"]'
)
first_button.click()
def tearDown(self):
self.browser.quit()
def test_landing_page(self):
""" Edith goes to landing page of boxplot app.
"""
# She reads the title in browser tab
self.assertEqual('Demo boxplot', self.browser.title)
def test_landing_page_has_home_links(self):
""" Edith goes to landing page of boxplot app, finds link to same page.
"""
self.browser.get('http://localhost:8000')
# in the footer, a link back to the index is displayed
# Deprecated: assertDictContainsSubset() <- https://github.com/pylover/restfulpy/issues/186
# https://selenium-python.readthedocs.io/locating-elements.html
#
# TypeError: argument of type 'FirefoxWebElement' is not iterable
home_link = self.browser.find_element_by_link_text('Demo startside')
# Type: https://www.selenium.dev/documentation/en/webdriver/web_element/
# NB: landing_page_link.get_attribute('href') gives ABSOLUTE URL
# 'http://localhost:8000/', NOT the RELATIVE string '/' entered in template HTML!
self.assertEqual(
home_link.get_attribute('href'),
'http://localhost:8000/'
)
def landing_page_has_github_link(self):
""" Edith goes to landing page of boxplot app, finds link to GitHub repo.
"""
# in the footer, a link to Github/engelsmann/boxplot is displayed
# self.browser.get('http://localhost:8000')
github_link = self.browser.find_element_by_link_text('GitHub repo')
self.assertEqual(
github_link.get_attribute('href'),
'https://github.com/engelsmann/boxplot'
)
def test_can_select_a_student(self, student_name='Helle Byskov', student_id=7):
"""You click on a student's name -> The correct radio button is selected
https://www.guru99.com/accessing-forms-in-webdriver.html
https://www.guru99.com/checkbox-and-radio-button-webdriver.html
"""
student_radio = self.browser.find_element_by_css_selector(
f"input[type='radio'][id='elev{student_id}']"
)
student_radio.click()
self.assertTrue(
student_radio.is_selected()
)
# Selecting a student who has not received assesment is handled
# Selecting a student who has not received assesment (KeyError) results in meaningful message
def test_keyerror_on_student_not_assessed(self, student_name='Andersine Andersen', student_id=1):
"""Page not reached if student selected did not receive assessment
"""
self.browser = webdriver.Firefox(
firefox_binary='/home/morten/firefox/firefox')
self.browser.get("http://127.1:8000")
student_radio_button = self.wait.until(
presence_of_element_located(
(By.CSS_SELECTOR,
f'input[type="radio"][id="elev{student_id}"]')
)
)
student_radio_button.click()
form_submit = self.browser.find_element_by_css_selector(
"input[type='submit']"
)
form_submit.click()
self.assertGreater(
self.browser.title.find("KeyError"),
-1,
"Proceeding to chart page with "+student_name+" did not raise KeyError as expected."
)
# Tried, unsuccesful ...
# https://stackoverflow.com/a/6103983/888033
#with self.assertRaises(KeyError):
# form_submit.click()
# AssertionError: KeyError not raised
def test_h2_after_submitted(self, student_name='Helle Byskov', student_id=7, assignment_title="covid"):
# Page after submit shows H2 headline tellling the assignment title
self.browser.get("http://127.1:8000")
student_radio = self.browser.find_element_by_css_selector(
f"input[type='radio'][id='elev{student_id}']"
)
student_radio.click()
form_submit = self.browser.find_element_by_css_selector(
"input[type='submit']"
)
form_submit.click()
page_title = self.browser.find_element_by_tag_name(
"h2"
).get_attribute("innerHTML")
self.assertGreater(
page_title.find(assignment_title),
-1,
f"Assignment '{assignment_title}'' not found in H2 headline."
)
def test_img_after_submitted(self, student_name='Helle Byskov', student_id=7):
# E: timeout
# A chart is displayed, that is: A HTML tag named "img" is present.
self.browser = webdriver.Firefox(
firefox_binary='/home/morten/firefox/firefox'
)
self.browser.get("http://127.1:8000")
student_radio = self.browser.find_element_by_css_selector(
f"input[type='radio'][id='elev{student_id}']"
)
student_radio.click()
form_submit = self.browser.find_element_by_css_selector(
"input[type='submit']"
)
form_submit.click()
# The image embedded as string
img_element = self.browser.find_element_by_tag_name("img")
src = img_element.get_attribute('src')
# The expected beginning of that string
needle = 'image/png;'
self.assertGreater(
src.find(needle),
-1, # Not found
"Image string should start with code including 'image/png'."
)
# In the footer, a link back to the index is displayed
# In the footer, a link to Github/engelsmann/boxplot is displayed
#self.fail(f'Fails, because it is asked to. Reminds you: "Finish writing the test!"')
if __name__ == '__main__':
### The unittest test runner, which will automatically find test classes and methods in the file and run them.
unittest.main()