Sunday, November 2, 2014

Creation Of Cest

Ever wanted a structure as classes for your Cepts!
Then Cest format should be your choice.
The biggest advantage is that it is very simple and compatibility with Cept scenarios.
So if your script/test is bit lengthy and you need to split it then you can approach the cest approach.

Just create a new cest file by running the command in console as :
$ php codecept.phar generate:cest suitename CestName

The generated file will look like this:

<?php
class Mytest
{
    public function _before(\AcceptanceTester $I)
    {
    }

    public function _after(\AcceptanceTester $I)
    {
    }

    // tests
    public function actualtest(\AcceptanceTester $I)
    {  
    }
}
?>

Here in the above file the _before and _after annotations are used for the setup and flow of test execution.
So _before will prepare the test set up before the test execution.
And _after will prepare the generation of falied reports,generation of reports etc.

We will pass the actor objects to actualtest method.

<?php
class Mytest
{
    // test
    public function myLogin(\AcceptanceTester $I)
    {
        $I->wantTo('log in to site');
        $I->amOnPage('/');
        $I->click('Login');
        $I->fillField('username', 'test');
        $I->fillField('password', 'password');
        $I->click('Login');
        $I->see('Home');
        $I->seeInCurrentUrl('/home');
    }
}
?>

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");
}

Monday, September 22, 2014

Codeception:At once how to run tests with WebDriver and PHPBrowser

we can  run tests with webdriver and PHPBrowser at once by setting the environment.
 
The configurations can be achieved by modifying acceptance suite.yml

The configuration can be defined as follows:

class_name: AcceptanceTester
modules:
    enabled:
        - PhpBrowser
        - AcceptanceHelper
    config:
        PhpBrowser:
            url: 'http://your app url'
env:
    browser:
        modules:
             enabled: [WebDriver, AcceptanceHelper]
             config:
                 WebDriver:
                     url: 'http://your app url'
                     browser: chrome

Sunday, September 14, 2014

Installation and Configuration of Codeception with Composer

Installation and Configuration of Codeception with Composer:

What is Composer: Basically composer is dependency manager for php.If you have ever worked with PHP you must have got the feeling that for some of the common tasks you need to look for the dependencies for various frameworks like zend, laravel or symphony etc.
This is where Composer comes into play.
It will manage all the dependencies required for different frameworks.
Composer will pull all the required stuffs(libraries,dependencies) and put them all in a common place.


1. Install composer to your projects root folder.
You can download the composer here.

2. Then we need to download all the codeception libraries and dependencies.

3. So create a new test folder say MyFirstTest and within this folder create a new json file called Composer.json.
Include the below lines od code within the Composer.json

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


4. Now navigate to the MyFirstTest test folderand fire the below command.

composer install

You will notice a new folder called 'vendor' is added within our test folder.

5. Now to initialize our testing environment we need to install bootstrap.
Simply navigate to the path Codecept/vendor/bin and fire the below command.

codecept bootstrap

6. A new folder called tests should be added under bin folder after bootstrap installation.

7. Navigate to tests folder and you will see all unit,functional and acceptance folder has been created to define our tests.

Now we are ready to define our first acceptance test.

I have already discussed in my previous post. Please find it here.
Powered by Blogger.