Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mink Goutte Driver Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup in Laravel

  1. 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
    
  2. 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()))
    ]);
    
  3. 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);
    }
    

Implementation Patterns

Common Workflows

  1. Page Interaction

    $session->visit('/dashboard');
    $session->getPage()->clickLink('Logout');
    $this->assertEquals('/logout', $session->getCurrentUrl());
    
  2. Form Submission

    $form = $session->getPage()->find('form', ['id' => 'login-form']);
    $form->fillField('email', 'user@example.com');
    $form->pressButton('Login');
    
  3. Assertions

    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->elementTextContains('css', '.alert', 'Success');
    
  4. Dynamic Content

    $session->visit('/posts');
    $links = $session->getPage()->findAll('css', '.post-title a');
    $this->assertCount(5, $links);
    

Integration with Laravel

  • Relative URLs: Use / for routes (e.g., visit('/dashboard')).
  • Session Persistence: Reset the driver between tests:
    $session->reset();
    
  • Custom Headers: Extend Goutte\Client:
    $client = new Client();
    $client->getClient()->getOptions()->set('headers', ['X-Custom' => 'Header']);
    

Testing APIs

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);

Gotchas and Tips

Pitfalls

  1. Deprecation Warning

    • This driver is deprecated in favor of behat/mink-browserkit-driver. Migrate if possible:
      composer require --dev behat/mink-browserkit-driver
      
    • Replace GoutteDriver with BrowserKitDriver in your setup.
  2. PHP Version

    • Requires PHP 7.2+. Avoid using with older versions.
  3. Session State

    • Goutte does not maintain session cookies across requests by default. Use middleware or Laravel’s session driver if needed:
      $client->getClient()->getOptions()->set('cookies', true);
      
  4. JavaScript Limitation

    • Goutte is headless and non-JS. For JS-heavy apps, use MinkSeleniumDriver or Laravel Dusk.

Debugging Tips

  • Inspect HTML: Dump the page content:
    file_put_contents('debug.html', $session->getPage()->getHtml());
    
  • Check Headers: Log the request/response:
    $client->getClient()->getOptions()->set('debug', true);
    
  • Slow Requests: Add delays:
    $session->wait(2000); // 2-second wait
    

Extension Points

  1. Custom Client Extend Goutte\Client for reusable configurations:

    class CustomGoutteClient extends Client {
        public function __construct() {
            $this->getClient()->getOptions()->set('timeout', 30);
        }
    }
    
  2. Middleware Attach Laravel middleware to Goutte requests:

    $client->getClient()->getOptions()->set('headers', [
        'X-CSRF-TOKEN' => $this->app['session']->token()
    ]);
    
  3. Testing Auth For authenticated routes, pass tokens/cookies:

    $session->visit('/dashboard', [
        'HTTP_X_XSRF_TOKEN' => $this->app['session']->token()
    ]);
    

Performance

  • Reuse Sessions: Avoid creating new sessions per test; reset instead.
  • Parallel Tests: Goutte is lightweight but may conflict with shared state in parallel runs. Use --parallel cautiously.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky