<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>blog.johanv.org (Posts about codeception)</title><link>https://blog.johanv.org/</link><description></description><atom:link href="https://blog.johanv.org/en/categories/codeception.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2025 &lt;a href="https://blog.johanv.org/pages/contact/"&gt;Johan Vervloet&lt;/a&gt; 
&lt;a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/"&gt;
&lt;img alt="Creative Commons License BY-NC-SA"
style="border-width:0; margin-bottom:12px;"
src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png"&gt;&lt;/a&gt;</copyright><lastBuildDate>Wed, 29 Oct 2025 18:31:16 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Selenium web tests on gitlab-ci for an app using Mercure</title><link>https://blog.johanv.org/en/posts/gitlab-ci-selenium-and-mercure/</link><dc:creator>Johan Vervloet</dc:creator><description>&lt;div&gt;&lt;p&gt;I recently learned
&lt;a href="https://symfony.com/doc/current/mercure.html"&gt;how to use Mercure&lt;/a&gt;
so that the php/Symfony back-end of my
application can send out events that can be picked up by the
javascript on my front end. This is really cool technology, you should
definitely try this out if you haven't already done so.&lt;/p&gt;
&lt;p&gt;Now if you write this kind of software, of course you also want to
write some web tests, so that you can automatically check if existing
features of your program keep working after you add new things.&lt;/p&gt;
&lt;p&gt;&lt;img alt="gitlab-ci, codeception and mercure" src="https://blog.johanv.org/galleries/misc/gitlab-codeception-mercure-smaller.png"&gt;&lt;/p&gt;
&lt;p&gt;For these kind of tests, I use gitlab-ci and codeception, as I wrote
in &lt;a href="https://blog.johanv.org/categories/ci"&gt;previous posts&lt;/a&gt;. Today I got these test working
with a Mercure hub.&lt;/p&gt;
&lt;p&gt;You need to use a mercure service in the job running your web tests,
which you can configure in &lt;code&gt;.gitlab-ci.yml&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update 2020-12-03:&lt;/strong&gt; I use the 0.10 version of the mercure container,
because the acceptance test job started failing with 0.11, with these
error messages:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Message: "Couldn't connect to server for "http://mercure:3000/.well-known/mercure"."
In HandleMessageMiddleware.php line 80:&lt;/p&gt;
&lt;p&gt;Couldn't connect to server for "http://mercure:3000/.well-known/mercure".)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So here's the adapted &lt;code&gt;.gitlab-ci.yml&lt;/code&gt;-file:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nt"&gt;variables&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nt"&gt;JWT_KEY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;YourSecretKey&lt;/span&gt;
  &lt;span class="nt"&gt;DEMO&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;1&lt;/span&gt;
  &lt;span class="nt"&gt;ALLOW_ANONYMOUS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;1&lt;/span&gt;
  &lt;span class="nt"&gt;HEARTBEAT_INTERVAL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;30s&lt;/span&gt;
  &lt;span class="nt"&gt;ADDR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;':3000'&lt;/span&gt;
  &lt;span class="nt"&gt;CORS_ALLOWED_ORIGINS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'*'&lt;/span&gt;
  &lt;span class="nt"&gt;PUBLISH_ALLOWED_ORIGINS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'*'&lt;/span&gt;

&lt;span class="c1"&gt;# ...&lt;/span&gt;

&lt;span class="nt"&gt;acceptance tests&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# I would prefer to use a public php-7.3 image with mysqli, but&lt;/span&gt;
  &lt;span class="c1"&gt;# I don't think such a thing exists.&lt;/span&gt;
  &lt;span class="c1"&gt;# So let's reuse the image I created for the dikdikdik web tests.&lt;/span&gt;
  &lt;span class="nt"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;registry.gitlab.com/rva-vzw/dikdikdik.php&lt;/span&gt;
  &lt;span class="nt"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p p-Indicator"&gt;-&lt;/span&gt; &lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;mariadb:10.4&lt;/span&gt;
      &lt;span class="nt"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;db&lt;/span&gt;
    &lt;span class="p p-Indicator"&gt;-&lt;/span&gt; &lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;selenium/standalone-chrome&lt;/span&gt;
      &lt;span class="nt"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;chrome&lt;/span&gt;
    &lt;span class="p p-Indicator"&gt;-&lt;/span&gt; &lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;dunglas/mercure&lt;/span&gt;
      &lt;span class="nt"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="l l-Scalar l-Scalar-Plain"&gt;mercure:v0.10&lt;/span&gt;
  &lt;span class="c1"&gt;# (the rest of the job follows here)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;JWT_KEY&lt;/code&gt; should work with the &lt;code&gt;JWT&lt;/code&gt; your backend uses
