bencarter78@hotmail.com's avatar

Setting the environment when running acceptance tests

Hi there

I am trying to figure out why my acceptance tests running using Selenium with Firefox always uses my development env file and settings (APP_ENV=local).

In my tests (Codeception) I have the following...

acceptance.suite.yml

class_name: AcceptanceTester
modules:
    enabled:
        - \Helper\Selenium
        - Db
        - WebDriver:
            browser: firefox
            url: http://localhost:8000

acceptance/_bootstrap.php

require 'bootstrap/autoload.php';
$app = require 'bootstrap/app.php';
$app->loadEnvironmentFrom('.env.selenium');
$app->instance('request', new \Illuminate\Http\Request);
$app->make('Illuminate\Contracts\Http\Kernel')->bootstrap();

.env.selenium

APP_ENV=selenium
APP_DEBUG=true
DB_CONNECTION=selenium
DB_HOST=192.168.10.10
DB_DATABASE=testing_db
DB_USERNAME=homestead
DB_PASSWORD=secret

When my test first runs I create a new user and attempt to log them in

$user = $I->createUser();
$I->amOnPage('/auth/login');
$I->fillField('email', $user->email);
$I->fillField('password', 'password');
$I->click('Sign in');

If I var_dump the User::all() immediately after creating the user then I can see it in the database, however when the test then attempts to log in the user then it says the credentials do not match.

This is because when Selenium runs, boots up Firefox it loads the .env file which uses my development database and therefore the user is never going to be found.

How can I change what .env file is used when Firefox runs the test?

0 likes
5 replies
MikeHopley's avatar

I don't think you can change the .env file from within Codeception's configuration. I think you must change it from Laravel itself.

You can change it in /bootstrap/app.php. Just before the last line (return $app;), add a check for your environment. Here's what mine looks like:

// Set browser tests to use separate database
if ( isset($_SERVER['HTTP_HOST'] ) && in_array( $_SERVER['HTTP_HOST'],
    ['testing.local', 'acceptance.badmintonbible.com'] ) )
{
    $app->loadEnvironmentFrom('.env.acceptance');
}

return $app;

As you can see, I am using the domain name (HTTP_HOST) to perform the check. The first one is for testing on my localhost. The second one is for testing on my remote server (which is a lot more fiddly to set up).

martinbean's avatar

@bencarter78 Is it not because Selenium accesses your website through a browser like a user would, therefore your application is booted as normal.

You may be create a new Application instance and loading .env.selenium in your bootstrap file, but that’s not the actual Application instance used to serve requests.

1 like
bencarter78@hotmail.com's avatar

@MikeHopley I was just reading it from the Codeception docs but have probably misinterpreted them...

You should not use this module (Laravel5) for acceptance tests. If you want to use Laravel functionality with your acceptance tests, for example to do test setup, you can initialize the Laravel functionality by adding the following lines of code to your suite _bootstrap.php file:

require 'bootstrap/autoload.php';
$app = require 'bootstrap/app.php';
$app->loadEnvironmentFrom('.env.testing');
$app->instance('request', new \Illuminate\Http\Request);
$app->make('Illuminate\Contracts\Http\Kernel')->bootstrap();

I've tried @MikeHopley's idea however the $_SERVER['HTTP_HOST'] variable isn't set. I tried with $_SERVER['argv'][2] == 'acceptance' (from the command line when I run the acceptance tests) however it's still loading the default .env file.

@martinbean That's exactly what I was thinking about using the browser but then I have no idea how you would test the system without hitting your development database. Maybe you can't have test users in these types of tests, but that doesn't sound like a Laravel way to do things.

I wonder what Jeff would do...

MikeHopley's avatar

the $_SERVER['HTTP_HOST'] variable isn't set.

You will need to create that host. The procedure might vary a bit depending on your operating system and local web server. I'm on Windows with XAMPP so I had to edit my hostsfile (Windows) and httpd-vhosts.conf (XAMPP Apache).

Once you have set it up, you should be able to open a browser and go to testing.local in the address bar. This should show you exactly the same site as you normally get on localhost. The only difference is that it will be using the different .env file.

Please or to participate in this conversation.