メインコンテンツへスキップ

TestStepInfo

TestStepInfoは、現在実行中のテストステップに関する情報を含んでいます。これはステップ関数への引数として渡されます。TestStepInfoは、テストステップの実行を制御するユーティリティを提供します。

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

test('basic test', async ({ page, browserName }) => {
await test.step('check some behavior', async step => {
step.skip(browserName === 'webkit', 'The feature is not available in WebKit');
// ... rest of the step code
});
});

メソッド

attach

追加されたバージョン: v1.51 testStepInfo.attach

現在のテストステップに値またはファイルをディスクから添付します。一部のレポーターはテストステップの添付ファイルを表示します。pathまたはbodyのいずれかを指定する必要があり、両方は指定できません。このメソッドを呼び出すと、すべての添付ファイルをテストレベルで保存するtestInfo.attach()とは対照的に、添付ファイルがそのステップに帰属します。

例えば、スクリーンショットをテストステップに添付できます

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

test('basic test', async ({ page }) => {
await page.goto('https://playwright.dokyumento.jp');
await test.step('check page rendering', async step => {
const screenshot = await page.screenshot();
await step.attach('screenshot', { body: screenshot, contentType: 'image/png' });
});
});

または、APIが返すファイルを添付することもできます

import { test, expect } from '@playwright/test';
import { download } from './my-custom-helpers';

test('basic test', async ({}) => {
await test.step('check download behavior', async step => {
const tmpPath = await download('a');
await step.attach('downloaded', { path: tmpPath });
});
});
注意

testStepInfo.attach()は、添付されたファイルをレポーターがアクセスできる場所に自動的にコピーします。attach呼び出しが完了した後に、添付ファイルを安全に削除できます。

使用法

await testStepInfo.attach(name);
await testStepInfo.attach(name, options);

引数

  • name string#

    添付ファイル名。この名前はサニタイズされ、ディスクに保存する際のファイル名のプレフィックスとしても使用されます。

  • options Object (任意)

    • body string | Buffer (任意)#

      添付ファイル本体。pathとは相互に排他的です。

    • contentType string (任意)#

      レポートで適切に表示するための、この添付ファイルのコンテンツタイプ(例: 'application/json' または 'image/png')。省略された場合、コンテンツタイプはpathに基づいて推測されるか、string添付ファイルの場合はtext/plainに、Buffer添付ファイルの場合はapplication/octet-streamにデフォルトで設定されます。

    • path string (任意)#

      添付ファイルへのファイルシステム上のパス。bodyとは相互に排他的です。

戻り値


skip()

追加されたバージョン: v1.51 testStepInfo.skip()

現在実行中のステップを中断し、スキップ済みとしてマークします。現在失敗しており、短期的な修正が予定されているステップに役立ちます。

使用法

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

test('my test', async ({ page }) => {
await test.step('check expectations', async step => {
step.skip();
// step body below will not run
// ...
});
});

skip(condition)

追加されたバージョン: v1.51 testStepInfo.skip(condition)

条件付きで現在実行中のステップを中断し、オプションの説明を付けてスキップ済みとしてマークします。特定の場合に実行すべきでないステップに役立ちます。

使用法

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

test('my test', async ({ page, isMobile }) => {
await test.step('check desktop expectations', async step => {
step.skip(isMobile, 'not present in the mobile layout');
// step body below will not run
// ...
});
});

引数

  • condition boolean#

    スキップ条件。条件がtrueの場合、テストステップはスキップされます。

  • description string (任意)#

    テストレポートに表示されるオプションの説明。