Repository: django/django
File: tests/admin_views/tests.py (Python)

Code before:
        for index, fieldset in enumerate(fieldsets):
            legend = fieldset.find_element(By.TAG_NAME, "legend")
            self.assertEqual(legend.text, expected_legend_tags_text[index])

    @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
    def test_use_fieldset_fields_horizontal(self):
        self.admin_login(
            username="super", password="secret", login_url=reverse("admin:index")
        )
        self.selenium.get(
            self.live_server_url + reverse("admin:admin_views_mainprepopulated_add")
        )
        self.take_screenshot("horizontal_fieldset")

    @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
    @override_settings(MESSAGE_LEVEL=10)
    def test_messages(self):
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support.ui import Select

        with override_settings(MESSAGE_LEVEL=10):
            self.admin_login(
                username="super", password="secret", login_url=reverse("admin:index")
            )
            UserMessenger.objects.create()
            for level in ["warning", "info", "error", "success", "debug"]:
                self.selenium.get(
                    self.live_server_url
                    + reverse("admin:admin_views_usermessenger_changelist"),
                )
                checkbox = self.selenium.find_element(
                    By.CSS_SELECTOR, "tr input.action-select"
                )
                checkbox.click()
                Select(self.selenium.find_element(By.NAME, "action")).select_by_value(
                    f"message_{level}"
                )
                self.selenium.find_element(By.XPATH, '//button[text()="Run"]').click()
                message = self.selenium.find_element(
                    By.CSS_SELECTOR, "ul.messagelist li"
                )
                self.assertEqual(message.get_attribute("innerText"), f"Test {level}")
                self.take_screenshot(level)

    @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
    def test_list_editable_with_filter(self):
        from selenium.webdriver.common.by import By

        Person.objects.create(name="Tom", gender=1)
        self.admin_login(
            username="super", password="secret", login_url=reverse("admin:index")

Review comment (security):
I think we should verify that the multiline container contains fieldset tags and at least 2 fields before taking the screenshot :)
```diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py
index 8ac5571f9d..8290c56138 100644
--- a/tests/admin_views/tests.py
+++ b/tests/admin_views/tests.py
@@ -7081,12 +7081,23 @@ class SeleniumTests(AdminSeleniumTestCase):
 
     @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
     def test_use_fieldset_fields_horizontal(self):
+        from selenium.webdriver.common.by import By
+
         self.admin_login(
             username="super", password="secret", login_url=reverse("admin:index")
         )
         self.selenium.get(
             self.live_server_url + reverse("admin:admin_views_mainprepopulated_add")
         )
+        multiline = self.selenium.find_element(
+            By.CSS_SELECTOR, "#content-main .field-pubdate .form-multiline",
+        )
+        # Get direct children (div, fieldset) of multiline
+        divs = multiline.find_elements(By.CSS_SELECTOR, ":scope > div")
+        fieldsets = multiline.find_elements(By.CSS_SELECTOR, ":scope > fieldset")
+        fields = divs + fieldsets
+        self.assertGreater(len(fields), 1)
+        self.assertGreater(len(fieldsets), 0)
         self.take_screenshot("horizontal_fieldset")
 
     @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
```
I'm not sure if using `:scope` is the best approach here 🥲

Code after:
        for index, fieldset in enumerate(fieldsets):
            legend = fieldset.find_element(By.TAG_NAME, "legend")
            self.assertEqual(legend.text, expected_legend_tags_text[index])

    @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
    def test_use_fieldset_with_grouped_fields(self):
        from selenium.webdriver.common.by import By

        self.admin_login(
            username="super", password="secret", login_url=reverse("admin:index")
        )
        self.selenium.get(
            self.live_server_url + reverse("admin:admin_views_course_add")
        )
        multiline = self.selenium.find_element(
            By.CSS_SELECTOR, "#content-main .field-difficulty, .form-multiline"
        )
        # Two field boxes.
        field_boxes = multiline.find_elements(By.CSS_SELECTOR, "div > div.fieldBox")
        self.assertEqual(len(field_boxes), 2)
        # One of them is under a <fieldset>.
        under_fieldset = multiline.find_elements(
            By.CSS_SELECTOR, "fieldset > div > div.fieldBox"
        )
        self.assertEqual(len(under_fieldset), 1)
        self.take_screenshot("horizontal_fieldset")

    @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
    @override_settings(MESSAGE_LEVEL=10)
    def test_messages(self):
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support.ui import Select

        with override_settings(MESSAGE_LEVEL=10):
            self.admin_login(
                username="super", password="secret", login_url=reverse("admin:index")
            )
            UserMessenger.objects.create()
            for level in ["warning", "info", "error", "success", "debug"]:
                self.selenium.get(
                    self.live_server_url
                    + reverse("admin:admin_views_usermessenger_changelist"),
                )
                checkbox = self.selenium.find_element(
                    By.CSS_SELECTOR, "tr input.action-select"
                )
                checkbox.click()
                Select(self.selenium.find_element(By.NAME, "action")).select_by_value(
                    f"message_{level}"
                )
                self.selenium.find_element(By.XPATH, '//button[text()="Run"]').click()