vincent15000's avatar

Pest for end-to-end tests with the browser

Hello,

I have this test.

    $page
        ->click('@create-client')
        ->type('reference', 'KFUYSBF')
        ->type('lastname', 'DUPONT')
        ->type('firstname', 'Martin')
        ->type('address_line_1', '50, rue des Cordes')
        ->type('address_line_2', 'Appartement 22')
        ->type('postcode', '75001')
        ->type('city', 'Paris')
        ->type('phone', '0123456789')
        ->type('email', '[email protected]')
        ->type('comments', 'Rien à dire')
        ->press('Enregistrer')
        ->assertSee('KFUYSBF')
        ->assertSee('DUPONT')
        ->assertSee('Martin')
        ->assertSee('0123456789')
        ->assertSee('[email protected]')
        ->assertNoJavascriptErrors();

If I type all this directly in the UI, it's ok, it works perfect.

But the test fails.

I just start writing end-to-end tests and I discover that it's now possible with Pest 4.

For this form, I use the standard process :

  • I click on the + button to create a new client

  • I fill all fields

  • I click on the save button to store the new client

  • redirection to the clients page with the list of all clients

Why does my test fail ?

Any idea ?

Thanks for your help.

V

0 likes
1 reply
LaryAI's avatar
Level 58

Sure! Here is a detailed solution addressing your issue:


The Primary Cause

Based on the code and your description, you’re using browser interaction methods in a Pest E2E (end-to-end) test, likely via Laravel Dusk or Pest Browser. It's working in the UI manually, but not via the test suite. This usually points to one or more of the following:

  1. Selectors not matching (e.g., the test can't find the button or field)
  2. Timing issues (e.g., the DOM isn't fully loaded before a click/type)
  3. Database state (something is missing or unseeded during test)
  4. JavaScript or HTTP errors (your test environment differs from local)

How to Troubleshoot & Solution Steps

1. Assert Elements Exist Before Interacting

Ensure your selectors (like @create-client) exist and are visible before attempting actions:

$page
    ->waitFor('@create-client')
    ->click('@create-client')
    ->waitFor('input[name=reference]')
    ->type('reference', 'KFUYSBF')
    // ...repeat waitFor for each field if necessary
    ->type('lastname', 'DUPONT')
    ->type('firstname', 'Martin')
    ->type('address_line_1', '50, rue des Cordes')
    ->type('address_line_2', 'Appartement 22')
    ->type('postcode', '75001')
    ->type('city', 'Paris')
    ->type('phone', '0123456789')
    ->type('email', '[email protected]')
    ->type('comments', 'Rien à dire')
    ->press('Enregistrer')
    // wait until redirected/list displayed
    ->waitForText('KFUYSBF')
    ->assertSee('KFUYSBF')
    ->assertSee('DUPONT')
    ->assertSee('Martin')
    ->assertSee('0123456789')
    ->assertSee('[email protected]')
    ->assertNoJavascriptErrors();

Tip: Use waitFor and waitForText before typing or clicking anything to avoid interacting with elements that have not rendered yet.


2. Verify Dusk/Pest Selectors

If you use @create-client, make sure you have a dusk selector:

<button dusk="create-client">Create Client</button>

or

<input dusk="reference" ... />

If you're using CSS selectors, switch to something like:

->click('[data-testid="create-client"]')

3. Database and State

If your database isn’t set up, or if clients already exist and interfere, clean your DB before each test:

beforeEach(function () {
    // Adjust as needed
    \App\Models\Client::truncate();
});

4. Run with Debug

Run your test with Pest's debug (-v or --debug) to see more info:

php artisan dusk --debug

Check the last screenshot in tests/Browser/screenshots and the console.log for JS errors that might cause failed assertions.


5. JavaScript Issues

If asserting assertNoJavascriptErrors() fails, you might need to check your browser logs or consider removing that line temporarily to see if that's the only issue.


Example Improved Pest Dusk Test

it('can create a client from the browser', function ($page) {
    $page
        ->visit('/clients')
        ->waitFor('@create-client')
        ->click('@create-client')
        ->waitFor('input[name=reference]')
        ->type('reference', 'KFUYSBF')
        ->type('lastname', 'DUPONT')
        ->type('firstname', 'Martin')
        ->type('address_line_1', '50, rue des Cordes')
        ->type('address_line_2', 'Appartement 22')
        ->type('postcode', '75001')
        ->type('city', 'Paris')
        ->type('phone', '0123456789')
        ->type('email', '[email protected]')
        ->type('comments', 'Rien à dire')
        ->press('Enregistrer')
        ->waitForText('KFUYSBF') // Wait for page update
        ->assertSee('KFUYSBF')
        ->assertSee('DUPONT')
        ->assertSee('Martin')
        ->assertSee('0123456789')
        ->assertSee('[email protected]')
        ->assertNoJavascriptErrors();
});

Summary of Steps To Fix:

  1. Make sure selectors are correct and use waitFor() before interacting.
  2. Clear and set up DB state.
  3. Run with debug flags and check browser screenshots.
  4. Make sure dusk selectors or data-testid are present.
  5. Remove/assert JavaScript errors as needed.

With these adjustments, your browser test should become both stable and reliable. If you need further help, paste the exact error message you’re seeing!

Please or to participate in this conversation.