Download the PHP package symfony/panther without Composer
On this page you can find all versions of the php package symfony/panther. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package panther
A browser testing and web scraping library for PHP and Symfony
Panther is a convenient standalone library to scrape websites and to run end-to-end tests using real browsers.
Panther is super powerful. It leverages the W3C's WebDriver protocol to drive native web browsers such as Google Chrome and Firefox.
Panther is very easy to use, because it implements Symfony's popular BrowserKit and DomCrawler APIs, and contains all the 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.
Panther automatically finds your local installation of Chrome or Firefox and launches them, so you don't need to install anything else on your computer, a Selenium server is not needed!
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.
Features
Unlike testing and web scraping libraries you're used to, Panther:
- executes the JavaScript code contained in webpages
- supports everything that Chrome (or Firefox) implements
- allows taking screenshots
- can wait for asynchronously loaded elements to show up
- 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
Installing Panther
Use Composer to install Panther in your project. You may want to use the --dev
flag if you want to use Panther for testing only and not for web scraping in a production environment:
composer req symfony/panther
composer req --dev symfony/panther
Installing ChromeDriver and geckodriver
Panther uses the WebDriver protocol to control the browser used to crawl websites.
On all systems, you can use dbrekelmans/browser-driver-installer
to install ChromeDriver and geckodriver locally:
composer require --dev dbrekelmans/bdi
vendor/bin/bdi detect drivers
Panther will detect and use automatically drivers stored in the drivers/
directory.
Alternatively, you can use the package manager of your operating system to install them.
On Ubuntu, run:
apt-get install chromium-chromedriver firefox-geckodriver
On Mac, using Homebrew:
brew install chromedriver geckodriver
On Windows, using chocolatey:
choco install chromedriver selenium-gecko-driver
Finally, you can download manually ChromeDriver (for Chromium or Chrome)
and GeckoDriver (for Firefox) and put them anywhere in your PATH
or in the drivers/
directory of your project.
Registering the PHPUnit Extension
If you intend to use Panther to test your application, we strongly recommend registering the Panther PHPUnit extension. While not strictly mandatory, this extension dramatically improves the testing experience by boosting the performance and allowing to use the interactive debugging mode.
When using the extension in conjunction with the PANTHER_ERROR_SCREENSHOT_DIR
environment variable, tests using the
Panther client that fail or error (after the client is created) will automatically get a screenshot taken to help
debugging.
To register the Panther extension, add the following lines to phpunit.xml.dist
:
Without the extension, the web server used by Panther to serve the application under test is started on demand and
stopped when tearDownAfterClass()
is called.
On the other hand, when the extension is registered, the web server will be stopped only after the very last test.
Basic Usage
Testing Usage
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.
To provide all the testing tools you're used to, it extends PHPUnit's TestCase
.
If you are testing a Symfony application, PantherTestCase
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. In this case, you can use all crawler test assertions
provided by Symfony with Panther.
To run this test:
bin/phpunit tests/E2eTest.php
A Polymorphic Feline
Panther also gives you instant access to other BrowserKit-based implementations of Client
and Crawler
.
Unlike Panther's native client, these alternative clients don't support JavaScript, CSS and screenshot capturing,
but they are super-fast!
Two alternative clients are available:
- The first directly manipulates the Symfony kernel provided by
WebTestCase
. It is the fastest client available, but it is only available for Symfony apps. - The second leverages Symfony's HttpBrowser. It is an intermediate between Symfony's kernel and Panther's test clients. HttpBrowser sends real HTTP requests using Symfony's HttpClient component. It is fast and is able to browse any webpage, not only the ones of the application under test. However, HttpBrowser doesn't support JavaScript and other advanced features because it is entirely written in PHP. This one is available even for non-Symfony apps!
The fun part is that the 3 clients implement the exact same API, so you can switch from one to another just by calling the appropriate factory method, resulting in a good trade-off for every single test case (Do I need JavaScript? Do I need to authenticate with an external SSO server? Do I want to access the kernel of the current request? ... etc).
Here is how to retrieve instances of these clients:
Creating Isolated Browsers to Test Apps Using Mercure or WebSocket
Panther provides a convenient way to test applications with real-time capabilities which use Mercure, WebSocket and similar technologies.
PantherTestCase::createAdditionalPantherClient()
creates additional, isolated browsers which can interact with each other.
For instance, this can be useful to test a chat application having several users connected simultaneously:
Accessing Browser Console Logs
If needed, you can use Panther to access the content of the console:
Passing Arguments to ChromeDriver
If needed, you can configure the arguments to pass to the chromedriver
binary:
Checking the State of the WebDriver Connection
Use the Client::ping()
method to check if the WebDriver connection is still active (useful for long-running tasks).
Additional Documentation
Since Panther implements the API of popular libraries, it already has an extensive documentation:
- For the
Client
class, read the BrowserKit documentation - For the
Crawler
class, read the DomCrawler documentation - For WebDriver, read the PHP WebDriver documentation
Environment Variables
The following environment variables can be set to change some Panther's behaviour:
PANTHER_NO_HEADLESS
: to disable the browser's headless mode (will display the testing window, useful to debug)PANTHER_WEB_SERVER_DIR
: to change the project's document root (default to./public/
, relative paths must start by./
)PANTHER_WEB_SERVER_PORT
: to change the web server's port (default to9080
)PANTHER_WEB_SERVER_ROUTER
: to use a web server router script which is run at the start of each HTTP requestPANTHER_EXTERNAL_BASE_URI
: to use an external web server (the PHP built-in web server will not be started)PANTHER_APP_ENV
: to override theAPP_ENV
variable passed to the web server running the PHP appPANTHER_ERROR_SCREENSHOT_DIR
: to set a base directory for your failure/error screenshots (e.g../var/error-screenshots
)PANTHER_DEVTOOLS
: to toggle the browser's dev tools (defaultenabled
, useful to debug)PANTHER_ERROR_SCREENSHOT_ATTACH
: to add screenshots mentioned above to test output in junit attachment format
Changing the Hostname and Port of the Built-in Web Server
If you want to change the host and/or the port used by the built-in web server, pass the hostname
and port
to the $options
parameter of the createPantherClient()
method:
Chrome-specific Environment Variables
PANTHER_NO_SANDBOX
: to disable Chrome's sandboxing (unsafe, but allows to use Panther in containers)PANTHER_CHROME_ARGUMENTS
: to customize Chrome arguments. You need to setPANTHER_NO_HEADLESS
to fully customize.PANTHER_CHROME_BINARY
: to use anothergoogle-chrome
binary
Firefox-specific Environment Variables
PANTHER_FIREFOX_ARGUMENTS
: to customize Firefox arguments. You need to setPANTHER_NO_HEADLESS
to fully customize.PANTHER_FIREFOX_BINARY
: to use anotherfirefox
binary
Accessing To Hidden Text
According to the spec, WebDriver implementations return only the displayed text by default.
When you filter on a head
tag (like title
), the method text()
returns an empty string. Use the method html()
to get
the complete contents of the tag, including the tag itself.
Interactive Mode
Panther can make a pause in your tests suites after a failure.
It is a break time really appreciated for investigating the problem through the web browser.
For enabling this mode, you need the --debug
PHPUnit option without the headless mode:
$ PANTHER_NO_HEADLESS=1 bin/phpunit --debug
Test 'App\AdminTest::testLogin' started
Error: something is wrong.
Press enter to continue...
To use the interactive mode, the PHPUnit extension must be registered.
Using an External Web Server
Sometimes, it's convenient to reuse an existing web server configuration instead of starting the built-in PHP one.
To do so, set the external_base_uri
option:
Having a Multi-domain Application
It happens that your PHP/Symfony application might serve several different domain names.
As Panther saves the Client in memory between tests to improve performances, you will have to run your tests in separate processes if you write several tests using Panther for different domain names.
To do so, you can use the native @runInSeparateProcess
PHPUnit annotation.
ℹ Note: it is really convenient to use the external_base_uri
option and start your own webserver in the background,
because Panther will not have to start and stop your server on each test. Symfony CLI can
be a quick and easy way to do so.
Here is an example using the external_base_uri
option to determine the domain name used by the Client:
Using a Proxy
To use a proxy server, set the following environment variable: PANTHER_CHROME_ARGUMENTS='--proxy-server=socks://127.0.0.1:9050'
Accepting Self-signed SSL Certificates
To force Chrome to accept invalid and self-signed certificates, set the following environment variable: PANTHER_CHROME_ARGUMENTS='--ignore-certificate-errors'
This option is insecure, use it only for testing in development environments, never in production (e.g. for web crawlers).
For Firefox, instantiate the client like this:
Docker Integration
Here is a minimal Docker image that can run Panther with both Chrome and Firefox:
Build it with docker build . -t myproject
Run it with docker run -it -v "$PWD":/srv/myproject -w /srv/myproject myproject bin/phpunit
GitHub Actions Integration
Panther works out of the box with GitHub Actions.
Here is a minimal .github/workflows/panther.yml
file to run Panther tests:
Travis CI Integration
Panther will work out of the box with Travis CI if you add the Chrome addon.
Here is a minimal .travis.yml
file to run Panther tests:
Gitlab CI Integration
Here is a minimal .gitlab-ci.yml
file to run Panther tests with Gitlab CI:
AppVeyor Integration
Panther will work out of the box with AppVeyor as long as Google Chrome is installed.
Here is a minimal appveyor.yml
file to run Panther tests:
Usage with Other Testing Tools
If you want to use Panther with other testing tools like LiipFunctionalTestBundle
or if you just need to use a different base class, Panther has got you covered.
It provides you with the Symfony\Component\Panther\PantherTestCaseTrait
and you can use it to enhance your existing
test-infrastructure with some Panther awesomeness:
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 usesWebDriverElement
internally) - Selecting invalid choices in select
Pull Requests are welcome to fill the remaining gaps!
Troubleshooting
Run with Bootstrap 5
If you are using Bootstrap 5, then you may have a problem with testing. Bootstrap 5 implements a scrolling effect, which tends to mislead Panther.
To fix this, we advise you to deactivate this effect by setting the Bootstrap 5 $enable-smooth-scroll variable to false in your style file.
Save the Panthers
Many of the wild cat species are highly threatened. If you like this software, help save the (real) panthers by donating to the Panthera organization.
Credits
Created by Kévin Dunglas. Sponsored by Les-Tilleuls.coop.
Panther is built on top of PHP WebDriver and several other FOSS libraries. It has been inspired by Nightwatch.js, a WebDriver-based testing tool for JavaScript.
All versions of panther with dependencies
ext-dom Version *
ext-libxml Version *
php-webdriver/webdriver Version ^1.8.2
symfony/browser-kit Version ^5.3 || ^6.0 || ^7.0
symfony/dependency-injection Version ^5.3 || ^6.0 || ^7.0
symfony/deprecation-contracts Version ^2.4 || ^3
symfony/dom-crawler Version ^5.3 || ^6.0 || ^7.0
symfony/http-client Version ^5.3 || ^6.0 || ^7.0
symfony/http-kernel Version ^5.3 || ^6.0 || ^7.0
symfony/process Version ^5.3 || ^6.0 || ^7.0