Text Post

Full screen web apps with Alfred and Google Chrome

There are some web applications I enjoy using, but when I use them in my main browser (Safari), they sometimes get a bit lost, surrounded with a multitude of other browser tabs.

A simple way to give these web apps a more “native” feel, is by launching them in a full screen window without window chrome.

This can be accomplished by using the Google Chrome browser in presentation mode. ( the keyboard shortcut is CMD+SHIFT+F )

If you wish to launch a web app in this presentation mode with a few key strokes, you can create a simple Alfred workflow.
From the workflow templates menu, choose the “keyword to applescript” option, define your keyword and adapt this applescript for the web app of your choice :

on alfred_script(q)
    tell application "Google Chrome"
            open location "http://asana.com"
            tell window 1 to enter presentation mode
    end tell
end alfred_script

You can also download two example workflows I made for Asana and Trello.

Text Post

Using Behat with Laravel Homestead

I would like to document the steps I took to set up the testing framework Behat in order to use it with the Laravel framework in a Homestead development environment.

Behat is a tool that will allow you to test your application by writing human-readable stories that describe the expected behaviour of your application.

Laravel Homestead is an official, pre-packaged Vagrant “box” that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine.

Making the two play together nice is not always easy, so I hope this post will help others facing similar problems.

Adding Behat to your Laravel application

I am using version 4.2 of the Laravel Framework and to add Behat to my application, I used the following instructions in my composer.yml file and ran the composer update command.

"behat/behat": "2.5.*@stable",
"behat/mink": "1.5.*@stable",
"behat/mink-extension": "*",
"behat/mink-goutte-driver": "*"

The next step is to add a behat.ymlfile to the root of the application with the following instructions.

default:
  paths:
    features: app/tests/acceptance
  extensions:
    Behat\MinkExtension\Extension:
      goutte: ~
      base_url: http://myapp.app

Now I can run the behat command without getting errors. This command is located at vendor/bin/behat, so I recommend adding vendor/bin to your PATH or setting up an alias.

Finally, I ran the behat --init command, this will create a features/ directory with some basic files to get started.

Using Homestead

I configured Laravel Homestead for my local development environment so that my local server configuration is similar to the production environment on Laravel Forge.

My Homestead.yaml file contains the following information :

folders:
    - map: /Users/gunther/Sites/myapp
      to: /home/vagrant/Sites/myapp

sites:
    - map: myapp.app
      to: /home/vagrant/Sites/myapp/public

For more details on Homestead, you can read the documentation.

The problem : testing authentification

I want to test my login page with the following Behat feature

Feature: Authentification
  In order to administer the website
  As the site owner
  I want to be able to login to the backend of the website

  Scenario: User connects to the backend
    Given I am on "/login"
    And I login with my credentials
    Then I should see "log out"

In order to test in a controlled environment, I wish to use a specific database for my Behat tests. To configure a different database for these tests, I will have to use a different environment in my laravel application.

However, if I want to test authentification I can not use the provided “testing” environment because the Laravel authentification filters are automatically disabled for the “testing environment”

So I had to set up a new environment in the Laravel application and I changed the server configuration so that the application uses this new environment when executing the Behat tests.

Configuring a new environment

I had to set up an “acceptance” environment in the Laravel application and configure Behat to use this environment.

In order to use the testing database for our acceptance test, all I had to do, is add an acceptance directory to the app/config directory with a new database.php file.

If I want Behat to use this environment, my first step is to change the FeatureContext.php file that can be found in the test/acceptance/bootstrap directory.

I had to add the following code to this file :

/**
 * @static
 * @beforeSuite
 */
public static function bootstrapLaravel()
{
    $unitTesting = true;
    $testEnvironment = 'acceptance';

    $app = require_once __DIR__ . '/../../../../bootstrap/start.php';
    $app->boot();

    Artisan::call('migrate:refresh');
    Artisan::call('db:seed');
} 

This code will run before the execution of a test suite and does the following things :

  • the environment is set “acceptance”
  • the Laravel framework is executed so that Laravel methods become available
  • the database structure is rebuild from scratch and test data is seeded to the database

Using the new environment in Homestead

The problem is, that when Behat executes my tests using the Homestead server (using http://myapp.app as a base URL), the application on Homestead does not know that it should use the acceptance environment.

The only way I found to resolve this problem is to set up a different URL for acceptance testing.

The first step is to change the nginx configuration on your homestead server. One can manually change the /etc/nginx/sites-available/myapp.app file and change the third line to :

server_name myapp.app myapp.test;

Or one can change the scripts/serve.sh script in the Homestead repository so that this happens automatically whenever one launches vagrant.

server_name $1 ${1/.app/.test};

If you are playing along, do not forget to add the myapp.test URL to your etc/hosts file.

The next step is to change the behat.yml file to indicate to Behat to use the new URL.

base_url: http://myapp.test

Finally, I had to change the environment detection code in the bootstrap/start.php file.

$current_url = parse_url($app['request']->url());
if ($current_url['host'] == 'myapp.test')
{
    $env = $app->detectEnvironment(function(){
        return "acceptance";
    });
} else {
    $env = $app->detectEnvironment(array(
        'local' => array('homestead'),
    ));
}

I used this code, but I would love to find a more elegant solution. If someone has a better idea, please contact me.

Now Behat uses the acceptance environment when running the tests against my application, hosted on Homestead.