Running Acceptance Tests on PHP Projects With Codeception and PhantomJS

Running Acceptance Tests on PHP Projects With Codeception and PhantomJS

Last updated:

I'll be using composer to install the tools I'll use for this short tutorial. If you haven't started using it yet, you'll have to sooner or later so why not do it now? It'll be good for you, I promise. You'll be more productive and make more money.

This is the bare minimum you should have in composer.json:

{
    "require": {
        "codeception/codeception":"*"
    }
}  

After you've run php composer.phar install in the directory composer.json lives, you can start writing tests.

Bootstraping: creating test directories

From the current directory, locate directory vendor/bin/ (vendor/ is the directory where composer keeps downloaded dependencies; it should be on the same level as composer.json) and run codecept bootstrap to create needed files: Note: (replace /path/to/vendor/bin with your own values).

$ /path/to/vendor/bin/codecept bootstrap 

This will create codeception.yml and directory tests/.

Download and start PhantomJS

Go to this link and download PhantomJS for your platform. Extract the file and locate directory bin/. Once you find it, start phantomjs like this:

phantomjs/bin$ phantomjs --webdriver=4444
PhantomJS is launching GhostDriver...
[INFO  - 2014-04-16T18:08:37.104Z] GhostDriver - Main - running on port 4444

If close this console/terminal it'll stop running!

Setting up a browser emulator to run your tests

Edit file acceptance.suite.yml:

class_name: WebGuy
modules:
    enabled: 
      - Selenium2
      - WebHelper
    config:
        Selenium2:
            url: 'http://your-app-name'
            browser: phantomjs
            capabilities:
              webStorageEnabled: true

Your first actual test

  • Run the generator:

    $ path/to/vendor/bin/codecept generate:cept acceptance Welcome
    
  • Add this to file tests/acceptance/WelcomeCept.php:

    <?php
    $I = new WebGuy($scenario);
    $I->wantTo('run my first test');
    $I->amOnPage('/'); 
    $I->see('some text'); /* replace this with your own text */
    

    Running your test

  • cd to the directory where codeception.yml lives.

  • From this directory, locate directory vendor/bin/ (vendor/ is the directory where composer keeps downloaded dependencies; it should be on the same level as composer.json) and run codecept run to run your tests. If all goes well, you should see something like this:

    Codeception PHP Testing Framework v1.8.5
    Powered by PHPUnit 3.7.34 by Sebastian Bergmann.
    Unit Tests (0) ------------------------------
    ---------------------------------------------
    Acceptance Tests (1) ------------------------
    Trying to run my first test (WelcomeCept.php)        Ok
    ---------------------------------------------
    Functional Tests (0) ------------------------
    ---------------------------------------------
    Time: 4.97 seconds, Memory: 4.75Mb
    

Dialogue & Discussion