samedi 14 mars 2020

Testing multiple viewport sizes with Django/Selenium

I'm trying to test characteristics of certain UI elements given different viewport sizes and media defined in CSS. I have a setup function to instantiate a headless Chrome browser with I believe a default viewport size of 800x600:

class NewDesktopVisitorTest(StaticLiveServerTestCase):

    def setUp(self):
        self.chrome_options = Options()
        self.chrome_options.add_argument('--headless')
        self.browser = webdriver.Chrome(options=self.chrome_options)

This works well for testing what I would consider the desktop version of a page, but I'd also like to make sure the mobile version renders as I'd expect. I can create a completely separate class with a different setup and specify the viewport size like so:

class NewMobileVisitorTest(StaticLiveServerTestCase):

    def setUp(self):
        self.chrome_options = Options()
        self.chrome_options.add_argument('--headless')
        self.chrome_options.add_argument('--window-size=375,800')
        self.browser = webdriver.Chrome(options=self.chrome_options)

This has the benefit of very cleanly showing where a given test is failing (i.e. desktop or mobile). The trouble is that I want the exact same tests to run against both (and potentially multiple) viewport sizes, and I don't want to have to maintain the exact same tests in multiple classes.

Most of my searches for parameterized testing yield solutions for running the same tests against different data sets, but I've yet to find examples of solutions for setting up and running the same tests against multiple starting configurations.

I'm hesitant (although not unwilling) to switch to a different testing framework or install additional packages only to solve for this specific case.

Has anyone found a straightforward solution to this problem?

Aucun commentaire:

Enregistrer un commentaire