Spaces:
Sleeping
Sleeping
Commit ·
6b63775
1
Parent(s): 748a538
Change FT to CalculatorTest
Browse files- src/functional_tests/tests.py +53 -124
src/functional_tests/tests.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
| 2 |
from selenium import webdriver
|
| 3 |
from selenium.webdriver.common.by import By
|
| 4 |
-
from selenium.webdriver.common.keys import Keys
|
| 5 |
from selenium.common.exceptions import WebDriverException
|
| 6 |
import time
|
| 7 |
-
import unittest
|
| 8 |
import os
|
| 9 |
|
| 10 |
MAX_WAIT = 5
|
| 11 |
|
| 12 |
-
class
|
| 13 |
|
| 14 |
def setUp(self):
|
| 15 |
self.browser = webdriver.Firefox()
|
|
@@ -19,135 +17,66 @@ class NewVisitorTest(StaticLiveServerTestCase):
|
|
| 19 |
def tearDown(self):
|
| 20 |
self.browser.quit()
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
def
|
| 24 |
start_time = time.time()
|
| 25 |
while True:
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
self.assertIn(
|
| 30 |
return
|
| 31 |
except (AssertionError, WebDriverException):
|
| 32 |
if time.time() - start_time > MAX_WAIT:
|
| 33 |
raise
|
| 34 |
-
time.sleep(
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
#
|
|
|
|
| 38 |
self.browser.get(self.live_server_url)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
self.
|
| 42 |
-
|
| 43 |
-
# She notices the input box is nicely centered
|
| 44 |
-
inputbox = self.browser.find_element(By.ID, "id_new_item")
|
| 45 |
-
self.assertAlmostEqual(
|
| 46 |
-
inputbox.location["x"] + inputbox.size["width"] / 2,
|
| 47 |
-
512,
|
| 48 |
-
delta=30,
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
inputbox.send_keys("testing")
|
| 52 |
-
inputbox.send_keys(Keys.ENTER)
|
| 53 |
-
self.wait_for_row_in_list_table("1: testing ()")
|
| 54 |
-
inputbox = self.browser.find_element(By.ID, "id_new_item")
|
| 55 |
-
self.assertAlmostEqual(
|
| 56 |
-
inputbox.location["x"] + inputbox.size["width"] / 2,
|
| 57 |
-
512,
|
| 58 |
-
delta=30,
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
def test_can_start_a_todo_list(self):
|
| 62 |
-
# Jack has heard about a cool new online to-do app.
|
| 63 |
-
# He goes to check out its homepage
|
| 64 |
-
self.browser.get(self.live_server_url)
|
| 65 |
-
|
| 66 |
-
# He notices the page title and header mention to-do lists
|
| 67 |
-
self.assertIn("To-Do", self.browser.title)
|
| 68 |
header_text = self.browser.find_element(By.TAG_NAME, "h1").text
|
| 69 |
-
self.assertIn("
|
| 70 |
-
|
| 71 |
-
# He
|
| 72 |
-
|
| 73 |
-
self.
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# He
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# He
|
| 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 |
-
# He check for Priority
|
| 111 |
-
inputbox = self.browser.find_element(By.ID, "id_new_priority")
|
| 112 |
-
inputbox.send_keys("High")
|
| 113 |
-
inputbox.send_keys(Keys.ENTER)
|
| 114 |
-
|
| 115 |
-
self.wait_for_row_in_list_table("1: Buy Gearboxes (High)")
|
| 116 |
-
|
| 117 |
-
# He notices that his list has a unique URL
|
| 118 |
-
jack_list_url = self.browser.current_url
|
| 119 |
-
self.assertRegex(jack_list_url, "/lists/.+")
|
| 120 |
-
|
| 121 |
-
# Now a new user, Herry, comes along to the site.
|
| 122 |
-
|
| 123 |
-
## We delete all the browser's cookies
|
| 124 |
-
## as a way of simulating a brand new user session
|
| 125 |
-
self.browser.delete_all_cookies()
|
| 126 |
-
|
| 127 |
-
# Henry visits the home page. There is no sign of Edith's
|
| 128 |
-
self.browser.get(self.live_server_url)
|
| 129 |
-
page_text = self.browser.find_element(By.TAG_NAME, "body").text
|
| 130 |
-
self.assertNotIn("Buy Gearboxes (High)", page_text)
|
| 131 |
-
|
| 132 |
-
# Henry starts a new list by entering a new item.
|
| 133 |
-
inputbox = self.browser.find_element(By.ID, "id_new_item")
|
| 134 |
-
inputbox.send_keys("Buy Wheels")
|
| 135 |
-
inputbox = self.browser.find_element(By.ID, "id_new_priority")
|
| 136 |
-
inputbox.send_keys('High')
|
| 137 |
-
inputbox.send_keys(Keys.ENTER)
|
| 138 |
-
self.wait_for_row_in_list_table("1: Buy Wheels (High)")
|
| 139 |
-
|
| 140 |
-
# Henry gets his own unique URL
|
| 141 |
-
henry_list_url = self.browser.current_url
|
| 142 |
-
self.assertRegex(henry_list_url, "/lists/.+")
|
| 143 |
-
self.assertNotEqual(henry_list_url, jack_list_url)
|
| 144 |
-
|
| 145 |
-
# Again, there is no trace of Edith's list
|
| 146 |
-
page_text = self.browser.find_element(By.TAG_NAME, "body").text
|
| 147 |
-
self.assertNotIn("Buy Gearboxes", page_text)
|
| 148 |
-
self.assertIn("Buy Wheels", page_text)
|
| 149 |
-
|
| 150 |
-
# Satisfied, they both go back to sleep
|
| 151 |
-
|
| 152 |
-
if __name__ == "__main__":
|
| 153 |
-
unittest.main()
|
|
|
|
| 1 |
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
| 2 |
from selenium import webdriver
|
| 3 |
from selenium.webdriver.common.by import By
|
|
|
|
| 4 |
from selenium.common.exceptions import WebDriverException
|
| 5 |
import time
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
MAX_WAIT = 5
|
| 9 |
|
| 10 |
+
class CalculatorTest(StaticLiveServerTestCase):
|
| 11 |
|
| 12 |
def setUp(self):
|
| 13 |
self.browser = webdriver.Firefox()
|
|
|
|
| 17 |
def tearDown(self):
|
| 18 |
self.browser.quit()
|
| 19 |
|
| 20 |
+
# Changed from 'wait_for_row' to 'wait_for_result'
|
| 21 |
+
def wait_for_result(self, expected_text):
|
| 22 |
start_time = time.time()
|
| 23 |
while True:
|
| 24 |
try:
|
| 25 |
+
# We expect the result to be shown in an element with id="id_result"
|
| 26 |
+
result_element = self.browser.find_element(By.ID, "id_result")
|
| 27 |
+
self.assertIn(expected_text, result_element.text)
|
| 28 |
return
|
| 29 |
except (AssertionError, WebDriverException):
|
| 30 |
if time.time() - start_time > MAX_WAIT:
|
| 31 |
raise
|
| 32 |
+
time.sleep(0.5)
|
| 33 |
|
| 34 |
+
def test_can_perform_calculations(self):
|
| 35 |
+
# Jack needs to calculate some numbers.
|
| 36 |
+
# He goes to the homepage of the calculator app
|
| 37 |
self.browser.get(self.live_server_url)
|
| 38 |
|
| 39 |
+
# He notices the page title and header mention "Calculator"
|
| 40 |
+
self.assertIn("Calculator", self.browser.title)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
header_text = self.browser.find_element(By.TAG_NAME, "h1").text
|
| 42 |
+
self.assertIn("Calculator", header_text)
|
| 43 |
+
|
| 44 |
+
# He sees two input boxes to enter numbers
|
| 45 |
+
num_input_1 = self.browser.find_element(By.ID, "id_number_1")
|
| 46 |
+
num_input_2 = self.browser.find_element(By.ID, "id_number_2")
|
| 47 |
+
|
| 48 |
+
self.assertEqual(num_input_1.get_attribute("placeholder"), "Enter first number")
|
| 49 |
+
self.assertEqual(num_input_2.get_attribute("placeholder"), "Enter second number")
|
| 50 |
+
|
| 51 |
+
# He types 10 into the first box and 5 into the second
|
| 52 |
+
num_input_1.send_keys("10")
|
| 53 |
+
num_input_2.send_keys("5")
|
| 54 |
+
|
| 55 |
+
# He sees two buttons: "Sum" and "Subtract"
|
| 56 |
+
sum_button = self.browser.find_element(By.ID, "id_btn_sum")
|
| 57 |
+
sub_button = self.browser.find_element(By.ID, "id_btn_subtract")
|
| 58 |
+
|
| 59 |
+
# He clicks the "Sum" button
|
| 60 |
+
sum_button.click()
|
| 61 |
+
|
| 62 |
+
# The page updates and shows the result "Result: 15"
|
| 63 |
+
self.wait_for_result("Result: 15")
|
| 64 |
+
|
| 65 |
+
# Satisfied with addition, he decides to test subtraction.
|
| 66 |
+
# He clears the inputs and enters new numbers: 20 and 8
|
| 67 |
+
num_input_1 = self.browser.find_element(By.ID, "id_number_1")
|
| 68 |
+
num_input_2 = self.browser.find_element(By.ID, "id_number_2")
|
| 69 |
+
|
| 70 |
+
num_input_1.clear()
|
| 71 |
+
num_input_2.clear()
|
| 72 |
+
|
| 73 |
+
num_input_1.send_keys("20")
|
| 74 |
+
num_input_2.send_keys("8")
|
| 75 |
+
|
| 76 |
+
# He clicks the "Subtract" button.
|
| 77 |
+
# (We need to re-find the element to avoid StaleElementReferenceException if the page reloaded)
|
| 78 |
+
sub_button = self.browser.find_element(By.ID, "id_btn_subtract")
|
| 79 |
+
sub_button.click()
|
| 80 |
+
|
| 81 |
+
# The page updates and shows the result "Result: 12"
|
| 82 |
+
self.wait_for_result("Result: 12")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|