Skip to content

Acceptance Tests

JP Barbosa edited this page Mar 14, 2016 · 4 revisions

Acceptance Tests

Add WebDriver configurations
nano tests/acceptance.suite.yml
class_name: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: http://localhost:8000
            browser: 'firefox'
        - \Helper\Acceptance
Load Laravel5 modules for acceptance tests
nano tests/acceptance/_bootstrap.php
<?php
// Here you can initialize variables that will be available to your tests

require 'bootstrap/autoload.php';
$app = require 'bootstrap/app.php';
$app->loadEnvironmentFrom('.env.testing');
$app->instance('request', new \Illuminate\Http\Request);
Install Selenium server with brew
brew install selenium-server-standalone
Add helper for create author and article
nano tests/_support/AcceptanceTester.php
class AcceptanceTester extends \Codeception\Actor
{
    use _generated\AcceptanceTesterActions;

    public function haveAuthor()
    {
        return factory(App\Author::class)->create();
    }

    public function haveArticle()
    {
        return factory(App\Article::class)->create();
    }
}
Generate acceptance tests for articles
vendor/bin/codecept generate:cest acceptance Article
Tests create and edit an Article
nano tests/acceptance/ArticleCest.php
<?php
use \Faker\Factory;

class ArticleCest
{
    public function _before(AcceptanceTester $I)
    {
        Artisan::call('db:truncate');
        $I->amOnPage('/articles');
    }

    public function _after(AcceptanceTester $I)
    {
    }

    //tests
    public function createAnValidArticle(AcceptanceTester $I)
    {
        $attributes = [
            'title' => 'Article Title',
            'content' => 'The new text!'
        ];

        $this->createAnArticle($I, $attributes);

        $I->waitForText('Article was stored with success', 10, '#content .alert-info');
        $I->see($attributes['title'], 'a');
    }

    public function createAnInvalidArticle(AcceptanceTester $I)
    {
        $attributes = [
            'title' => '12',
            'content' => ''
        ];

        $this->createAnArticle($I, $attributes);

        $I->waitForText('The title must be at least 3 characters.', 10, '#modal .alert-danger');
        $I->amOnPage('/articles');
        $I->dontSee($attributes['title'], 'a');
    }

    public function editAnArticleWithValidAttributes(AcceptanceTester $I)
    {
        $attributes = [
            'title' => Factory::create()->sentence($nbWords = 6),
            'content' => 'The updated text!'
        ];

        $this->editAnArticle($I, $attributes);

        $I->waitForText('Article was updated with success', 10, '#content .alert-info');
        $I->see($attributes['title'], 'a');
    }

    public function editAnArticleWithInvalidAttributes(AcceptanceTester $I)
    {
        $attributes = [
            'title' => 'Invalid article',
            'content' => ''
        ];

        $this->editAnArticle($I, $attributes);

        $I->waitForText('The content field is required.', 10, '#content .alert-danger');
        $I->amOnPage('/articles');
        $I->dontSee($attributes['title'], 'a');
    }

    private function createAnArticle(AcceptanceTester $I, $attributes)
    {
        $I->haveAuthor();
        $I->click('New Article');
        $this->submitTheForm($I, $attributes);
    }

    private function editAnArticle(AcceptanceTester $I, $attributes)
    {
        $I->haveAuthor();
        $I->haveArticle();
        $I->amOnPage('/articles');
        $I->click('[href*=edit]');
        $this->submitTheForm($I, $attributes);
    }

    private function submitTheForm(AcceptanceTester $I, $attributes)
    {
        $I->waitForElement('#articles-form', 10);
        $I->fillField('#articles-form #title', $attributes['title']);
        $I->fillField('#articles-form #content', $attributes['content']);
        $I->click('#articles-form .btn-primary');
    }
}
Run redis server
redis-server
Run the server
php artisan serve --env=testing
Run Selenium server
selenium-server
Run Codeception
vendor/bin/codecept run acceptance
Codeception PHP Testing Framework v2.1.1
Powered by PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

Acceptance Tests (4) --------------------------------------------------------------------------------------------------------------
Create an valid article (ArticleCest::createAnValidArticle)                                                                  Ok
Create an invalid article (ArticleCest::createAnInvalidArticle)                                                              Ok
Edit an article with valid attributes (ArticleCest::editAnArticleWithValidAttributes)                                        Ok
Edit an article with invalid attributes (ArticleCest::editAnArticleWithInvalidAttributes)                                    Ok
-----------------------------------------------------------------------------------------------------------------------------------

Time: 11.43 seconds, Memory: 35.25Mb

OK (4 tests, 4 assertions)
Run all tests
vendor/bin/codecept run
...
OK (10 tests, 10 assertions)
Add acceptance tests with Selenium to Git
git add .
git commit -m "Add acceptance tests with Selenium"
Clone this wiki locally