behat/mink-goutte-driver
Deprecated Mink driver for the Goutte browser client, enabling fast, headless HTTP-based browsing and DOM interaction in Behat/Mink tests. Use for simple crawling/navigation without JavaScript; superseded by behat/mink-browserkit-driver.
Installation
Add to composer.json:
"require-dev": {
"behat/mink": "^1.9",
"behat/mink-goutte-driver": "^2.0"
}
Run:
composer require --dev behat/mink behat/mink-goutte-driver
Basic Configuration
In tests/CreatesApplication.php (or a custom Mink service provider):
use Behat\Mink\Mink;
use Behat\Mink\Session;
use Behat\Mink\Driver\GoutteDriver;
use Goutte\Client;
$this->mink = new Mink([
'goutte' => new Session(new GoutteDriver(new Client()))
]);
First Use Case: Scraping In a test class:
public function testScrapeHomepage()
{
$session = $this->mink->getSession('goutte');
$session->visit('/'); // Relative URL (Laravel's public path)
$title = $session->getPage()->getTitle();
$this->assertEquals('My App', $title);
}
Page Interaction
$session->visit('/dashboard');
$session->getPage()->clickLink('Logout');
$this->assertEquals('/logout', $session->getCurrentUrl());
Form Submission
$form = $session->getPage()->find('form', ['id' => 'login-form']);
$form->fillField('email', 'user@example.com');
$form->pressButton('Login');
Assertions
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->elementTextContains('css', '.alert', 'Success');
Dynamic Content
$session->visit('/posts');
$links = $session->getPage()->findAll('css', '.post-title a');
$this->assertCount(5, $links);
/ for routes (e.g., visit('/dashboard')).$session->reset();
Goutte\Client:
$client = new Client();
$client->getClient()->getOptions()->set('headers', ['X-Custom' => 'Header']);
Use Goutte for HTML-based API responses (e.g., SPAs):
$session->visit('/api/data');
$json = $session->getPage()->getContent();
$data = json_decode($json, true);
$this->assertArrayHasKey('results', $data);
Deprecation Warning
behat/mink-browserkit-driver. Migrate if possible:
composer require --dev behat/mink-browserkit-driver
GoutteDriver with BrowserKitDriver in your setup.PHP Version
Session State
$client->getClient()->getOptions()->set('cookies', true);
JavaScript Limitation
MinkSeleniumDriver or Laravel Dusk.file_put_contents('debug.html', $session->getPage()->getHtml());
$client->getClient()->getOptions()->set('debug', true);
$session->wait(2000); // 2-second wait
Custom Client
Extend Goutte\Client for reusable configurations:
class CustomGoutteClient extends Client {
public function __construct() {
$this->getClient()->getOptions()->set('timeout', 30);
}
}
Middleware Attach Laravel middleware to Goutte requests:
$client->getClient()->getOptions()->set('headers', [
'X-CSRF-TOKEN' => $this->app['session']->token()
]);
Testing Auth For authenticated routes, pass tokens/cookies:
$session->visit('/dashboard', [
'HTTP_X_XSRF_TOKEN' => $this->app['session']->token()
]);
--parallel cautiously.How can I help you explore Laravel packages today?