0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Playwrightのエミュレーター機能について

Posted at

playwright.config.ts の記述について

playwright.config.tsにて以下の様なprojectを定義します。

    {
      name: 'mobile',
      testMatch: 'testMobile.spec.ts',
      use: { ...devices['iPhone 13 Pro'] },
    },

上記はテストでiPhone 13 Proエミュレーターを使用する場合の記述です。

テストの動作

例えば、以下の様なテストファイル”testMobile.spec.ts”があったとします。

import { test } from '@playwright/test';

test('input fields', async ({ page }, testInfo) => {
  await page.goto('/');
  await page.locator('.sidebar-toggle').click();
  await page.getByText('Forms').click();
  await page.getByText('Form Layouts').click();
  await page.locator('.sidebar-toggle').click();
  const usingTheGridEmailInput = page.locator('nb-card', { hasText: 'Using the Grid' }).getByRole('textbox', { name: 'Email' });
  await usingTheGridEmailInput.fill('test@test.com');
  await usingTheGridEmailInput.clear();
  await usingTheGridEmailInput.fill('test@test.com');
});

これを以下のコマンドで実行します。

npx playwright test tests/testMobile.spec.ts --project=mobile

すると、以下のような画面でテストすることができます。

名称未設定.jpg

testInfoによる分岐

以下の様に、testInfoを使用することで、使用するエミュレーターによるif文も使うことができます。

import { test } from '@playwright/test';

test('input fields', async ({ page }, testInfo) => {
  await page.goto('/');
  if (testInfo.project.name === 'mobile') {
    await page.locator('.sidebar-toggle').click();
  }
  await page.getByText('Forms').click();
  await page.getByText('Form Layouts').click();
  if (testInfo.project.name === 'mobile') {
    await page.locator('.sidebar-toggle').click();
  }
  const usingTheGridEmailInput = page.locator('nb-card', { hasText: 'Using the Grid' }).getByRole('textbox', { name: 'Email' });
  await usingTheGridEmailInput.fill('test@test.com');
  await usingTheGridEmailInput.clear();
  await usingTheGridEmailInput.fill('test@test.com');
});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?