Search code examples
playwright

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.

Update: For anyone else who asks this, you can use isMobile https://playwright.dev/docs/api/class-browser#browser-new-context-option-is-mobile

e.g.

test('MyTest', async ({ page, isMobile }) => {

// isMobile returns a boolean depending if a mobile browser / device is used. }


Solution

  • https://playwright.dev/docs/api/class-browser#browser-new-context-option-is-mobile

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