Skip to content

Commit

Permalink
Refs #34043 -- Added context managers to SeleniumTestCase for changin…
Browse files Browse the repository at this point in the history
…g window size.
  • Loading branch information
sarahboyce authored and felixxm committed Oct 16, 2023
1 parent 20b7aac commit f6629ee
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 139 deletions.
29 changes: 29 additions & 0 deletions django/test/selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ def create_webdriver(self):
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")
class SeleniumTestCase(LiveServerTestCase, metaclass=SeleniumTestCaseBase):
implicit_wait = 10
Expand All @@ -118,6 +132,21 @@ def setUpClass(cls):
super().setUpClass()
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
def _quit_selenium(cls):
# quit() the WebDriver before attempting to terminate and join the
Expand Down
8 changes: 2 additions & 6 deletions tests/admin_views/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6489,14 +6489,10 @@ def test_hidden_fields_small_window(self):
)
self.selenium.get(self.live_server_url + reverse("admin:admin_views_story_add"))
field_title = self.selenium.find_element(By.CLASS_NAME, "field-title")
current_size = self.selenium.get_window_size()
try:
self.selenium.set_window_size(1024, 768)
with self.small_screen_size():
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)
finally:
self.selenium.set_window_size(current_size["width"], current_size["height"])

def test_updating_related_objects_updates_fk_selects_except_autocompletes(self):
from selenium.webdriver.common.by import By
Expand Down
270 changes: 137 additions & 133 deletions tests/admin_widgets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,6 @@ def test_calendar_show_date_from_input(self):
"""
from selenium.webdriver.common.by import By

self.selenium.set_window_size(1024, 768)
self.admin_login(username="super", password="secret", login_url="/")

# Enter test data
Expand All @@ -1096,29 +1095,30 @@ def test_calendar_show_date_from_input(self):
path = os.path.join(
os.path.dirname(import_module("django.contrib.admin").__file__), "locale"
)
for language_code, language_name in settings.LANGUAGES:
try:
catalog = gettext.translation("djangojs", path, [language_code])
except OSError:
continue
if month_string in catalog._catalog:
month_name = catalog._catalog[month_string]
else:
month_name = month_string

# Get the expected caption
may_translation = month_name
expected_caption = "{:s} {:d}".format(may_translation.upper(), 1984)

# Test with every locale
with override_settings(LANGUAGE_CODE=language_code):
# 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)
# Click on the calendar icon
self.selenium.find_element(By.ID, "calendarlink0").click()
# Make sure that the right month and year are displayed
self.wait_for_text("#calendarin0 caption", expected_caption)
url = reverse("admin:admin_widgets_member_change", args=(member.pk,))
with self.small_screen_size():
for language_code, language_name in settings.LANGUAGES:
try:
catalog = gettext.translation("djangojs", path, [language_code])
except OSError:
continue
if month_string in catalog._catalog:
month_name = catalog._catalog[month_string]
else:
month_name = month_string

# Get the expected caption.
may_translation = month_name
expected_caption = "{:s} {:d}".format(may_translation.upper(), 1984)

# Every locale.
with override_settings(LANGUAGE_CODE=language_code):
# Open a page that has a date picker widget.
self.selenium.get(self.live_server_url + url)
# Click on the calendar icon.
self.selenium.find_element(By.ID, "calendarlink0").click()
# The right month and year are displayed.
self.wait_for_text("#calendarin0 caption", expected_caption)


@requires_tz_support
Expand Down Expand Up @@ -1416,23 +1416,24 @@ def execute_basic_operations(self, mode, field_name):
def test_basic(self):
from selenium.webdriver.common.by import By

self.selenium.set_window_size(1024, 768)
self.school.students.set([self.lisa, self.peter])
self.school.alumni.set([self.lisa, self.peter])

self.admin_login(username="super", password="secret", login_url="/")
self.selenium.get(
self.live_server_url
+ reverse("admin:admin_widgets_school_change", args=(self.school.id,))
)
with self.small_screen_size():
self.admin_login(username="super", password="secret", login_url="/")
self.selenium.get(
self.live_server_url
+ reverse("admin:admin_widgets_school_change", args=(self.school.id,))
)

self.wait_page_ready()
self.execute_basic_operations("vertical", "students")
self.execute_basic_operations("horizontal", "alumni")
self.wait_page_ready()
self.execute_basic_operations("vertical", "students")
self.execute_basic_operations("horizontal", "alumni")

# Save and check that everything is properly stored in the database ---
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.wait_page_ready()
# Save, everything should be stored properly stored in the
# database.
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.wait_page_ready()
self.school = School.objects.get(id=self.school.id) # Reload from database
self.assertEqual(
list(self.school.students.all()),
Expand All @@ -1451,113 +1452,116 @@ def test_filter(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

self.selenium.set_window_size(1024, 768)
self.school.students.set([self.lisa, self.peter])
self.school.alumni.set([self.lisa, self.peter])

self.admin_login(username="super", password="secret", login_url="/")
self.selenium.get(
self.live_server_url
+ reverse("admin:admin_widgets_school_change", args=(self.school.id,))
)

for field_name in ["students", "alumni"]:
from_box = "#id_%s_from" % field_name
to_box = "#id_%s_to" % field_name
choose_link = "id_%s_add_link" % field_name
remove_link = "id_%s_remove_link" % field_name
input = self.selenium.find_element(By.ID, "id_%s_input" % field_name)

# Initial values
self.assertSelectOptions(
from_box,
[
str(self.arthur.id),
str(self.bob.id),
str(self.cliff.id),
str(self.jason.id),
str(self.jenny.id),
str(self.john.id),
],
with self.small_screen_size():
self.admin_login(username="super", password="secret", login_url="/")
self.selenium.get(
self.live_server_url
+ reverse("admin:admin_widgets_school_change", args=(self.school.id,))
)

# Typing in some characters filters out non-matching options
input.send_keys("a")
self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.jason.id)]
)
input.send_keys("R")
self.assertSelectOptions(from_box, [str(self.arthur.id)])
for field_name in ["students", "alumni"]:
from_box = "#id_%s_from" % field_name
to_box = "#id_%s_to" % field_name
choose_link = "id_%s_add_link" % field_name
remove_link = "id_%s_remove_link" % field_name
input = self.selenium.find_element(By.ID, "id_%s_input" % field_name)
# Initial values.
self.assertSelectOptions(
from_box,
[
str(self.arthur.id),
str(self.bob.id),
str(self.cliff.id),
str(self.jason.id),
str(self.jenny.id),
str(self.john.id),
],
)
# Typing in some characters filters out non-matching options.
input.send_keys("a")
self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.jason.id)]
)
input.send_keys("R")
self.assertSelectOptions(from_box, [str(self.arthur.id)])
# Clearing the text box makes the other options reappear.
input.send_keys([Keys.BACK_SPACE])
self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.jason.id)]
)
input.send_keys([Keys.BACK_SPACE])
self.assertSelectOptions(
from_box,
[
str(self.arthur.id),
str(self.bob.id),
str(self.cliff.id),
str(self.jason.id),
str(self.jenny.id),
str(self.john.id),
],
)

# Clearing the text box makes the other options reappear
input.send_keys([Keys.BACK_SPACE])
self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.jason.id)]
)
input.send_keys([Keys.BACK_SPACE])
self.assertSelectOptions(
from_box,
[
str(self.arthur.id),
str(self.bob.id),
str(self.cliff.id),
str(self.jason.id),
str(self.jenny.id),
str(self.john.id),
],
)
# Choosing a filtered option sends it properly to the 'to' box.
input.send_keys("a")
self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.jason.id)]
)
self.select_option(from_box, str(self.jason.id))
self.selenium.find_element(By.ID, choose_link).click()
self.assertSelectOptions(from_box, [str(self.arthur.id)])
self.assertSelectOptions(
to_box,
[
str(self.lisa.id),
str(self.peter.id),
str(self.jason.id),
],
)

# -----------------------------------------------------------------
# Choosing a filtered option sends it properly to the 'to' box.
input.send_keys("a")
self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.jason.id)]
)
self.select_option(from_box, str(self.jason.id))
self.selenium.find_element(By.ID, choose_link).click()
self.assertSelectOptions(from_box, [str(self.arthur.id)])
self.assertSelectOptions(
to_box,
[
str(self.lisa.id),
str(self.peter.id),
str(self.jason.id),
],
)
self.select_option(to_box, str(self.lisa.id))
self.selenium.find_element(By.ID, remove_link).click()
self.assertSelectOptions(
from_box, [str(self.arthur.id), str(self.lisa.id)]
)
self.assertSelectOptions(
to_box, [str(self.peter.id), str(self.jason.id)]
)

self.select_option(to_box, str(self.lisa.id))
self.selenium.find_element(By.ID, remove_link).click()
self.assertSelectOptions(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
self.assertSelectOptions(
from_box,
[
str(self.arthur.id),
str(self.bob.id),
str(self.cliff.id),
str(self.jenny.id),
str(self.john.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
self.assertSelectOptions(
from_box,
[
str(self.arthur.id),
str(self.bob.id),
str(self.cliff.id),
str(self.jenny.id),
str(self.john.id),
str(self.lisa.id),
],
)
self.assertSelectOptions(
to_box, [str(self.peter.id), str(self.jason.id)]
)

# -----------------------------------------------------------------
# Pressing enter on a filtered option sends it properly to
# the 'to' box.
self.select_option(to_box, str(self.jason.id))
self.selenium.find_element(By.ID, remove_link).click()
input.send_keys("ja")
self.assertSelectOptions(from_box, [str(self.jason.id)])
input.send_keys([Keys.ENTER])
self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)])
input.send_keys([Keys.BACK_SPACE, Keys.BACK_SPACE])
# Pressing enter on a filtered option sends it properly to
# the 'to' box.
self.select_option(to_box, str(self.jason.id))
self.selenium.find_element(By.ID, remove_link).click()
input.send_keys("ja")
self.assertSelectOptions(from_box, [str(self.jason.id)])
input.send_keys([Keys.ENTER])
self.assertSelectOptions(
to_box, [str(self.peter.id), str(self.jason.id)]
)
input.send_keys([Keys.BACK_SPACE, Keys.BACK_SPACE])

# Save and check that everything is properly stored in the database ---
with self.wait_page_loaded():
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
# Save, everything should be stored properly in the database.
with self.wait_page_loaded():
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.school = School.objects.get(id=self.school.id) # Reload from database
self.assertEqual(list(self.school.students.all()), [self.jason, self.peter])
self.assertEqual(list(self.school.alumni.all()), [self.jason, self.peter])
Expand Down

0 comments on commit f6629ee

Please sign in to comment.