Spaces:
Sleeping
Sleeping
Download src/functional_tests/tests.py from pruek0704/django-docker: direct link, hf CLI and curl.
- Browser
- Download file 6.79 kB
-
https://huggingface.co/spaces/pruek0704/django-docker/resolve/main/src/functional_tests/tests.py
- Command line
-
hf download hf://spaces/pruek0704/django-docker/src/functional_tests/tests.py
-
curl -L -o tests.py https://huggingface.co/spaces/pruek0704/django-docker/resolve/main/src/functional_tests/tests.py
6.79 kB
| import os | |
| import unittest | |
| from selenium import webdriver | |
| from selenium.webdriver.common.by import By | |
| from selenium.webdriver.common.keys import Keys | |
| import time | |
| from django.contrib.staticfiles.testing import StaticLiveServerTestCase | |
| from selenium.common.exceptions import WebDriverException | |
| from selenium.webdriver.support.ui import Select | |
| MAX_WAIT = 10 | |
| class NewVisitorTest(StaticLiveServerTestCase): | |
| def setUp(self): | |
| self.browser = webdriver.Chrome() | |
| # ตรวจสอบว่ามีตัวแปร environment ชื่อ TEST_SERVER หรือไม่ | |
| if test_server := os.environ.get("TEST_SERVER"): | |
| # ถ้ามี ให้เปลี่ยน live_server_url เป็นที่อยู่ของ server นั้น | |
| self.live_server_url = "http://" + test_server | |
| def tearDown(self): | |
| self.browser.quit() | |
| def check_for_row_in_list_table(self, row_text): | |
| table = self.browser.find_element(By.ID, "id_list_table") | |
| rows = table.find_elements(By.TAG_NAME, "tr") | |
| self.assertIn(row_text, [row.text for row in rows]) | |
| def test_can_start_a_todo_list(self): | |
| # Edith has heard about a cool new online to-do app. | |
| # She goes to check out its homepage | |
| self.browser.get(self.live_server_url) | |
| # She notices the page title and header mention to-do lists | |
| self.assertIn("To-Do", self.browser.title) | |
| header_text = self.browser.find_element(By.TAG_NAME, "h1").text | |
| self.assertIn("To-Do", header_text) | |
| # She is invited to enter a to-do item straight away | |
| inputbox = self.browser.find_element(By.ID, "id_new_item") | |
| self.assertEqual(inputbox.get_attribute("placeholder"), "Enter a to-do item") | |
| # She types "Buy peacock feathers" into a text box | |
| # (Edith's hobby is tying fly-fishing lures) | |
| inputbox.send_keys("Buy peacock feathers") | |
| # ***จำลองการเลือก Priority เป็น High*** | |
| select_box = Select(self.browser.find_element(By.ID, "id_priority")) | |
| select_box.select_by_visible_text('High') | |
| # When she hits enter, the page updates, and now the page lists | |
| # "1: Buy peacock feathers" as an item in a to-do list table | |
| inputbox.send_keys(Keys.ENTER) | |
| time.sleep(1) | |
| #***ตรวจสอบว่าต้องเจอคำว่า (High) ในตาราง*** | |
| self.wait_for_row_in_list_table("1: Buy peacock feathers (High)") | |
| # table = self.browser.find_element(By.ID, "id_list_table") | |
| # rows = table.find_elements(By.TAG_NAME, "tr") | |
| # self.assertTrue( | |
| # any(row.text == "1: Buy peacock feathers" for row in rows), | |
| # f"New to-do item did not appear in table. Contents were:\n{table.text}", | |
| # ) | |
| # There is still a text box inviting her to add another item. | |
| # She enters "Use peacock feathers to make a fly" | |
| # (Edith is very methodical) | |
| inputbox = self.browser.find_element(By.ID, "id_new_item") | |
| inputbox.send_keys("Use peacock feathers to make a fly") | |
| inputbox.send_keys(Keys.ENTER) | |
| time.sleep(1) | |
| self.wait_for_row_in_list_table("1: Buy peacock feathers (High)") | |
| self.wait_for_row_in_list_table("2: Use peacock feathers to make a fly (Medium)") | |
| def wait_for_row_in_list_table(self, row_text): | |
| start_time = time.time() | |
| while True: | |
| try: | |
| table = self.browser.find_element(By.ID, "id_list_table") | |
| rows = table.find_elements(By.TAG_NAME, "tr") | |
| self.assertIn(row_text, [row.text for row in rows]) | |
| return | |
| except (AssertionError, WebDriverException): | |
| if time.time() - start_time > MAX_WAIT: | |
| raise | |
| time.sleep(0.5) | |
| def test_multiple_users_can_start_lists_at_different_urls(self): | |
| # Edith starts a new to-do list | |
| self.browser.get(self.live_server_url) | |
| inputbox = self.browser.find_element(By.ID, "id_new_item") | |
| inputbox.send_keys("Buy peacock feathers") | |
| inputbox.send_keys(Keys.ENTER) | |
| self.wait_for_row_in_list_table("1: Buy peacock feathers (Medium)") | |
| # She notices that her list has a unique URL | |
| edith_list_url = self.browser.current_url | |
| self.assertRegex(edith_list_url, "/lists/.+") | |
| # Now a new user, Francis, comes along to the site. | |
| ## We delete all the browser's cookies | |
| ## as a way of simulating a brand new user session | |
| self.browser.delete_all_cookies() | |
| # Francis visits the home page. There is no sign of Edith's | |
| # list | |
| self.browser.get(self.live_server_url) | |
| page_text = self.browser.find_element(By.TAG_NAME, "body").text | |
| self.assertNotIn("Buy peacock feathers", page_text) | |
| # Francis starts a new list by entering a new item. He | |
| # is less interesting than Edith... | |
| inputbox = self.browser.find_element(By.ID, "id_new_item") | |
| inputbox.send_keys("Buy milk") | |
| inputbox.send_keys(Keys.ENTER) | |
| # รอคำว่า medium | |
| self.wait_for_row_in_list_table("1: Buy milk (Medium)") | |
| # Francis gets his own unique URL | |
| francis_list_url = self.browser.current_url | |
| self.assertRegex(francis_list_url, "/lists/.+") | |
| self.assertNotEqual(francis_list_url, edith_list_url) | |
| # Again, there is no trace of Edith's list | |
| page_text = self.browser.find_element(By.TAG_NAME, "body").text | |
| self.assertNotIn("Buy peacock feathers", page_text) | |
| self.assertIn("Buy milk", page_text) | |
| # Satisfied, they both go back to sleep | |
| def test_layout_and_styling(self): | |
| # Edith goes to the home page, | |
| self.browser.get(self.live_server_url) | |
| # Her browser window is set to a very specific size | |
| self.browser.set_window_size(1024, 768) | |
| # She notices the input box is nicely centered | |
| inputbox = self.browser.find_element(By.ID, "id_new_item") | |
| self.assertAlmostEqual( | |
| inputbox.location["x"] + inputbox.size["width"] / 2, | |
| 512, | |
| delta=30, | |
| ) | |
| # She starts a new list and sees the input is nicely | |
| # centered there too | |
| inputbox.send_keys("testing") | |
| inputbox.send_keys(Keys.ENTER) | |
| self.wait_for_row_in_list_table("1: testing (Medium)") | |
| inputbox = self.browser.find_element(By.ID, "id_new_item") | |
| self.assertAlmostEqual( | |
| inputbox.location["x"] + inputbox.size["width"] / 2, | |
| 512, | |
| delta=30, | |
| ) | |