Symfony panther

Symfony Panther is a convenient standalone library to scrape websites and to run end-to-end tests using real browsers. The library leverages the W3C’s WebDriver protocol to drive native web browsers such as Google Chrome and Firefox.

Symfony Panther is very easy to use, because it implements Symfony’s popular BrowserKit and DomCrawler APIs, and contains all features you need to test your apps. It will sound familiar if you have ever created a functional test for a Symfony app: as the API is exactly the same! Keep in mind that Panther can be used in every PHP project, as it is a standalone library.

The library automatically finds your local installation of Chrome and launches it, thanks to ChromeDriver, so you don’t need to install anything on your computer, neither Selenium server nor any other obscure driver.

Symfony panther

In test mode, Panther automatically starts your application using the PHP built-in web-server. You can focus on writing your tests or web-scraping scenario and Panther will take care of everything else.

Getting started with Symfony Panther

You can install Panther in your symfony project with composer :

composer req symfony/panther

A basic usage of the library, you can load a javascript-based web page, select a link then click on it, finally wait for an element to be rendered and take a screenshot :

<?php
require __DIR__.'/vendor/autoload.php'; // Composer's autoloader

$client = \Symfony\Component\Panther\Client::createChromeClient();
$crawler = $client->request('GET', 'https://api-platform.com'); // Yes, this website is 100% in JavaScript

$link = $crawler->selectLink('Support')->link();
$crawler = $client->click($link);

// Wait for an element to be rendered
$client->waitFor('.support');

echo $crawler->filter('.support')->text();
$client->takeScreenshot('screen.png'); // Yeah, screenshot!

The PantherTestCase class allows you to easily write E2E tests. It automatically starts your app using the built-in PHP web server and let you crawl it using Panther. It extends PHPUnit’s TestCase and provide all testing tools you’re used to.

<?php
namespace App\Tests;

use Symfony\Component\Panther\PantherTestCase;

class E2eTest extends PantherTestCase
{
    public function testMyApp()
    {
        $client = static::createPantherClient(); // Your app is automatically started using the built-in web server
        $crawler = $client->request('GET', '/mypage');

        $this->assertContains('My Title', $crawler->filter('title')->html()); // You can use any PHPUnit assertion
    }
}

Then run the test using :

phpunit tests/E2eTest.php

Features of Panther library include :

  • executes the JavaScript code contained in webpages
  • supports everything that Chrome (or Firefox) implements
  • allows screenshots taking
  • can wait for the appearance of asynchronously loaded elements
  • lets you run your own JS code or XPath queries in the context of the loaded page
  • supports custom Selenium server installations
  • supports remote browser testing services including SauceLabs and BrowserStack

More information at https://github.com/symfony/panther

LEAVE A REPLY

Please enter your comment!
Please enter your name here