|
| 1 | +# Panthère |
| 2 | +**A browser testing and web scrapping library for [PHP](https://php.net) and [Symfony](https://symfony.com)** |
| 3 | + |
| 4 | +*Panthère* is a convenient standalone library to scrape websites and to run end to end tests **using real browsers**. |
| 5 | + |
| 6 | +Because it leverages [the W3C's WebDriver protocol](https://www.w3.org/TR/webdriver/) to drive native web browsers such |
| 7 | +as Google Chrome and Firefox, Panthère is super powerful. |
| 8 | + |
| 9 | +Because it implements the popular Symfony's [BrowserKit](https://symfony.com/doc/current/components/browser_kit.html) and |
| 10 | +[DomCrawler](https://symfony.com/doc/current/components/dom_crawler.html) APIs, Panthère is very easy to use, and contains |
| 11 | +all features you need to test your apps. It will sound familiar if you ever created [a functional test for a Symfony app](https://symfony.com/doc/current/testing.html#functional-tests): |
| 12 | +the API is exactly the same! |
| 13 | +Keep in mind that Panthère doesn't depend of Symfony, it's a standalone library. |
| 14 | + |
| 15 | +Because Panthère automatically finds your local installation of Chrome and launches it (thanks to [ChromeDriver](https://sites.google.com/a/chromium.org/chromedriver/)), |
| 16 | +you don't have anything to install on your computer: no Selenium server nor obscure driver. |
| 17 | +In test mode, Panthère automatically starts your application using [the PHP built-in web-server](http://php.net/manual/en/features.commandline.webserver.php). |
| 18 | +Focus on writing your tests or web-scrapping scenario, Panthère takes care of everything else. |
| 19 | + |
| 20 | +## Install |
| 21 | + |
| 22 | +Use [Composer](https://getcomposer.org/) to install Panthère in your project: |
| 23 | + |
| 24 | + composer req dunglas/panthere |
| 25 | + |
| 26 | +## Basic Usage |
| 27 | + |
| 28 | +```php |
| 29 | +<?php |
| 30 | + |
| 31 | +require __DIR__.'/vendor/autoload.php'; |
| 32 | + |
| 33 | +$client = new \Panthere\Client(); |
| 34 | +$crawler = $client->request('GET', 'http://api-platform.com'); // Yes, this website is 100% in JavaScript |
| 35 | + |
| 36 | +$link = $crawler->selectLink('Support')->link(); |
| 37 | +$crawler = $client->click($link); |
| 38 | + |
| 39 | +// Wait for an element to be rendered |
| 40 | +$client->getWebDriver()->wait()->until( |
| 41 | + \Facebook\WebDriver\WebDriverExpectedCondition::visibilityOfElementLocated(\Facebook\WebDriver\WebDriverBy::className('support')) |
| 42 | +); |
| 43 | + |
| 44 | +echo $crawler->filter('.support')->text(); |
| 45 | +$client->getWebDriver()->takeScreenshot('screen.png'); // Yeah, screenshot! |
| 46 | + |
| 47 | +$client->stop(); |
| 48 | +``` |
| 49 | + |
| 50 | +## Testing Usage |
| 51 | + |
| 52 | +The `PanthereTestCase` class allows you to easily write E2E tests. It automatically starts your app using the built-in PHP |
| 53 | +web server and let you crawl it using Panthère. |
| 54 | +It extends [PHPUnit](https://phpunit.de/)'s `TestCase` and provide all testing tools you're used to. |
| 55 | + |
| 56 | +```php |
| 57 | +<?php |
| 58 | + |
| 59 | +use Panthere\PanthereTestCase; |
| 60 | + |
| 61 | +class E2eTest extends PanthereTestCase |
| 62 | +{ |
| 63 | + public function testMyApp() |
| 64 | + { |
| 65 | + $client = static::createPanthereClient(); // Your app is automatically started using the built-in web server |
| 66 | + $crawler = $client->request('GET', static::$baseUri.'/mypage'); // static::$baseUri contains the base URL |
| 67 | + |
| 68 | + $this->assertContains('My Title', $crawler->filter('title')->text()); // You can use any PHPUnit assertion |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +To run this test: |
| 74 | + |
| 75 | + phpunit tests/E2eTest.php |
| 76 | + |
| 77 | +### A Polymorph Feline |
| 78 | + |
| 79 | +If you are testing a Symfony application, `PanthereTestCase` automatically extends the `WebTestCase` class. It means that |
| 80 | +you can easily create functional tests executing directly the kernel of your application and accessing all your existing |
| 81 | +services. Unlike the Panthère's client, the Symfony's testing client doesn't support JavaScript or taking screenshots, but |
| 82 | +it is super-fast! |
| 83 | + |
| 84 | +Alternatively (and even for non-Symfony apps), Panthère can also leverage the [Goutte](https://github.com/FriendsOfPHP/Goutte) |
| 85 | +web scrapping library. Goutte is an intermediate between the Symfony's test client and the Panthère one: it sends real HTTP |
| 86 | +requests, is fast and can browse any webpage, not only the ones of the application under test. |
| 87 | +But, because it is entirely written in PHP, Goutte doesn't support JavaScript and other advanced features. |
| 88 | + |
| 89 | +The fun part is that the 3 libraries implement the exact same API, so you can switch from one to another just by calling |
| 90 | +the appropriate factory method, and find the good trade off for every single test case (do I need JavaScript, do I need |
| 91 | +to authenticate to an external SSO server, do I want to access the kernel of the current request...). |
| 92 | + |
| 93 | +```php |
| 94 | +<?php |
| 95 | + |
| 96 | +use Panthere\PanthereTestCase; |
| 97 | + |
| 98 | +class E2eTest extends PanthereTestCase |
| 99 | +{ |
| 100 | + public function testMyApp() |
| 101 | + { |
| 102 | + $symfonyClient = static::createClient(); // A cute kitty: the Symfony's functional test too |
| 103 | + $goutteClient = static::createGoutteClient(); // An agile lynx: Goutte |
| 104 | + $panthereClient = static::createGoutteClient(); // A majestic Panther |
| 105 | + |
| 106 | + // Both Goutte and Panthère benefits from the built-in HTTP server |
| 107 | + |
| 108 | + // enjoy the same API for the 3 felines |
| 109 | + // $*client->request('GET', '...') |
| 110 | + |
| 111 | + $kernel = static::createKernel(); // You can also access to the app's kernel |
| 112 | + |
| 113 | + // ... |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## Features |
| 119 | + |
| 120 | +Unlike testing and web scrapping libraries you're used to, Panthère: |
| 121 | + |
| 122 | +* executes the JavaScript code contained in webpages |
| 123 | +* supports all everything that Chrome (or Firefox) implements |
| 124 | +* can take screenshots |
| 125 | +* can wait for the appearance of elements loaded asynchronously |
| 126 | +* lets you run your own JS code or XPath queries in the context of the loaded page |
| 127 | +* supports custom [Selenium server](https://www.seleniumhq.org) installations |
| 128 | +* supports remote browser testing services including [SauceLabs](https://saucelabs.com/) and [BrowserStack](https://www.browserstack.com/) |
| 129 | + |
| 130 | +## Documentation |
| 131 | + |
| 132 | +Because Panthère implements the API of popular, it already has an extensive documentation: |
| 133 | + |
| 134 | +* For the `Client` class, read [the BrowserKit's documentation](https://symfony.com/doc/current/components/browser_kit.html) |
| 135 | +* For the `Crawler` class, read [the DomCrawler's documentation](https://symfony.com/doc/current/components/dom_crawler.html) |
| 136 | +* For Webdriver, read [the Facebook's PHP WebDriver documentation](https://github.com/facebook/php-webdriver) |
| 137 | + |
| 138 | +## Limitations |
| 139 | + |
| 140 | +The following features are not currently supported: |
| 141 | + |
| 142 | +* Crawling XML documents (only HTML is supported) |
| 143 | +* Updating existing documents (browsers are mostly used to consume data, not to create webpages) |
| 144 | +* Setting form values using the multidimensional PHP array syntax |
| 145 | +* Methods returning an instance of `\DOMElement` (because this library uses `WebDriverElement` internally) |
| 146 | +* Selecting invalid choices in select |
| 147 | + |
| 148 | +Pull Requests are welcome to fill the remaining gaps! |
| 149 | + |
| 150 | +## Credits |
| 151 | + |
| 152 | +Created by [Kévin Dunglas](https://dunglas.fr). Sponsored by [Les-Tilleuls.coop](https://les-tilleuls.coop). |
0 commit comments