Sunday, October 26, 2014

Codeception Helpers

Codeception has always the flexibilty to add the add the custom actions to the test suite.
It just doesn't restrict to add custom actions.
'bootstrap' is the command to that codeception generates these modules.
These modules actually called the Helpers.
These Helper files are always created in tests/support.

So here every action defined is a public function.
So we need to run a build 'build' command.
Once after running build command you will see the function is getting added to AcceptanceTester class.

Lets take an example of login helper class.

/*
    * Login to My Site
    *  
    * Example: -
    *  
    * $I->MyLogin(&I, 'test', 'testuser', 'testpassword');
    *  
    *  
    * @param  String $customername: Name of the Customer
    * @param  String $userName    : User Name
    * @param  String $password    : Password
    *  
    */

public function MyLogin($I, $username, $password){

$I->amOnPage('/');
        $I->click('Login');
$I->fillField('signin_username',$username);
$I->fillField('signin_password',$password);
$I->click('signin_signin');
$I->seeInCurrentUrl("home");
$I->see("My Home Page");

So if you ever need to use the login multiple time in your script you don't need to write these steps.You just need to call the helper function.

Sunday, October 19, 2014

Codeception Modules

In Codception various modules are comes as packages initially.
Some of them are Webdriver,symphony,Laravel etc.
Modules allow you to choose the actions and assertions that can be performed in tests.
All actions and assertions that can be performed by the Tester object in a class are defined in modules.

So you might be thinking that you are limited with the modules coming with Codeception installation.
But that's not true.
You can create custom modules by your own and use them in your tests.


So lets see at the below example:


<?php
$I = AcceptanceTester($scenario);
$I->amOnPage('/');
$I->see('Home');
$I->seeInDatabase('customers', array('id' => 1));
$I->seeFileFound('locked');
?>



So you can see the above script actually using different modules to wor,.

1. It uses PhpBrowser to load the web page.
2. It uses Db to see into the database.
3. It uses Filesystem to check the status of the file.


Modules are attached to Actor classes in the suite config.

In tests/acceptance.suite.yml we can see

class_name: AcceptanceTester
modules:
    enabled: [PhpBrowser, Db, Filesystem]


Generally Codeception is bundled with many modules as a package.
These are called stendard modules.
Some of the standard modules are Webdriver,symphony,Laravel etc.

Monday, October 13, 2014

Codeception:Variables for UI Elements

As we know we can write complex tests using codeception.Think of a scenario of a login page.
We have generally 3 fields in login page.

1.User id fiels
2.Password field
3.Sign in button.

We have created a test using codeception to login and that worked fine.
But think in future if we have to login to the same website multiple times using different user ids and passwords and the credentials are changing frequently.
So if we want a solid automation script, we need to consider the use of variables in the script.


let see the following example of a login page with different credentials:


<?php

/**
 * @var AcceptanceTester
 */

$I = new AcceptanceTester($scenario);

/************************
 * Cept Data Declaration
 * **********************/

//User Credential

$FirstUname='FirstUser';
$FirstPwd='FirstPassword';
$SecondUname='SecondUser';
$SecondPwd='SecondPassword';


/**
 
 * Login to the page using First User's credential

 */

$I->click('Login');
$I->fillField('signin_username', $FirstUname);
$I->fillField('signin_password',$FirstPwd);
$I->click('signin_signin');

//Signout from page for the current user and relogin using a different user

$I->click('Login');
$I->fillField('signin_username', $SecondUname);
$I->fillField('signin_password',$SecondPwd);
$I->click('signin_signin');


In this way we can login to same page using different credentials and we can change the variables declared at top of script if required in future.

Friday, October 10, 2014

Repopulation of database using mysql instead of db module


What is DB module:

The DB module is used to cleaning database before each test. So this module was added into codeception.yml. To have your database properly cleaned you should configure it to access the database. Also provides actions to perform checks in database.

The cofig can be done in below formats:


modules:
   enabled: [Db]
   config:
      Db:
         dsn: 'mysql:host=localhost;dbname=test1'
         user: 'root'
         password: ''
         dump: 'tests/_data/dump.sql'
         populate: true
         cleanup: false

So config for db module is done.
Now comes to the real work scenarios.


You must have experienced that Db module is quite slow when loading large database dumps and it is also not a good idea when dealing with  complex SQLs.

So i think it's an  good idea to leave tests/_data/dump.sql to be empty file and leave cleanup option of Db module enabled, so database will be cleaned up before each test, yet dump not to be loaded. We will use realdump.sql file to populate database.

To load dump before each test you can use AcceptanceHelper or any other Helper class you have. Probably you will need to create your own helper with codecept g:helper DbHelper command.

In its before method you can load db dump (using mysql in this example)

<?php
public function _before(\Codeception\TestCase $test)
{
    exec("mysql -u user -ppassword test1 < tests/_data/real_dump.sql");
}

Powered by Blogger.