Using Travis CI built-in phantomjs along Phantoman Codeception extension.
Enter the phantom
PhantomJS is a steady presence in any acceptance test that will require JavaScript support.
While using Codeception I opt for faster solutions like PhpBrowser or the WPBrowser module for wp-browser but JavaScript might be integral to the application and in those cases a JavaScript-capable web driver comes into play. Adding some continuous integration to the mix and willing to use Travis CI for that I’ve encountered some difficulties setting up the Phantoman extension.
The neat extension will start and stop the phantomjs server on-demand any time a Codeception test requires it.
Path please
Phantoman configuration includes the possibility to specify the path to the phantomjs binary:
extensions:
enabled:
- Codeception\Extension\Phantoman
config:
Codeception\Extension\Phantoman:
path: '/usr/bin/phantomjs'
port: 4445
suites: ['acceptance']
and while this is known on my local setup it’s not set in stone in Travis CI, quoting the guide:
CI environment provides PhantomJS pre-installed (available in PATH as phantomjs; don’t rely on the exact location).
Since I do need a path to set up the distribution version of my project Codeception configuration file I’ve came up with a solution to avoid installing a second version of phantomjs from scratch.
Sed
As an example wp-browser local codeception.yml
file reads:
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\Phantoman
config:
Codeception\Extension\Phantoman:
path: '/usr/local/bin/phantomjs'
port: 4444
suites: ['phantomjs']
while the codeception.dist.yml
distribution version reads:
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\Phantoman
config:
Codeception\Extension\Phantoman:
path: 'phantomjsbin'
port: 4444
suites: ['phantomjs']
where PhantomJS binary absolute path is specified as phantomjsbin
.
The complimentary part to this is using sed
to replace that string with the correct path to the Travis CI PhantomJS binary at build time.
The relevant line of the .travis.yml
configuration file reads:
before_script:
# set the path to the phantomjs bin in Phantoman
- sed -i "s_phantomjsbin_$(which phantomjs)_" $TRAVIS_BUILD_DIR/codeception.dist.yml
The which phantomjs
command will expand to the phantomjs binary absolute path and that will replace the phantomjsbin
string.