スキル一覧に戻る
moonpixels

writing-browser-tests

by moonpixels

Laravel skeleton application template with Inertia React.

1🍴 0📅 2026年1月10日
GitHubで見るManusで実行

SKILL.md


name: writing-browser-tests description: Write browser tests for end-to-end user workflows and JavaScript interactions using Pest v4 with Playwright. Use MINIMALLY - only for core areas, happy paths, and functionality that CANNOT be tested with feature tests. Browser tests replace QA smoke testing, not feature tests.

Write Browser Tests

Browser tests should be used minimally as automated QA smoke tests. They are slow, complex, and must never duplicate feature test coverage. Use them only for core critical workflows and functionality that requires a real browser (JavaScript interactions, WebSocket updates, drag-and-drop).

Testing Hierarchy

  1. Feature Tests (PRIMARY) - Default for all business logic and HTTP endpoints
  2. Unit Tests (SUPPLEMENTARY) - For complex, concentrated areas
  3. Browser Tests (MINIMAL) - Only for core areas and JS interactions (you're here)

File Structure

tests/Browser/{FeatureName}Test.php

Examples: LoginTest.php, RegistrationTest.php, CheckoutTest.php

Core Conventions

Test Structure

<?php

declare(strict_types=1);

use App\Models\User;

test('users can complete the workflow', function (): void {
    $user = User::factory()->create();

    visit(route('feature.index'))
        ->fill('field', 'value')
        ->press('Submit')
        ->assertSee('Success message')
        ->assertRoute('dashboard.index');
});

Browser Testing Helpers

Navigation:

visit(route('login'))
click('Dashboard')
back()

Form Interactions:

fill('email', 'test@example.com')
select('country', 'US')
check('terms')
uncheck('newsletter')
press('Submit')

Assertions:

assertSee('Welcome')
assertDontSee('Error')
assertRoute('dashboard.index')
assertPresent('#element-id')
assertMissing('.error-message')
assertChecked('terms')

Waiting:

waitForText('Loading complete')
waitFor('.modal')
waitForRoute('dashboard.index')

Authentication Helper

function loginAs(User $user): void
{
    visit(route('login'))
        ->fill('email', $user->email)
        ->fill('password', 'password')
        ->press('Log in')
        ->assertRoute('dashboard.index');
}

Anti-Patterns

Don't Do This

// Don't test validation with browser tests (use feature tests)
test('form validation works', function (): void {
    visit(route('register'))
        ->press('Submit')
        ->assertSee('The email field is required');
});

// Don't test simple form submissions (use feature tests)
test('users can update profile', function (): void {
    loginAs($user);
    visit(route('profile.edit'))
        ->fill('name', 'New Name')
        ->press('Save')
        ->assertSee('Profile updated');
});

Do This Instead

// Feature test - Fast and comprehensive
test('registration requires email', function (): void {
    $this->post(route('register'), [])
        ->assertInvalid(['email']);
});

// Browser test - Only for what feature tests can't test
test('profile image preview updates when file selected', function (): void {
    $user = User::factory()->create();
    loginAs($user);

    visit(route('profile.edit'))
        ->attach('avatar', storage_path('testing/avatar.jpg'))
        ->waitFor('.image-preview') // JavaScript-driven preview
        ->assertPresent('.image-preview img');
});

The Golden Rule

Before writing a browser test, ask: "Can I test this with a feature test?"

If yes, write a feature test instead. Browser tests are for:

  • Core workflow smoke tests (happy paths only)
  • Functionality that literally cannot be tested without a real browser

Running Tests

# Run all browser tests
php artisan test --testsuite=Browser

# Run in headed mode (see browser)
HEADED=1 php artisan test --testsuite=Browser

Debugging

screenshot('debug-screen.png')
pause(5000)
dump(script('return document.body.innerHTML'))

Quality Standards

  • All browser tests must pass PHPStan level 8
  • Tests must be independent and idempotent
  • MINIMAL BROWSER TESTS - Far fewer than feature tests
  • NO DUPLICATION - Never test what feature tests already cover
  • Keep tests focused on ONE happy path workflow

References

スコア

総合スコア

50/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です