Search code examples
phplaraveltestinglaravel-dusk

Is there a way to have assertPathIs() ignore part of the url?


While making a Laravel Dusk test function, I am trying to check the path in my application. However the URL in my application changes, depending on the user or what they are looking at (e.g. /Test/123/testing: in this case the 123 could be any combination of numbers.)

Is it possible to ignore the middle portion?

$this->browse(function (Browser $browser) {
    $browser->loginAs('email@email.com')
            ->visit('/test')
            ->assertPathIs('/test')
            ->assertSee('LinkText')
            ->clickLink('LinkText')
            ->assertPathIs('/test/(CanBeAnything)/testing');
});

Solution

  • Taking a look at the code we can see a regular expression match is actually being performed, after the text is run through preg_quote():

    $pattern = str_replace('\*', '.*', preg_quote($path, '/'));
    

    This means that you can do a wildcard search by using *, so this should work:

    $this->browse(function (Browser $browser) {
        $browser->loginAs('email@email.com')
                ->visit('/test')
                ->assertPathIs('/test')
                ->assertSee('LinkText')
                ->clickLink('LinkText')
                ->assertPathIs('/test/*/testing');
    });