1
0
mirror of https://github.com/django/django.git synced 2025-07-09 20:29:12 +00:00

Refs #34043 -- Added context managers to SeleniumTestCase for changing window size.

This commit is contained in:
Sarah Boyce 2023-10-13 10:21:34 +02:00 committed by Mariusz Felisiak
parent 20b7aac7ca
commit f6629ee2c9
3 changed files with 166 additions and 137 deletions

View File

@ -98,6 +98,20 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
return self.import_webdriver(self.browser)(options=options) return self.import_webdriver(self.browser)(options=options)
class ChangeWindowSize:
def __init__(self, width, height, selenium):
self.selenium = selenium
self.new_size = (width, height)
def __enter__(self):
self.old_size = self.selenium.get_window_size()
self.selenium.set_window_size(*self.new_size)
return self
def __exit__(self, exc_type, exc_value, traceback):
self.selenium.set_window_size(self.old_size["width"], self.old_size["height"])
@tag("selenium") @tag("selenium")
class SeleniumTestCase(LiveServerTestCase, metaclass=SeleniumTestCaseBase): class SeleniumTestCase(LiveServerTestCase, metaclass=SeleniumTestCaseBase):
implicit_wait = 10 implicit_wait = 10
@ -118,6 +132,21 @@ class SeleniumTestCase(LiveServerTestCase, metaclass=SeleniumTestCaseBase):
super().setUpClass() super().setUpClass()
cls.addClassCleanup(cls._quit_selenium) cls.addClassCleanup(cls._quit_selenium)
@contextmanager
def desktop_size(self):
with ChangeWindowSize(1280, 720, self.selenium):
yield
@contextmanager
def small_screen_size(self):
with ChangeWindowSize(1024, 768, self.selenium):
yield
@contextmanager
def mobile_size(self):
with ChangeWindowSize(360, 800, self.selenium):
yield
@classmethod @classmethod
def _quit_selenium(cls): def _quit_selenium(cls):
# quit() the WebDriver before attempting to terminate and join the # quit() the WebDriver before attempting to terminate and join the

View File

@ -6489,14 +6489,10 @@ class SeleniumTests(AdminSeleniumTestCase):
) )
self.selenium.get(self.live_server_url + reverse("admin:admin_views_story_add")) self.selenium.get(self.live_server_url + reverse("admin:admin_views_story_add"))
field_title = self.selenium.find_element(By.CLASS_NAME, "field-title") field_title = self.selenium.find_element(By.CLASS_NAME, "field-title")
current_size = self.selenium.get_window_size() with self.small_screen_size():
try:
self.selenium.set_window_size(1024, 768)
self.assertIs(field_title.is_displayed(), False) self.assertIs(field_title.is_displayed(), False)
self.selenium.set_window_size(767, 575) with self.mobile_size():
self.assertIs(field_title.is_displayed(), False) self.assertIs(field_title.is_displayed(), False)
finally:
self.selenium.set_window_size(current_size["width"], current_size["height"])
def test_updating_related_objects_updates_fk_selects_except_autocompletes(self): def test_updating_related_objects_updates_fk_selects_except_autocompletes(self):
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By

View File

