Codeception Testing with Yii Framework 2.0 [Day 14]

Today’s Objective(s)

  1. Get acceptance test to pull url without having to hardcode (ie. use a function instead of ‘index.php?r=site/index’)
  2. Write an acceptance test for functionality that uses a session.
  3. Research best practices for acceptance/functional testing in Yii

Introduction

In yesterday’s acceptance test I ended up using the code:

$I->amOnPage('index.php?r=say/index');

Now even though it works, it’s a bit iffy to me. The reason for that is, what happens when I want to use pretty urls (ie. localhost/index.php?r=say/index becomes localhost/say)? I might end up having to rewrite my test, which in the current scenario would just mean changing one line of code. But if I had a complete website with over 50 acceptance tests, I wouldn’t want to manually go to each of those tests and change the page url…. I’m lazy like that ;)

The solution

There is a Class BasePage that we can extend to simulate the page we want to display and we specify our route as a variable for use in an inherited static function. Let’s get cracking.

Made ~\tests\codeception\_pages\HelloPage.php:

<?php

namespace tests\codeception\_pages;

use yii\codeception\BasePage;

/**
 * Represents hello page
 * @property \AcceptanceTester|\FunctionalTester $actor
 */
class HelloPage extends BasePage
{
    public $route = 'say/index';
}

And now update HelloCept.php :

<?php
/* @var $scenario Codeception\Scenario */
use tests\codeception\_pages\HelloPage;

$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that say page displays "Hello"');
HelloPage::openBy($I);
$I->see('Hello');

And voila! The acceptance tests works again, without having to hard code the URL. Delving further into the static function openBy for BasePage has shown that it could be replaced with the following:

$I->amOnPage(Yii::$app->getUrlManager()->createUrl(['say/index']));

But that doesn’t really read as well, and another benefit to the new page class that we have, we can define other functions to improve inserting data into forms and other items we might want to simplify.

Conclusion

This was a rather piss poor post, didn’t really have my head in it…. But we did cover creating a faux-page to use for better implementing complicated functions into our acceptance test. So progress has at least been made! :D

Looking forward to implementing sessions into this acceptance test.

 

Upcoming Objective(s)

  1. Write an acceptance test for functionality that uses a session.
  2. Research best practices for acceptance/functional testing in Yii

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>