Dusk: Access Dusk outside of CLI

Created on 7 Sep 2017  路  11Comments  路  Source: laravel/dusk

I would like to run dusk outside of the CLI. Basically I'm trying to run my tests via a route or controller
like this

Route::get('runtests', function()
{
echo '
init Dusk...';
Artisan::call('dusk');
echo 'done dusk';
});

Is there a way to access dusk through the artisan facade?

Most helpful comment

You can create your own Chrome browser like this @poxin13:

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome\ChromeProcess;

$process = (new ChromeProcess)->toProcess();
$process->start();
$options = (new ChromeOptions)->addArguments(['--disable-gpu', '--headless']);
$capabilities = DesiredCapabilities::chrome()->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = retry(5, function () use($capabilities) {
    return RemoteWebDriver::create('http://localhost:9515', $capabilities);
}, 50);
$browser = new Browser($driver);
$browser->visit('https://www.google.com');
$browser->quit();
$process->stop();

If you are using Windows, replace $process->start(); with this:

$process->start(null, [
    'SystemRoot' => 'C:\\WINDOWS',
    'TEMP' => 'C:\Users\<User>\AppData\Local\Temp',
]);

All 11 comments

I found that I needed to remove this if statement from DuskServiceProvider.php
"if ($this->app->runningInConsole()) {"

When I run this code now

$_SERVER['argv'] = [];
echo '
init Dusk...';
Artisan::call('dusk');
echo 'done dusk';

I get this error message

Warning: TTY mode is not supported on Windows platform.\r\n
Could not open input file: vendor/phpunit/phpunit/phpunit\n

The warning is normal. I get that even when I run via CLI. The second one in regards to phpunit is what i'm trying to figure out now.

I too would like to be able to cleanly call Dusk via a controller for aspects outside of pure testing. Could be used as a neat automation tool in certain scenarios. Example if the endpoint doesn't have an API.

maybe someone managed to run dusk from controller?

You can create your own Chrome browser like this @poxin13:

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome\ChromeProcess;

$process = (new ChromeProcess)->toProcess();
$process->start();
$options = (new ChromeOptions)->addArguments(['--disable-gpu', '--headless']);
$capabilities = DesiredCapabilities::chrome()->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = retry(5, function () use($capabilities) {
    return RemoteWebDriver::create('http://localhost:9515', $capabilities);
}, 50);
$browser = new Browser($driver);
$browser->visit('https://www.google.com');
$browser->quit();
$process->stop();

If you are using Windows, replace $process->start(); with this:

$process->start(null, [
    'SystemRoot' => 'C:\\WINDOWS',
    'TEMP' => 'C:\Users\<User>\AppData\Local\Temp',
]);

I used this code and prefixed it with the required namespaces like so

`
$process = (new \Laravel\Dusk\Chrome\ChromeProcess)->toProcess();
$process->start();
$options = (new \Facebook\WebDriver\Chrome\ChromeOptions)->addArguments(['--disable-gpu', '--headless']);
$capabilities = \Facebook\WebDriver\Remote\DesiredCapabilities::chrome()->setCapability(\Facebook\WebDriver\Chrome\ChromeOptions::CAPABILITY, $options);
$driver = retry(5, function () use($capabilities) {
return \Facebook\WebDriver\Remote\RemoteWebDriver::create('http://localhost:9515', $capabilities);}, 50);

$browser = new Browser($driver);
$browser->visit('https://www.google.com');
$browser->quit();
$process->stop();
`

Unfortunately it seems like it's not starting the browser process correctly whereas it does perfectly when using Dusk with phpunit as in the official doc.

Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"","args":["--disable-gpu","--headless"]}}} Failed to connect to localhost port 9515: Connection refused

I need to run this and have control on the process, make it visit pages, dump the HTML, etc... from a controller.

Has anyone managed to get through this ?

After a few research, it seems that when running as dusk tests, the chromedriver runs nicely and I guess it's instanciated on a random port as I see

DevTools listening on ws://127.0.0.1:12723/devtools/browser/b2dc83c4-5bfa-4df5-9db9-0bf7bc4befb2

Here it's 12723 but it's always a different port.

It seems that the port is defined at launch and the fact that I hardcode http://localhost:9515 seems to be what's causing the issue.

Any help on this?

Thanks in advance.

Maybe the port is already being used. Did you try a different one?

I tried several others and everytime it says

Failed to connect to localhost port XXXX: Connection refused

I also tried with ports > 10000 just to be sure, several times with the same error again and again.

This is working great, thanks @staudenmeir

Closing this issue because it's already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue.

@staudenmeir thanks! Works perfectly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

susanBuck picture susanBuck  路  6Comments

marcusmoore picture marcusmoore  路  3Comments

alex-LE picture alex-LE  路  7Comments

muswanto picture muswanto  路  3Comments

dellow picture dellow  路  4Comments