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

Code before:
        """
        response = self.client.get(
            reverse("admin7:admin_views_prepopulatedpost_change", args=(self.p1.pk,))
        )
        self.assertContains(response, 'data-prepopulated-fields="[]"')
        self.assertContains(response, '<div class="readonly">%s</div>' % self.p1.slug)


def _clean_sidebar_state(driver):
    driver.execute_script("localStorage.removeItem('django.admin.navSidebarIsOpen')")


@override_settings(ROOT_URLCONF="admin_views.urls")
class SeleniumTests(AdminSeleniumTestCase):
    available_apps = ["admin_views"] + AdminSeleniumTestCase.available_apps

    def setUp(self):
        from selenium.webdriver.common.keys import Keys

        self.superuser = User.objects.create_superuser(
            username="super", password="secret", email="super@example.com"
        )
        self.p1 = PrePopulatedPost.objects.create(
            title="A Long Title", published=True, slug="a-long-title"
        )
        self.modifier_key = Keys.COMMAND if sys.platform == "darwin" else Keys.CONTROL

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

        self.selenium.get(self.live_server_url + reverse("admin:login"))
        button = self.selenium.find_element(By.CSS_SELECTOR, ".submit-row input")
        offset_left = button.get_property("offsetLeft")
        offset_right = button.get_property("offsetParent").get_property(
            "offsetWidth"
        ) - (offset_left + button.get_property("offsetWidth"))
        # Use assertAlmostEqual to avoid pixel rounding errors.
        self.assertAlmostEqual(offset_left, offset_right, delta=3)
        self.take_screenshot("login")

    def test_prepopulated_fields(self):
        """
        The JavaScript-automated prepopulated fields work with the main form
        and with stacked and tabular inlines.
        Refs #13068, #9264, #9983, #9784.
        """
        from selenium.webdriver import ActionChains
        from selenium.webdriver.common.by import By

        self.admin_login(

Review comment (refactor):
Despite the historical usage of `setUp` for object creation, I usually recommend using helpers to get/create interesting values used by tests. The reason is that everything that is attached to `self` in `setUp`, is "alive" for the whole test suite run (not the test case, but the whooooole test suite run). So I would suggest:

```suggestion


    @property
    def modifier_key(self):
        return Keys.COMMAND if sys.platform == "darwin" else Keys.CONTROL
```

Code after:
        """
        response = self.client.get(
            reverse("admin7:admin_views_prepopulatedpost_change", args=(self.p1.pk,))
        )
        self.assertContains(response, 'data-prepopulated-fields="[]"')
        self.assertContains(response, '<div class="readonly">%s</div>' % self.p1.slug)


def _clean_sidebar_state(driver):
    driver.execute_script("localStorage.removeItem('django.admin.navSidebarIsOpen')")


@override_settings(ROOT_URLCONF="admin_views.urls")
class SeleniumTests(AdminSeleniumTestCase):
    available_apps = ["admin_views"] + AdminSeleniumTestCase.available_apps

    def setUp(self):
        self.superuser = User.objects.create_superuser(
            username="super", password="secret", email="super@example.com"
        )
        self.p1 = PrePopulatedPost.objects.create(
            title="A Long Title", published=True, slug="a-long-title"
        )

    @property
    def modifier_key(self):
        from selenium.webdriver.common.keys import Keys

        return Keys.COMMAND if sys.platform == "darwin" else Keys.CONTROL

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

        self.selenium.get(self.live_server_url + reverse("admin:login"))
        button = self.selenium.find_element(By.CSS_SELECTOR, ".submit-row input")
        offset_left = button.get_property("offsetLeft")
        offset_right = button.get_property("offsetParent").get_property(
            "offsetWidth"
        ) - (offset_left + button.get_property("offsetWidth"))
        # Use assertAlmostEqual to avoid pixel rounding errors.
        self.assertAlmostEqual(offset_left, offset_right, delta=3)
        self.take_screenshot("login")

    def test_prepopulated_fields(self):
        """
        The JavaScript-automated prepopulated fields work with the main form
        and with stacked and tabular inlines.
        Refs #13068, #9264, #9983, #9784.
        """
        from selenium.webdriver import ActionChains