Search code examples
phplaravellaravel-dusklaravel-dusk2

Laravel Dusk says Class 'Tests\Browser\Components\...' could not be found


After I installed laravel-dusk following the official Laravel documentation and run this command:

php artisan make:component CardComponentTest

Then try to run immediately:

php artisan dusk tests/Browser/Components/CardComponentTest.php

I get this error:

Class 'Tests\Browser\Components\CardComponentTest' could not be found in '/var/www/html/tests/Browser/Components/CardComponentTest.php'.

I tested file and path are correct:

ls -l /var/www/html/tests/Browser/Components/CardComponentTest.php

And it says:

-rw-r--r-- 1 djw djw 6917 Dec  3 11:25 /var/www/html/tests/Browser/Components/CardComponentTest.php

So it is exists and readable.

I checked the namespace in the file:

<?php

namespace Tests\Browser\Components;

It also looks good.

I checked the composer.json and in this I have this section:

    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },

So the file exists, the namespace is good and the namespace is picked up in the composer.json.

I tried to run composer dump-autoload too. All good.

Any ide what's wrong whit this?


Solution

  • the error:

    Class 'Tests\Browser\Components\CardComponentTest' could not be found in '/var/www/html/tests/Browser/Components/CardComponentTest.php'.
    

    indicate you don't have CardComponentTest test case. Because CardComponentTest is not test case but just an component/part of test.

    As aless said, component is not a test itself. But if you really want to test CardComponentTest you can replace

    use Laravel\Dusk\Component as BaseComponent;
    
    class CardComponentTest extends BaseComponent
    

    to :

    use Tests\DuskTestCase;
     
    class CardComponentTest extends DuskTestCase
    

    after you do that you can run php artisan dusk tests/Browser/Components/CardComponentTest.php , You just need to add test method first like. An example:

    public function testComponentExample()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/')
                    ->assertSee('Laravel');
        });
    }
    

    But create test case in component folder is not best practice. You should create test case like official documentation said. Run dusk:make instead of dusk:component. An example:

    php artisan dusk:make SimpleBrowserTest
    

    and now you can run

    php artisan dusk tests/Browser/SimpleBrowserTest.php