Spaces:
Sleeping
Sleeping
File size: 5,882 Bytes
ebcdde9 6b88f68 58b6f20 a1191d9 58b6f20 8249c1e 6b88f68 a1191d9 6b63775 8266708 58b6f20 8249c1e f45fc66 58b6f20 f45fc66 6b63775 a1191d9 6b63775 a1191d9 abb5328 8ffbf47 6b63775 095529e 6b63775 8266708 6b63775 979df8e 6b63775 979df8e abb5328 979df8e | 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 | from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
import time
import os
MAX_WAIT = 5
class CalculatorTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
if test_server := os.environ.get("TEST_SERVER"):
self.live_server_url = "http://" + test_server
def tearDown(self):
self.browser.quit()
# Changed from 'wait_for_row' to 'wait_for_result'
def wait_for_result(self, expected_text):
start_time = time.time()
while True:
try:
# We expect the result to be shown in an element with id="id_result"
result_element = self.browser.find_element(By.ID, "id_result")
self.assertIn(expected_text, result_element.text)
return
except (AssertionError, WebDriverException):
if time.time() - start_time > MAX_WAIT:
raise
time.sleep(0.5)
def wait_for_alert(self, expected_text):
start_time = time.time()
while True:
try:
# Focus on Alert
alert = self.browser.switch_to.alert
# Check Alert Text
self.assertIn(expected_text, alert.text)
time.sleep(0.5)
# Press Ok
alert.accept()
return
except (AssertionError, NoAlertPresentException):
if time.time() - start_time > MAX_WAIT:
raise
time.sleep(0.5)
def test_can_perform_calculations(self):
# Jack needs to calculate some numbers.
# He goes to the homepage of the calculator app
self.browser.get(self.live_server_url)
# He notices the page title and header mention "Calculator"
self.assertIn("Calculator", self.browser.title)
header_text = self.browser.find_element(By.TAG_NAME, "h1").text
self.assertIn("Calculator", header_text)
# He sees two input boxes to enter numbers
num_input_1 = self.browser.find_element(By.ID, "id_number_1")
num_input_2 = self.browser.find_element(By.ID, "id_number_2")
self.assertEqual(num_input_1.get_attribute("placeholder"), "Enter first number")
self.assertEqual(num_input_2.get_attribute("placeholder"), "Enter second number")
# He types 10 into the first box and 5 into the second
num_input_1.send_keys("10")
num_input_2.send_keys("5")
# He sees two buttons: "Sum" and "Subtract"
sum_button = self.browser.find_element(By.ID, "id_btn_sum")
sub_button = self.browser.find_element(By.ID, "id_btn_subtract")
# He clicks the "Sum" button
sum_button.click()
# The page updates and shows the result "Result: 15"
self.wait_for_result("Result: 15")
time.sleep(1)
# Satisfied with addition, he decides to test subtraction.
# He clears the inputs and enters new numbers: 20 and 8
num_input_1 = self.browser.find_element(By.ID, "id_number_1")
num_input_2 = self.browser.find_element(By.ID, "id_number_2")
num_input_1.clear()
num_input_2.clear()
num_input_1.send_keys("20")
num_input_2.send_keys("8")
# He clicks the "Subtract" button.
sub_button = self.browser.find_element(By.ID, "id_btn_subtract")
sub_button.click()
# The page updates and shows the result "Result: 12"
self.wait_for_result("Result: 12")
time.sleep(1)
# Satisfied with subtraction, he decides to test multiplication
# He clears the inputs and enters new numbers: 8 and 10
num_input_1 = self.browser.find_element(By.ID, "id_number_1")
num_input_2 = self.browser.find_element(By.ID, "id_number_2")
num_input_1.clear()
num_input_2.clear()
num_input_1.send_keys("8")
num_input_2.send_keys("10")
# He clicks the "Multiplication" button.
sub_button = self.browser.find_element(By.ID, "id_btn_multiplication")
sub_button.click()
# The page updates and shows the result "Result: 12"
self.wait_for_result("Result: 80")
time.sleep(1)
# Satisfied with multiplication, he decides to test division
# He clears the inputs and enters new numbers: 80 and 8
num_input_1 = self.browser.find_element(By.ID, "id_number_1")
num_input_2 = self.browser.find_element(By.ID, "id_number_2")
num_input_1.clear()
num_input_2.clear()
num_input_1.send_keys("80")
num_input_2.send_keys("8")
# He clicks the "Division" button.
sub_button = self.browser.find_element(By.ID, "id_btn_division")
sub_button.click()
# The page updates and shows the result "Result: 12"
self.wait_for_result("Result: 10")
time.sleep(1)
# Still Not Satisfied with Division, he decides to test DivideBy0
# He clears the inputs and enters new numbers: 100 and 0
num_input_1 = self.browser.find_element(By.ID, "id_number_1")
num_input_2 = self.browser.find_element(By.ID, "id_number_2")
num_input_1.clear()
num_input_2.clear()
num_input_1.send_keys("100")
num_input_2.send_keys("0")
# He clicks the "Division" button.
sub_button = self.browser.find_element(By.ID, "id_btn_division")
sub_button.click()
# The page updates and shows the result "Result: 12"
self.wait_for_alert("Please Do not Enter Num2 as 0")
time.sleep(1) |