@ -1083,7 +1083,6 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
""" """
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
self.selenium.set_window_size(1024, 768)
self.admin_login(username="super", password="secret", login_url="/") self.admin_login(username="super", password="secret", login_url="/")
# Enter test data # Enter test data
@ -1096,6 +1095,8 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
path = os.path.join( path = os.path.join(
os.path.dirname(import_module("django.contrib.admin").__file__), "locale" os.path.dirname(import_module("django.contrib.admin").__file__), "locale"
) )
url = reverse("admin:admin_widgets_member_change", args=(member.pk,))
with self.small_screen_size():
for language_code, language_name in settings.LANGUAGES: for language_code, language_name in settings.LANGUAGES:
try: try:
catalog = gettext.translation("djangojs", path, [language_code]) catalog = gettext.translation("djangojs", path, [language_code])
@ -1106,18 +1107,17 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
else: else:
month_name = month_string month_name = month_string
# Get the expected caption # Get the expected caption.
may_translation = month_name may_translation = month_name
expected_caption = "{:s} {:d}".format(may_translation.upper(), 1984) expected_caption = "{:s} {:d}".format(may_translation.upper(), 1984)
# Test with every locale # Every locale.
with override_settings(LANGUAGE_CODE=language_code): with override_settings(LANGUAGE_CODE=language_code):
# Open a page that has a date picker widget # Open a page that has a date picker widget.
url = reverse("admin:admin_widgets_member_change", args=(member.pk,))
self.selenium.get(self.live_server_url + url) self.selenium.get(self.live_server_url + url)
# Click on the calendar icon # Click on the calendar icon.
self.selenium.find_element(By.ID, "calendarlink0").click() self.selenium.find_element(By.ID, "calendarlink0").click()
# Make sure that the right month and year are displayed # The right month and year are displayed.
self.wait_for_text("#calendarin0 caption", expected_caption) self.wait_for_text("#calendarin0 caption", expected_caption)
@ -1416,10 +1416,10 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
def test_basic(self): def test_basic(self):
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
self.selenium.set_window_size(1024, 768)
self.school.students.set([self.lisa, self.peter]) self.school.students.set([self.lisa, self.peter])
self.school.alumni.set([self.lisa, self.peter]) self.school.alumni.set([self.lisa, self.peter])
with self.small_screen_size():
self.admin_login(username="super", password="secret", login_url="/") self.admin_login(username="super", password="secret", login_url="/")
self.selenium.get( self.selenium.get(
self.live_server_url self.live_server_url
@ -1430,7 +1430,8 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
self.execute_basic_operations("vertical", "students") self.execute_basic_operations("vertical", "students")
self.execute_basic_operations("horizontal", "alumni") self.execute_basic_operations("horizontal", "alumni")
# Save and check that everything is properly stored in the database --- # Save, everything should be stored properly stored in the
# database.
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click() self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.wait_page_ready() self.wait_page_ready()
self.school = School.objects.get(id=self.school.id) # Reload from database self.school = School.objects.get(id=self.school.id) # Reload from database
@ -1451,10 +1452,10 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
self.selenium.set_window_size(1024, 768)
self.school.students.set([self.lisa, self.peter]) self.school.students.set([self.lisa, self.peter])
self.school.alumni.set([self.lisa, self.peter]) self.school.alumni.set([self.lisa, self.peter])
with self.small_screen_size():
self.admin_login(username="super", password="secret", login_url="/") self.admin_login(username="super", password="secret", login_url="/")
self.selenium.get( self.selenium.get(
self.live_server_url self.live_server_url
@ -1467,8 +1468,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
choose_link = "id_%s_add_link" % field_name choose_link = "id_%s_add_link" % field_name
remove_link = "id_%s_remove_link" % field_name remove_link = "id_%s_remove_link" % field_name
input = self.selenium.find_element(By.ID, "id_%s_input" % field_name) input = self.selenium.find_element(By.ID, "id_%s_input" % field_name)
# Initial values.
# Initial values
self.assertSelectOptions( self.assertSelectOptions(
from_box, from_box,
[ [
@ -1480,16 +1480,14 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
str(self.john.id), str(self.john.id),
], ],
) )
# Typing in some characters filters out non-matching options.
# Typing in some characters filters out non-matching options
input.send_keys("a") input.send_keys("a")
self.assertSelectOptions( self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.jason.id)] from_box, [str(self.arthur.id), str(self.jason.id)]
) )
input.send_keys("R") input.send_keys("R")
self.assertSelectOptions(from_box, [str(self.arthur.id)]) self.assertSelectOptions(from_box, [str(self.arthur.id)])
# Clearing the text box makes the other options reappear.
# Clearing the text box makes the other options reappear
input.send_keys([Keys.BACK_SPACE]) input.send_keys([Keys.BACK_SPACE])
self.assertSelectOptions( self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.jason.id)] from_box, [str(self.arthur.id), str(self.jason.id)]
@ -1507,7 +1505,6 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
], ],
) )
# -----------------------------------------------------------------
# Choosing a filtered option sends it properly to the 'to' box. # Choosing a filtered option sends it properly to the 'to' box.
input.send_keys("a") input.send_keys("a")
self.assertSelectOptions( self.assertSelectOptions(
@ -1527,8 +1524,12 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
self.select_option(to_box, str(self.lisa.id)) self.select_option(to_box, str(self.lisa.id))
self.selenium.find_element(By.ID, remove_link).click() self.selenium.find_element(By.ID, remove_link).click()
self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.lisa.id)]) self.assertSelectOptions(
self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)]) from_box, [str(self.arthur.id), str(self.lisa.id)]
)
self.assertSelectOptions(
to_box, [str(self.peter.id), str(self.jason.id)]
)
input.send_keys([Keys.BACK_SPACE]) # Clear text box input.send_keys([Keys.BACK_SPACE]) # Clear text box
self.assertSelectOptions( self.assertSelectOptions(
@ -1542,9 +1543,10 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
str(self.lisa.id), str(self.lisa.id),
], ],
) )
self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)]) self.assertSelectOptions(
to_box, [str(self.peter.id), str(self.jason.id)]
)
# -----------------------------------------------------------------
# Pressing enter on a filtered option sends it properly to # Pressing enter on a filtered option sends it properly to
# the 'to' box. # the 'to' box.
self.select_option(to_box, str(self.jason.id)) self.select_option(to_box, str(self.jason.id))
@ -1552,10 +1554,12 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
input.send_keys("ja") input.send_keys("ja")
self.assertSelectOptions(from_box, [str(self.jason.id)]) self.assertSelectOptions(from_box, [str(self.jason.id)])
input.send_keys([Keys.ENTER]) input.send_keys([Keys.ENTER])
self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)]) self.assertSelectOptions(
to_box, [str(self.peter.id), str(self.jason.id)]
)
input.send_keys([Keys.BACK_SPACE, Keys.BACK_SPACE]) input.send_keys([Keys.BACK_SPACE, Keys.BACK_SPACE])
# Save and check that everything is properly stored in the database --- # Save, everything should be stored properly in the database.
with self.wait_page_loaded(): with self.wait_page_loaded():
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click() self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.school = School.objects.get(id=self.school.id) # Reload from database self.school = School.objects.get(id=self.school.id) # Reload from database