to publish events. The port number in &lt;code&gt;ADDR&lt;/code&gt; as well. If you
use Symfony, as I do, the corresponding configuration of your
backend should be in the &lt;code&gt;.env&lt;/code&gt; file, e.g.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;MERCURE_PUBLISH_URL=http://mercure:3000/.well-known/mercure
MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOltdfX0.FFSjymJCGRDWrmAmPJDoVGuYwnx5FRTjRFkkYfvLkUg
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Needless to say: don't use the production JWT and token in the
source code that is used by gitlab! If you're unsure how to
create a JWT, see the section ‘Creating your JWT’
of 
&lt;a href="https://thedevopsguide.com/real-time-notifications-with-mercure/"&gt;this excellent blog post&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update 2020-12-03:&lt;/strong&gt; That blog post doesn't exist anymore, but 
&lt;a href="https://medium.com/@stefan.poeltl/instant-realtime-notifications-with-symfony-and-mercure-e45270f7c8a5"&gt;I found another page&lt;/a&gt;
that covers mercure and the JWT token.&lt;/p&gt;
&lt;p&gt;Now the problem I ran into, is similar to the
&lt;a href="https://blog.johanv.org/posts/gitlab-ci-codeception-and-selenium-web-tests"&gt;general problem I had with web tests&lt;/a&gt;:
the chrome container doesn't know where the mercure hub is;
the host name ‘mercure’ is not resolved to an ip address.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update 2022-06-20:&lt;/strong&gt; &lt;em&gt;The solution described below will still work, but
I found a better one. You should just set the
&lt;code&gt;FF_NETWORK_PER_BUILD&lt;/code&gt; variable to 1 in your &lt;code&gt;.gitlab-ci.yml&lt;/code&gt; file,
as described &lt;a href="https://blog.johanv.org/posts/php-ci-demo"&gt;in this post&lt;/a&gt;. If you want to see how
this works for a real life project, you can check out the
&lt;a href="https://gitlab.com/rva-vzw/dikdikdik/-/blob/develop/.gitlab-ci.yml"&gt;.gitlab-ci.yml&lt;/a&gt;
file of &lt;a href="https://gitlab.com/rva-vzw/dikdikdik/"&gt;dikdikdik&lt;/a&gt;, an app to
keep track of the scores when playing &lt;a href="https://www.rijkvanafdronk.be/wiezen"&gt;solo whist&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;To fix this, I figure out the IP address of the Mercure hub when I
start the job, and I replace the host name of the &lt;code&gt;MERCURE_PUBLISH_URL&lt;/code&gt;
in the &lt;code&gt;.env&lt;/code&gt; file by this IP-address. This works, since my frontend
retrieves the publish url from the backend by a rest-call.&lt;/p&gt;
&lt;p&gt;This is how that looks like:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;  script:
    - MERCURE_IP=$(getent hosts mercure | awk '{ print $1 }')
    - echo Mercure IP $MERCURE_IP
    - sed -i "s/mercure:3000/$MERCURE_IP:3000/" .env
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can review the complete job definition in the &lt;code&gt;.gitlab-ci.yml&lt;/code&gt;
&lt;a href="https://gitlab.com/rva-vzw/wdebelek/-/blob/develop/.gitlab-ci.yml#L129-161"&gt;file in the wdebelek repository&lt;/a&gt;.
The file &lt;code&gt;acceptance/TrickCest.php&lt;/code&gt; contains an
&lt;a href="https://gitlab.com/rva-vzw/wdebelek/-/blob/develop/tests/acceptance/TrickCest.php#L42"&gt;example test involving server sent events&lt;/a&gt;.
And hopefully you can see in the
&lt;a href="https://gitlab.com/rva-vzw/wdebelek/pipelines"&gt;pipelines&lt;/a&gt; that all
tests passed without problems 🤓.&lt;/p&gt;&lt;/div&gt;</description><guid>https://blog.johanv.org/en/posts/gitlab-ci-selenium-and-mercure/</guid><pubDate>Fri, 08 May 2020 20:15:00 GMT</pubDate></item></channel></rss>