bencarter78@hotmail.com's avatar

Running Selenium against a test database

Hi there

I'm running Laravel 5.2 and testing with PHPUnit and Selenium. I tried to install the laracasts/integrated package but it seems that 5.2 isn't compatible so I ended up running phpunit-selenium.

I've set up my first test and when I've come to log in to my application it says it can't find the user which I have created in the test (and saved to the test database). It seems that when the browser loads to run the test it is trying to find my test user in the development database not the test database.

My phpunit.xml file has...

<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>

How can I get my test using the browser to use the test database?

0 likes
5 replies
ChristopherSFSD's avatar

In my TestCase class ...

public function createApplication()
{
    $app = require __DIR__ . '/../bootstrap/app.php';

    Dotenv::load(__DIR__ . '/../', '.env.testing');

    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    $this->faker = Faker\Factory::create();

    $this->baseUrl = 'http://' . env('APP_URL', $this->baseUrl);

    return $app;
}
ChristopherSFSD's avatar

That might not be enough now that I think about it. I use Selenium with Behat not PHPUnit. However, I also use port 8080 when I want to any interaction with the app to use the testing environment and thus the testing DB.

In bootstrap/app.php ...

if (array_get($_SERVER, 'SERVER_PORT') == '8080')
{
    $app->loadEnvironmentFrom('.env.testing');
}
bencarter78@hotmail.com's avatar

I think I'm fundamentally not understanding how selenium works.

In my test's setup my test (successfully) creates a user in the testing database however when Firefox opens and tries to log in with this user it says the credentials don't match because it is then using the development database. I have var_dump-ed the environment in the test which says 'testing' but when Firefox opens it then says 'local'.

How can I use the testing database when selenium opens Firefox? Or can't I? How are others testing with selenium and users?

ChristopherSFSD's avatar

I think it's because you're accessing the site with a normal browser and thus you need some way to differentiate those browser requests for testing. This is why I use port 8080 for testing. Port 80 is my normal DB, port 8080 is my testing DB.

That's where my previous post comes in. If the incoming request is on port 8080, then load up the testing environment which points to the testing DB.

MikeHopley's avatar

How can I use the testing database when selenium opens Firefox? Or can't I? How are others testing with selenium and users?

I thought this was answered in your other thread? Did you try my suggestion, and what happened?

To get Selenium to use a separate database, you need a separate environment and a way to trigger that. The solution by @ChristopherRaymond should work just as well as my solution (using different hostnames).

Please or to participate in this conversation.