laraveldockerlaravel-dusklaravel-sail

Laravel dusk can't disable headless mode on laravel sail with wsl2


i have a existing up to date laravel 11 project that's running under laravel sail with docker in windows 11 with wsl2 (ubuntu). i've installed and configured laravel dusk according to the official laravel docs the dusk test are running fine but i cant disable headless mode to open a browser window no matter what i try

The first think i tried was to add DUSK_HEADLESS_DISABLED=true in my .env.dusk.local did not work.

Next i commented out '--headless=new' in the driver method in DuskTestCase.php and did nothing

The same with '--disable-gpu'

Tried adding '--no-sandbox' suggested by chat GPT and guess what? it did nothing

Next i fended a discussion on laracast where someone suggested to change my selenium image in my docker-compose.yml to:

image: 'selenium/standalone-chrome:4.1.2-20220217'

that did not work either.

tried sail down sail up --build -d.

I'm stuck and any help with be greatly appreciated.


Solution

  • Run laravel-dusk headless false

    Before you can run your laravel dusk tests in a visable browser on laravel sail with wsl2 there are a few steps you need to do beforehand:

    Step 1:

    You need to edit your DuskTestCase.php like:

     protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            // '--disable-gpu', //disable or remove disable-gpu to run with an browser it needs gpu 
            // '--headless', //disable or remove headless to run with an browser
            '--no-sandbox',
            '--disable-dev-shm-usage' //this isnt necesery but can help you with future problems look "https://stackoverflow.com/a/75719112/20538588"
        ]);
    
        return RemoteWebDriver::create(
            'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
            )
        );
    }
    

    Step 2:

    Make sure you also unlock port 7900 and have a chrome standalone image defined. You can do this by configuring you docker-compose.yml like:

    selenium:
    image: 'selenium/standalone-chrome:4.1.2-20220217' // <---have a chrome image configured
    environment:
        LARAVEL_SAIL: 1
    volumes:
        - '/dev/shm:/dev/shm'
    ports:
        - '${FORWARD_SELENIUM_PORT:-4444}:4444'
        - '${FORWARD_SELENIUM_HEADFUL_PORT:-7900}:7900' // <---open port :7900
    networks:
        - sail
    

    You can then visit the url localhost:7900 the default credential is 'secret'

    Step 3:

    The last step is configuring you .env by adding: DUSK_HEADLESS_DISABLED=true