Skip to content

Latest commit

 

History

History
102 lines (77 loc) · 3.32 KB

behat-panther.md

File metadata and controls

102 lines (77 loc) · 3.32 KB

Wanna see some Panther?

In the previous blog post, we successfully configured Behat with Symfony 5. Everything was super cool, and we used Selenium to operate our tests that required JavaScript. But what about using Panther instead? Panther is an alternative to Selenium that can be a bit easier to use and faster, because it allows you to run tests in a "headless" browser (i.e. the tests run in a browser but without visually opening the browser).

Installing Symfony Panther extension for Behat

Ok, we need a Panther! So... let's go to the Zoo! Or, maybe just install the digital version with Composer:

composer require robertfausk/behat-panther-extension --dev

This library integrates Panther with Behat... and though it's still pretty young, it works really well!

After it finishes, you won't find many changes to your project: only the composer.json and composer.lock files have changed. But you will see that the symfony/panther recipe post-install prints a recommendation:

Install ChromeDriver or geckodriver

Woohoo! That's something we already did! If you missed it - check out the previous blog post.

The post-install text also says:

To use Panther with PHPUnit, uncomment the snippet registering the Panther extension in phpunit.xml.dist

We won't be using Panther inside PHPUnit, so you can ignore this.

Configuring PantherExtension

We now have a PantherExtension in our app, which is like a "plugin" between Behat and Panther. To tell Behat to use it, update your behat.yaml.dist file:

default:
    # ...
    extensions:
        # ...
        Robertfausk\Behat\PantherExtension: ~ # no configuration here
        Behat\MinkExtension:
        # ...

Now we need to configure Behat\MinkExtension to use Panther for the JavaScript session. Here is the minimum configuration:

default:
    # ...
    extensions:
        # ...
        Behat\MinkExtension:
            # ...
            javascript_session: panther
            # ...
            panther:
                options:
                    browser: 'chrome'

To run tests, open your terminal and execute:

vendor/bin/behat

And... woohoo, everything should work! You won't see the browser open (like you did with Selenium), but your tests are being run in a real browser. If you get an exception like:

The port 9080 is already in use. (RuntimeException)

No problem, just configure another port for Panther:

default:
    # ...
    extensions:
        # ...
        Behat\MinkExtension:
            # ...
            panther:
                options:
                    port: 9081  # or another port you like
                    browser: 'chrome'

By the way, you can find all available configuration options in PantherTestCaseTrait::$defaultOptions.

As you can see, configuring and using Panther is a bit easier than Selenium, which we like. You can find more usage examples in the robertfausk/behat-panther-extension repository

That's all folks!