PhantomJS is a quick, excellent alternative to native browser testing for both e2e testing with selenium and JavaScript unit testing with Karma. The beta version, PhantomJS 2.5 offers support for ES2015 unlike the current stable branch (at the time of writing this article 2.1.x).
Download PhantomJS 2.5 for Ubuntu
At the time of writing this article, the following versions are available:
- Ubuntu 16.04 LTS (xenial)
- Ubuntu 14.04 LTS (trusty)
- MacOS – unsure if works on El-Capitan or Yosemite. Will find a way to verify in the future.
Download the version for your operating system from:
https://bitbucket.org/ariya/phantomjs/downloads/
Add PhantomJS to Your Path
TL;DR Advanced
The PhantomJS binary is located under the bin dir of the extracted archive. If you have a preferred way of adding things to your path, go ahead and add it. If you dont know what that means, you can follow along below.
- After adding to your path, verify by running
which phantomjs
from the command line. - If everything looks good and you see the correct path, add the PHANTOMJS_BIN variable to your environment by typing
export PHANTOMJS_BIN=`which phantomjs`
- NOTE: Make sure you use the backtick character instead of the single quote mark for the sample above!
Instructions for Adding PhantomJS to Environment
These instructions are based off of the assumption that you downloaded the PhantomJS tarball to your downloads directory.
- Open your terminal and make sure you are in your home dir.
- Type
mkdir ./.phantomjs
- Move to your new dir
cd .phantomjs
- Move your archive into your folder
cp ~/Downloads/phantomjs-2.5.0-beta-linux-ubuntu-xenial-x86_64.tar.gz ./
(make sure you reference the correct file, as you are typing the filename, you can try hitting the tab key to autocomplete) - Extract your archive
tar -xvzf ./phantomjs-2.5.0-beta-linux-ubuntu-xenial-x86_64.tar.gz
- Rename the extracted dir
mv phantomjs-2.5.0-beta-ubuntu-xenial phantomjs
- Change dir into the extracted phantom dir
cd phantomjs
- Change dir into the phantom bin dir
cd bin
- Add executable permissions
chmod +x phantomjs
- Add to your path
echo "PATH=$PATH:`pwd`" >> ~/.bashrc
- Close and re-open your terminal or set source:
source ~/.bashrc
- Test by typing:
phantomjs --version
(at this point you should see the version. At the time of this post, 2.5.0-development).- If you dont get a version, you may need to add execute permissions. Browse back to the bin dir and type
chmod +x ./phantomjs
and try checking the version again. If you still don’t see a version number typesource ~/.bashrc
and typewhich phantomjs
(you should see a path to the phantomjs bin folder)
- If you dont get a version, you may need to add execute permissions. Browse back to the bin dir and type
- After you have verified that you are showing phantomjs 2.5+, you need to add the PHANTOMJS_BIN variable to override any other global installations. type:
echo "export PHANTOMJS_BIN=`which phantomjs`" >> ~/.bashrc
- Close and re-open your terminal or type:
source ~/.bashrc
- Verify PHANTOMJS_BIN var:
echo $PHANTOMJS_BIN
(you should see the executable path for your phantomjs 2.5).
Leave a Reply