PlaywrightのNewwork mockingをつかってみる - @ledsun blog ではテスト用のUIを使ってリダイレクトの動作を確認しました。 テストコードで確認してみましょう。
import { test, expect } from '@playwright/test'; test.beforeEach(async ({ context }) => { await context.route(/redirect_to/, route => route.fulfill({ status: 301, headers: { location: 'https://playwright.dev/' } })); }); test('has title', async ({ page }) => { await page.goto('https://playwright.dev/'); const url = await page.evaluate(async () => { const response = await fetch('https://example.com/redirect_to') return response.url }) expect(url).toBe('https://playwright.dev/') });
page.evaluate
をつかって、ページ内でJavaScriptを実行します。
fetch('https://example.com/redirect_to')
を実行して、Response#urlがリダレクト先のURLになっているか確認します。
前回と比べて次の点が変わっています