Symfony Panther is a browser testing and web scraping library for PHP that leverages the WebDriver protocol. It allows you to control browsers like Chrome and Firefox programmatically and is often used for end-to-end testing of web applications, as well as for scraping content from websites.
The system requirements for running Symfony Panther are as follows:
PHP Version: Symfony Panther requires PHP 7.1 or higher. It's always recommended to use the latest stable version of PHP for better performance and security.
Composer: Composer is required for managing the dependencies of the Panther package. Make sure you have Composer installed on your system.
Symfony Components: Panther is designed to work with the Symfony framework, so it may require some Symfony components. However, you can use Panther independently of the full Symfony framework.
WebDriver Binary: To control browsers, you need to have the appropriate WebDriver binary installed:
- For Google Chrome, you'll need
chromedriver
. - For Mozilla Firefox, you'll need
geckodriver
.
- For Google Chrome, you'll need
Browser: You need to have a supported browser installed on your system:
- Google Chrome or Chromium for use with
chromedriver
. - Mozilla Firefox for use with
geckodriver
.
- Google Chrome or Chromium for use with
PHPUnit (optional): If you are going to use Panther for testing, you might need PHPUnit, which is a PHP testing framework.
Operating System: Symfony Panther can be run on various operating systems such as Windows, macOS, and Linux, as long as the above requirements are met.
Installation Steps:
Here are the general steps to set up Symfony Panther in your PHP project:
- Install Symfony Panther using Composer:
composer require symfony/panther
- Download WebDriver Binary:
You can manually download the chromedriver
or geckodriver
binary compatible with your system and browser version, or you can use a tool like WebDriverManager
in PHP to download it automatically.
composer require dbrekelmans/bdi
php vendor/bin/bdi detect drivers
- Install a Browser:
Ensure that the browser you want to control with Panther is installed on your system. For headless testing (which doesn't require a graphical user interface), both Chrome and Firefox support headless modes.
Sample Code to Use Symfony Panther:
<?php
require __DIR__.'/vendor/autoload.php'; // Autoload files using Composer autoload
use Symfony\Component\Panther\PantherTestCase;
class MyPantherTest extends PantherTestCase
{
public function testMyApp()
{
$client = static::createPantherClient(); // This will start the WebDriver server and open a browser
$crawler = $client->request('GET', 'https://example.com');
// Do something with the crawler or client, like navigating or extracting information
$this->assertContains('Example Domain', $crawler->filter('h1')->text());
}
}
Before running your tests or scraper, make sure your system meets all the above requirements and that all necessary components are installed and configured properly. If you encounter issues, check the compatibility between the versions of the browsers, WebDriver binaries, and the Panther library.