Laravel dusk chrome driver timeout - laravel

Can anyone help, I am unable to get Laravel dusk to run the default sample test in my current Laravel 5.6 project on mac high sierra.
Error message
Time: 2.5 minutes, Memory: 14.00MB
There was 1 error:
1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"/Users/keith/Desktop/dusk/vendor/laravel/dusk/bin/chromedriver-mac","args":["--disable-gpu"]}}}
Operation timed out after 30002 milliseconds with 0 bytes received
/Users/keith/Desktop/dusk/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:286
/Users/keith/Desktop/dusk/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:126
/Users/keith/Desktop/dusk/tests/DuskTestCase.php:40
/Users/keith/Desktop/dusk/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:189
/Users/keith/Desktop/dusk/vendor/laravel/framework/src/Illuminate/Support/helpers.php:770
/Users/keith/Desktop/dusk/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:190
/Users/keith/Desktop/dusk/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:92
/Users/keith/Desktop/dusk/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:64
/Users/keith/Desktop/dusk/tests/Browser/ExampleTest.php:21
I have already done the following :
added the following to app\Providers\AppServiceProvider.php
use Laravel\Dusk\DuskServiceProvider;
...
public function register()
{
if ($this->app->environment('local', 'testing')) {
$this->app->register(DuskServiceProvider::class);
}
}
ran 'php artisan dusk:install' in terminal
set App_URL in .env to http://localhost:8000
specified the location of chromedriver in DuskTestCase
start 'php artisan serve' before running 'php artisan dusk'
Repository : https://github.com/KKOA/dusk

If your function extends from DuskTestCase.php, then you need to increase connection_timeout_in_ms.
Do this by changing driver method to the following:
DuskTestCase.php
protected function driver()
{
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
'--window-size=1920,1080',
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
), 90000, 90000
);
}
If that dosen't work for some reason then try to set_time_limit before $this->browse
set_time_limit(0);

try this while you instantiate your browser.. the most important is
$driver = retry(5, function () use ($capabilities) {
return RemoteWebDriver::create('http://localhost:9515', $capabilities, 60000, 60000);
Below is how i instantiate my browser
$options = (new ChromeOptions)->addArguments(['--disable-gpu', '--headless', '--no-sandbox']);
$capabilities = DesiredCapabilities::chrome()
->setCapability(ChromeOptions::CAPABILITY, $options)
->setPlatform('Linux');
$driver = retry(5, function () use ($capabilities) {
return RemoteWebDriver::create('http://localhost:9515', $capabilities, 60000, 60000);
}, 50);
$browser = new Browser($this->driver, new ElementResolver($driver, ''));

You really need to have latest google-chrome and latest chrome driver installed. I used to have only chromium browser and it doesn't work with it. To install latest chrome driver run php artisan dusk:chrome-driver.

Related

I'm trying to use guzzlehttp/guzzle in Magento 2.2.9 but it's not working

It's throwing me an error
PHP Error: Cannot instantiate interface GuzzleHttp\\ClientInterface in vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 111, referer: http://127.0.0.1/local_m2/admin/admin/system_config/edit/section/active_campaign/key/6a9ce672c9414c57acd889eb50fb82020e13e651b74cf3a81b9cd8b30da45306/ here
I have already run all Magento required commands Like Setup: upgrade, di:compile and deploy but still it's throwing me this error.
I have already checked GuzzleHttp in the vendor folder, it's already installed in Magento 2.2.9
I have tried the composer require guzzlehttp/guzzle:^6.0 to reinstall the library but having no luck.
import this library:
use GuzzleHttp\Client;
use GuzzleHttp\ClientFactory;
use GuzzleHttp\Exception\RequestException;
on __construct use:
$this->client = new Client([
"base_uri" => url_base,
"timeout" => 2,
]);
and then call:
$response = $this->client->get(
url_base . "/" . url_api_point,
['headers' =>
[
'Authorization' => "Bearer {$this->token}" /* if have */
]
]
);
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
try this way to create an instance of GuzzleClient, im currently using a similar in a magento 2.4.4 and works fine, you donĀ“t have to inyect that on __construct()
/**
* #return \GuzzleHttp\Client
*/
public function getClient()
{
$client = new \GuzzleHttp\Client(["base_uri" => "your url", "verify" => false]);
return $client;
}

Laravel Dusk Test Multi Threading

I have a series of Laravel Dusk tests as shown below ,,
when I run php laravel dusk
each of them work correctly but one test after another
how can I run more than one test in the same time each test in separate browser ??
and how to control the order of the tests to run ?
here is my DuskTestCase options :
protected function driver()
{
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--window-size=1920,1080',
]);
return RemoteWebDriver::create(
'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY,
$options
),
90000,
90000
);
}

Why I receive "CSRF token mismatch" while running tests in laravel?

I want to run my tests without receiving "CSRF token mismatch" exceptions. In the laravel documentation is noted that:
The CSRF middleware is automatically disabled when running tests.
the line of code where the exception is thrown looks like this:
$response = $this->json('POST', route('order.create'), [
'product_id', $product->id
]);
and for running tests I am working in my zsh terminal:
php artisan test --env=testing
This is my test class:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\TestCase;
class SessionCartTest extends TestCase
{
public function testExample()
{
$product = \App\Product::inRandomOrder()->first();
$response = $this->postJson(route('order.insert'), [
'product_id' => $product->id,
]);
$response->assertStatus(200); // here I receive 419
}
}
What am I doing wrong and how could I fix this? I am using laravel 7.
I ran into this problem x times now and each time I fix it by running:
php artisan config:clear
Probably the APP_ENV is not being set to testing.
You can set a ENV variable in the command line by preceding the php command.
So on your case set the environment to testing and run the artisan command by:
APP_ENV=testing php artisan test
Your data array is wrong. Try the following change:
$response = $this->postJson(route('order.insert'), [
'product_id' => $product->id, // use the arrow notation here.
]);
When you are running tests on Docker where the APP_ENV is hard coded with other values than testing (dev, local) in docker-compose.yaml file, phpunit cannot execute tests properly.
You will need to delete the all APP_ENV in docker files.
This works by setting a custom csrf-token
$this
->withSession(['_token' => 'bzz'])
->postJson('/url', ['_token' => 'bzz', 'other' => 'data']);

Laravel Dusk 2.0 / Laravel 5.5 returns an empty page

I'm in the midst of upgrading my Laravel 5.3 website to 5.5 and unable to get Dusk working properly against my localhost. I have other unit tests working properly against my localhost but for some reason, Dusk returns "<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>" for any local page. Local pages seem to work fine browsing it on my desktop.
My DuskTestCase
protected function driver()
{
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
'--no-sandbox',
'--ignore-certificate-errors'
]);
return RemoteWebDriver::create(
'http://localhost:9515',
DesiredCapabilities::chrome()
->setCapability(WebDriverCapabilityType::ACCEPT_SSL_CERTS, true)
->setCapability('acceptInsecureCerts', true)
->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
My Sample Test
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$result = $browser->visit('http://localhost');
$browser->screenshot('test');
$browser->dump();
});
}
chrome driver works fine calling google.com and dumps content
hardcoded the url to localhost for testing
page is http not https
tried php artisan serve before calling test, same result
tried cache / config clear
created an .env.dusk.local file, didn't seem to affect it
chromedriver -v is ChromeDriver 2.44.609551
tried 127.0.0.1 no dice
The only way I could make it working:
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit(env('APP_URL').'/home')
->assertSee('Laravel');
});
}

Laravel Dusk how to show the browser while executing tests

I am writing some tests and I want to see whether Dusk correctly fills in the input fields but Dusk doesn't show the browser while running the tests, is there any way to force it to do that?
Disable the headless mode in tests\DuskTestCase.php file driver() function:
$options = (new ChromeOptions)->addArguments([
//'--disable-gpu',
//'--headless'
]);
Updated (2021):
You can disable headless with 2 methods:
Method 1: Add this to your .env
DUSK_HEADLESS_DISABLED=true
Method 2: Add this to your special test case if you don't need to show the browser for all tests
protected function hasHeadlessDisabled(): bool
{
return true;
}
Btw, I don't know why these are not mentioned in the documentation. I found the above methods myself from DuskTestCase.php.
Answer for Laravel 8 & UP
You can use php artisan dusk --browse to force showing the browser.
Near the top of your tests/DuskTestCase.php file, add:
use Facebook\WebDriver\Chrome\ChromeOptions;
In that same file, replace the entire driver() function with:
/**
* Create the RemoteWebDriver instance.
*
* #return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver() {
$options = (new ChromeOptions)->addArguments([
//'--disable-gpu',
//'--headless'//https://stackoverflow.com/q/49938673/470749
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}

Resources