I have a spec file that has a test:
test.describe('mobile', () => {
test.beforeEach(async ({ page }) => {
await page.goto(url);
});
test('Mobile check - (Optimal on desktop / tablet) only shown on smaller screens (i.e mobile breakpoint)', async ({ page })=>{
await page.goto(url);
await expect(page.getByText('(Optimal on desktop / tablet)')).toBeVisible();
});
});
I have in the playwright config setup 2 mobile browsers to test iPhone and Android emulation:
/* Test against mobile viewports. */
{
name: 'Mobile Chrome',
grep: /mobile/,
use: { ...devices['Pixel 5'] },
},
{
name: 'Mobile Safari',
grep: /mobile/,
use: { ...devices['iPhone 14 Pro'] },
},
When I run this, it runs the tests in the desktop browser, not the mobile window. I've seen examples of setting the viewport on a per test basis, but not the emulator itself.
I'd like to be able to have 1 specs file describing both mobile and desktop emulation, rather than separate by device type.
Can I set the target emulator on per test basis?
I don't want to have to run with filters; rather I run the tests, they execute individually against a targeted browser/s.
Edit
test.spec.ts
import { test, expect, devices } from '@playwright/test';
test.use({ storageState: 'playwright/.auth.json' });
const url = 'https://test.com/custom-library';
test.describe('desktop', () => {
test('Hide No Data (shown twice) on radar whilst awaiting an input', async ({ page, isMobile }) => {
test.skip(isMobile)
await page.goto(url);
const buttonsCount = await page.getByText('No Data').count();
expect(buttonsCount).toBe(0);
});
test('Desktop check - (Optimal on desktop / tablet) only shown on smaller screens (i.e mobile breakpoint)', async ({ page, isMobile }) => {
test.skip(isMobile)
await page.goto(url);
const buttonsCount = await page.getByText('(Optimal on desktop / tablet)').count();
expect(buttonsCount).toBe(0);
});
});
test.describe('mobile', () => {
test('Mobile check - (Optimal on desktop / tablet) only shown on smaller screens (i.e mobile breakpoint)', async ({ page, isMobile })=>{
console.log(!isMobile);
test.skip(!isMobile)
await page.goto(url);
await expect(page.getByText('(Optimal on desktop / tablet)')).toBeVisible();
});
});
My playwright.config.ts file is:
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './tests',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: 'html',
use: {
trace: 'on-first-retry',
},
projects: [
{
name: 'setup',
testMatch: /.*\.setup\.ts/
},
{
name: 'chromium',
use: { ...devices['Desktop Chrome'], storageState: './playwright/.auth.json' },
dependencies: ['setup'],
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'], storageState: './playwright/.auth.json' },
dependencies: ['setup'],
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'], storageState: './playwright/.auth.json' },
dependencies: ['setup'],
},
/* Test against mobile viewports. */
{
name: 'Mobile Chrome',
use: { ...devices['Pixel 5'], storageState: './playwright/.auth.json' },
dependencies: ['setup'],
},
{
name: 'Mobile Safari',
use: { ...devices['iPhone 14 Pro'], storageState: './playwright/.auth.json' },
dependencies: ['setup'],
},
],
});