playwrightplaywright-test

Playwright: Can I get the device being used so I can follow a different route in a test?


I've got my suite to run against two different browsers by setting my config file to use a desktop and mobile device:

  {
      name: 'firefox - Desktop',
      use: {
        ...devices['Desktop Firefox'],
      },
    },

    {
      name: 'Mobile Safari - Portrait',
      use: {
        ...devices['iPhone 12'],
      },
    },

Is it possible to get which device is being used in a test? The site being tested is responsive so things like images might be visible on desktop but not on mobile so I need a way to put a condition in my test to assert it's visible if desktop but not if mobile.


Solution

  • You can use isMobile. e.g.

    test('MyTest', async ({ page, isMobile }) => {
      // isMobile returns a boolean depending if a mobile browser / device is used.
    });
    

    This can be called in a test and returns a boolean.