How to test your Sylius plugins with Selenium?
Tags: php, sylius, behat, selenium
Published: 2020-03-12 09:12:59

---
title: How to test your Sylius plugins with Selenium?
published: true
date: 2019-08-05 00:00:00 UTC
tags: php, sylius, behat, selenium
canonical_url: https://www.dylan-ballandras.fr/blog/test-sylius-plugins-with-selenium-docker/
---

At [Tataragne Interactive](https://www.tataragne.com/), we’re often working on Sylius features through plugins. We use Behat with Selenium for scenarii depending on Javascript functionnalities.

> Behat is an open source Behavior-Driven Development framework for PHP. BDD is a methodology for developing software through continuous example-based communication between developers and a business, which this application supports. Scenarii are written in a special format called Gherkin.

Through this blog post, you will learn how to configure your Sylius Plugin project to use Selenium. I consider that you are using [the Sylius plugin skeleton](https://github.com/Sylius/PluginSkeleton).

## [<svg viewbox="0 0 16 16" width="16" height="16" fill="currentcolor" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#adding-selenium-to-our-docker-compose-configuration)Adding Selenium to our docker-compose configuration

First, we need to a chrome-node (or firefox) to work. We will use two containers. One for the Hub and one for the node. The Hub is the machine where all the tests will run but the code be executed on different nodes as stated on [the Selenium documentation](https://www.seleniumhq.org/docs/07_selenium_grid.jsp#how-selenium-grid-works-with-a-hub-and-nodes).

```yaml
# docker-compose.yaml
version: '3.7'

services:
    plugin: # Your app configuration. This is not the subject here

    hub:
        image: selenium/hub:3.11.0
        ports:
            - 4444:4444
        links:
            - plugin

    chrome:
        image: selenium/node-chrome:3.11.0
        #  You can also use this image and open the  container port to one of your host port to be able to use a VNC viewer
        # image: selenium/node-chrome-debug:3.11.0
        depends_on:
            - hub
        links:
            - plugin
        environment:
            - HUB_HOST=hub
            - HUB_PORT=4444
        # ports:
        #     - 5900:5900
    
```

* * *

With this configuration, you will be able to run `docker-compose up -d` and use it easily with your `behat.yaml` configuration.

## [<svg viewbox="0 0 16 16" width="16" height="16" fill="currentcolor" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#what-do-you-need-to-change-in-behatyml-to-be-able-to-run-selenium)What do you need to change in behat.yml to be able to run Selenium

The changes in your Behat configuration are really simple.

First, your may change the `base_url` of the `Behat\MinkExtension` depending on what you set earlier for the app. For me, in could simply be `"http://plugin:80/"` to target my container on the (not secure 😅) port 80.

Secondly, you have to add your Hub host to your chrome session. Simple add a key `wd_host` in the `selenium2`configuration of your chrome session.

Thirdly, you can set `javascriptEnabled` to `true` in capabilities.

Follow the example configuration below.

```yaml
Behat\MinkExtension:
    files_path: "%paths.base%/vendor/sylius/sylius/src/Sylius/Behat/Resources/fixtures/"
    base_url: "http://plugin:80/"  # Adapt this
    default_session: symfony
    javascript_session: chrome
    sessions:
        symfony:
            symfony: ~
        chrome:
            selenium2:
                browser: chrome
                wd_host: http://hub:4444/wd/hub # Add this
                capabilities:
                    browserName: chrome
                    browser: chrome
                    version: ""
                    marionette: null # https://github.com/Behat/MinkExtension/pull/311
                    javascriptEnabled: true  # Add this
                    chrome:
                        switches:
                            - "start-fullscreen"
                            - "start-maximized"
                            - "no-sandbox"

```

* * *

🎉🎉🎉 Your configuration is now ready 🎉🎉🎉

And as stated in [this issue of SymfonyExtension](https://github.com/FriendsOfBehat/SymfonyExtension/issues/80), you might encounter a `Session "symfony" is not registered.`. You can follow the changes of the [#138 Pull Request in the Refund Plugin](https://github.com/Sylius/RefundPlugin/pull/138/files).

I hope this blog post helped you configure your project for admin or shop scenarii within a Sylius plugin.And I wish to be able to talk about running those tests inside a Gitlab CI job in a future article. 💪