Skip to content

Conversation

@parth391
Copy link

@parth391 parth391 commented Sep 5, 2025

Hello

This PR introduces support for object pages in visit().

It allows moving logic out of individual test cases, improving reusability and maintainability. Object pages can define shorthand selectors, local and global elements, and also configure timezone, locale, device and browser type settings.

Example

use Pest\Browser\Api\Webpage;
use Pest\Browser\Page;
use Pest\Browser\Enums\Device;
use Pest\Browser\Enums\BrowserType;

it('may register user', function (): void {
    Route::get('/', fn (): string => 'Home page');
    Route::get('/register', fn (): string => '
        <html>
            <head></head>
            <body>
                <form>
                    <input name="fullname" type="text" />
                    <input name="user_email" type="text" />
                    <input name="password" type="password" />
                    <Button id="sumbit-button" type="submit">Submit</Button>
                </form>
            </body>
        </html>
    ');

    $page = visit(new HomePage);

    $page->assertSee('Home page');

    $locale = $page->script('navigator.language');
    expect($locale)->toBe('fr-FR');

    $timezone = $page->script('Intl.DateTimeFormat().resolvedOptions().timeZone');
    expect($timezone)->toBe('Europe/Paris');

    $page->navigate(new RegisterPage);
    $page->register();
});

it('may visit multiple object pages', function (): void {
    Route::get('/', fn (): string => '');
    Route::get('/register', fn (): string => '');

    $response = visit([new HomePage, new RegisterPage]);

    $response->assertNoSmoke();
});

final class HomePage extends Page
{
    public function url(): string
    {
        return '/';
    }

    public function timezone(): string
    {
        return 'Europe/Paris';
    }

    public function locale(): string
    {
        return 'fr-FR';
    }

    public function device(): Device
    {
        return Device::MACBOOK_16;
    }

    public function browserType(): BrowserType
    {
        return BrowserType::SAFARI;
    }
}

final class RegisterPage extends Page
{
    public function url(): string
    {
        return '/register';
    }

    public function elements(): array
    {
        return [
            '@name' => 'fullname',
            '@email' => 'user_email',
            '@button' => 'sumbit-button',
        ];
    }

    public function register(Webpage $page): self
    {
        $page->type('@name', fake()->name())
            ->type('@email', fake()->unique()->safeEmail())
            ->click('@button');

        return $this;
    }
}

Why this is needed

  1. Simplifies migration from Laravel\Dusk\Browser.
  2. Encourages code splitting and reuse.

Breaking changes

None.

Todo:

  1. Accept Pest\Browser\Page in visit().
  2. Stub for base page, to simplify code.

If @nunomaduro is willing to accept, i will be happy to drop PR.

Thankyou.

@jasonmccreary
Copy link
Collaborator

@parth391, this is nice. I agree about encapsulating code as well as unifying with Dusk features.

Can you resolve the conflicts and I'll get this back in the queue.

@parth391
Copy link
Author

parth391 commented Sep 5, 2025

@jasonmccreary, conflict has been resolved.

@jasonmccreary
Copy link
Collaborator

@parth391, unfortunately the tests are failing.

@parth391
Copy link
Author

parth391 commented Sep 5, 2025

@jasonmccreary, test will fail, as right now visit(), only accept string.

@parth391
Copy link
Author

parth391 commented Sep 5, 2025

I can drop a pull request on https://github.com/pestphp/pest

@jasonmccreary
Copy link
Collaborator

@parth391, ah, I see. I'll defer this to @nunomaduro for proper handling then. Probably best to go ahead and open that PR too though and reference this one.

@parth391
Copy link
Author

Hello @jasonmccreary, is the Pest team currently exploring the possibility of including a Page feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants