VelogaWhite commited on
Commit
6b63775
·
1 Parent(s): 748a538

Change FT to CalculatorTest

Browse files
Files changed (1) hide show
  1. 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 NewVisitorTest(StaticLiveServerTestCase):
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
- # This is your new helper method
23
- def wait_for_row_in_list_table(self, row_text):
24
  start_time = time.time()
25
  while True:
26
  try:
27
- table = self.browser.find_element(By.ID, "id_list_table")
28
- rows = table.find_elements(By.TAG_NAME, "tr")
29
- self.assertIn(row_text, [row.text for row in rows])
30
  return
31
  except (AssertionError, WebDriverException):
32
  if time.time() - start_time > MAX_WAIT:
33
  raise
34
- time.sleep(2.5)
35
 
36
- def test_layout_and_styling(self):
37
- # Edith goes to the home page,
 
38
  self.browser.get(self.live_server_url)
39
 
40
- # Her browser window is set to a very specific size
41
- self.browser.set_window_size(1024, 768)
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("To-Do", header_text)
70
-
71
- # He is invited to enter a to-do item straight away
72
- inputbox = self.browser.find_element(By.ID, "id_new_item")
73
- self.assertEqual(inputbox.get_attribute("placeholder"), "Enter a to-do item")
74
-
75
- # He types "Buy Gearboxes" into a text box
76
- inputbox.send_keys("Buy Gearboxes")
77
-
78
- # He check for Priority
79
- inputbox = self.browser.find_element(By.ID, "id_new_priority")
80
- self.assertEqual(inputbox.get_attribute("placeholder"), "Enter a to-do priority")
81
-
82
- # He types "High" into a priority's text box
83
- inputbox.send_keys("High")
84
-
85
- # When he hits enter, the page updates, and now the page lists
86
- inputbox.send_keys(Keys.ENTER)
87
- self.wait_for_row_in_list_table("1: Buy Gearboxes (High)")
88
-
89
- # There is still a text box inviting him to add another item.
90
- # He enters "Use Gearboxes to make a Machine"
91
- inputbox = self.browser.find_element(By.ID, "id_new_item")
92
- inputbox.send_keys("Use Gearboxes to make a Machine")
93
-
94
- # There is also a priority box waiting for him to add.
95
- # He enters "Medium"
96
- inputbox = self.browser.find_element(By.ID, "id_new_priority")
97
- inputbox.send_keys("Medium")
98
- inputbox.send_keys(Keys.ENTER)
99
-
100
- self.wait_for_row_in_list_table("2: Use Gearboxes to make a Machine (Medium)")
101
- self.wait_for_row_in_list_table("1: Buy Gearboxes (High)")
102
-
103
- # Satisfied, he goes back to play game.
104
-
105
- def test_multiple_users_can_start_lists_at_different_urls(self):
106
- # Jack starts a new to-do list
107
- self.browser.get(self.live_server_url)
108
- inputbox = self.browser.find_element(By.ID, "id_new_item")
109
- inputbox.send_keys("Buy Gearboxes")
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")