56

GitHub - symfony/panthere: A browser testing and web crawling library for PHP an...

 5 years ago
source link: https://github.com/symfony/panthere
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Panthère

A browser testing and web scraping library for PHP and Symfony

Build Status

Panthère is a convenient standalone library to scrape websites and to run end-to-end tests using real browsers.

Panthère is super powerful, it leverages the W3C's WebDriver protocol to drive native web browsers such as Google Chrome and Firefox.

Panthère is very easy to use, because it implements the popular Symfony's 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 Panthère can be used in every PHP project, it's a standalone library.

Panthère 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 obscure driver.

In test mode, Panthère automatically starts your application using the PHP built-in web-server. You can just focus on writing your tests or web-scraping scenario, Panthère takes care of everything else.

Install

Use Composer to install Panthère in your project:

composer req symfony/panthere:dev-master

Basic Usage

<?php

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

$client = \Panthere\Client::createChromeClient();
$crawler = $client->request('GET', 'http://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!

Testing Usage

The PanthereTestCase 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 Panthère. It extends PHPUnit's TestCase and provide all testing tools you're used to.

<?php

use Panthere\PanthereTestCase;

class E2eTest extends PanthereTestCase
{
    public function testMyApp()
    {
        $client = static::createPanthereClient(); // Your app is automatically started using the built-in web server
        $crawler = $client->request('GET', static::$baseUri.'/mypage'); // static::$baseUri contains the base URL

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

To run this test:

phpunit tests/E2eTest.php

A Polymorph Feline

If you are testing a Symfony application, PanthereTestCase automatically extends the WebTestCase class. It means you can easily create functional tests, which can directly execute the kernel of your application and access all your existing services. Unlike the Panthère's client, the Symfony's testing client doesn't support JavaScript and screenshots capturing, but it is super-fast!

Alternatively (and even for non-Symfony apps), Panthère can also leverage the Goutte web scraping library, which is an intermediate between the Symfony's and the Panthère's test clients. Goutte sends real HTTP requests, it is fast and is able to browse any webpage, not only the ones of the application under test. But Goutte doesn't support JavaScript and other advanced features because it is entirely written in PHP.

The fun part is that the 3 libraries implement the exact same API, so you can switch from one to another just by calling the appropriate factory method, and find the good trade off for every single test case (do I need JavaScript, do I need to authenticate to an external SSO server, do I want to access the kernel of the current request...).

<?php

use Panthere\PanthereTestCase;

class E2eTest extends PanthereTestCase
{
    public function testMyApp()
    {
        $symfonyClient = static::createClient(); // A cute kitty: the Symfony's functional test too
        $goutteClient = static::createGoutteClient(); // An agile lynx: Goutte
        $panthereClient = static::createPanthereClient(); // A majestic Panther

        // Both Goutte and Panthère benefits from the built-in HTTP server

        // enjoy the same API for the 3 felines
        // $*client->request('GET', '...')

        $kernel = static::createKernel(); // You can also access to the app's kernel

        // ...
    }
}

Features

Unlike testing and web scraping libraries you're used to, Panthère:

  • executes the JavaScript code contained in webpages
  • supports everything that Chrome (or Firefox) implements
  • allows screenshots taking
  • can wait for the appearance of elements loaded asynchronously
  • 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

Documentation

Since Panthère implements the API of popular, it already has an extensive documentation:

Travis CI Integration

Panthère will work out of the box with Travis if you add the Chrome addon. Here is a minimal .travis.yml file to run Panthère tests:

language: php
addons:
  chrome: stable

php:
  - 7.2

script:
  - phpunit

Limitations

The following features are not currently supported:

  • Crawling XML documents (only HTML is supported)
  • Updating existing documents (browsers are mostly used to consume data, not to create webpages)
  • Setting form values using the multidimensional PHP array syntax
  • Methods returning an instance of \DOMElement (because this library uses WebDriverElement internally)
  • Selecting invalid choices in select

Pull Requests are welcome to fill the remaining gaps!

Credits

Created by Kévin Dunglas. Sponsored by Les-Tilleuls.coop.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK