< go back to home

Running Laravel Dusk in Non-Desktop Environment (Centos and Ubuntu)

July 2, 2017

Laravel Dusk is a built in browser testing tool for end-to-end tests. It is awesome because it is easy to setup out of the box within the Laravel framework which makes it a great alternative to other testing tools like Selenium.

By default though, it requires an environment with GUI as tests runs in actual browser (specifically Chrome). This is OK for development machines as usually, you already have a browser installed in an environment with GUI. Running Laravel Dusk though will be an issue for non-GUI environments due to the lack of browsers.

There are use cases where you want or need to run the test in non-GUI environment:

Fortunately, Laravel Dusk will work with headless browsers. Here, we will show the setups that I found around the internet to be able to run tests in headless browsers.

Ubuntu 16.04

For Ubuntu, we will basically use xvfb library that can emulate display hardwares. This will allow to run Chrome in a non-GUI environment.

# makes sure all your repos are up to date
sudo apt-get update

# chrome dependencies I think
sudo apt-get -y install libxpm4 libxrender1 libgtk2.0-0 libnss3 libgconf-2-4

# chromium is what I had success with on Codeship, so seemed a good option
sudo apt-get install chromium-browser

# XVFB for headless applications
sudo apt-get -y install xvfb gtk2-engines-pixbuf

# fonts for the browser
sudo apt-get -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable

# support for screenshot capturing
sudo apt-get -y install imagemagick x11-apps

References:

Note: Google is releasing a headless option for Chrome as of Chrome 59. This means xvfb won't be required. I'll update this article once I tested it.

Centos 7

For CentOS, installing Chrome is not as straightforward as of the writing of this blog due to unavailability of Chrome to Centos. We will use a headless browser PhantomJS instead. Since PhantomJS is already headless, which means it is capable of running in non-GUI environments, we don't have to install xvfb.

# fetch PhantomJS installation file
sudo curl -O https://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-i686.tar.bz2

# unpack the tar installation file
sudo tar xvf phantomjs-1.9.1-linux-i686.tar.bz2

# enable "phantomjs" command
sudo cp phantomjs-1.9.1-linux-i686/bin/phantomjs /usr/local/bin

# run PhantomJS in port 9515
phantomjs --webdriver=127.0.0.1:9515 &

The Dusk Test class needs to be updated to notify Dusk that it needs to use PhantomJS instead of Chrome. Take note of the port that we've used on the code above. Edit the file "test/DuskTestCase.php" on the functions as shown below:

    public static function prepare()
    {
        //static::startChromeDriver();
    }

    protected function driver()
    {
        return RemoteWebDriver::create(
            'http://localhost:9515', DesiredCapabilities::phantomjs()
        );
    }

References:




< go back to home