Road to Codeception setup and first test script for beginner

Codeception:  Codeception is PHP testing framework in BDD stype,It is easy to setup and use, not required any dependency except php. It has three sections for testing
  1. Acceptance Testing
  2. Functional Testing
  3. API testing 
Features:
  1. Can be integrated with selenium Webdriver
  2. Supported Symphony2, Laravel4, YII, Phalcon, Zend framework integration.
  3. BDD style data set.
  4. API testing Rest, Soap, XML-RPC
  5. Report in HTML, XML, JSON.
  6. Parallel execution
Setup:
  1. Download PHP from http://windows.php.net/download/
  2. Extract in PHP folder and save in your machine like I saved “D:\Program Files\PHP”
  3. Setup PHP path in your system environment variable.
  4. Open command prompt and check php version.

    5. Download Codecep.phar file from location http://codeception.com/builds as per your PHP version.
    6. Save this file into your created project root directory.
    7. Open command prompt and run below command.

   8. You should see that “tests” folder and “codeception.yml” file generated under project directory

Script Creation: 
   1. To create your first test just runs below command:
php codecept.phar generate:cept acceptance MyFirstTest

     You should see that test is generated under “tests\acceptance” directory.
   2. Open file “acceptance.suite.yml” and set your application url like below:
class_name: AcceptanceTester
modules:
    enabled:
        - PhpBrowser
        - AcceptanceHelper
    config:
        PhpBrowser:
            url: 'https://www.google.com'

   3. Write your acceptance code into file “tests\acceptance\MyfirstTestCept.php” in created file like I have          created google search
$I = new AcceptanceTester($scenario);
$I->wantTo('Google search testing keyword');
$I->fillField('q','testing');
$I->click('btnG');
?>    

Run Test: 
      Run below commands to execute test.
php codecept.phar run
      For detail steps output on console run command:
php codecept.phar run acceptance --steps
      To run only acceptance test:
php codecept.phar run acceptance 
      To run exactly one test 
php codecept.phar run acceptance MyfirstTestCept.php

Report:
To generate report in html format run command:
php codecept.phar run --html
You should see report is generated into “tests\_output” directory

Following are few exceptions what you will get during execution
Exception 1: if you get below error:
[Exception]
Codeception requires CURL extension installed to make tests run
If you are not sure, how to install CURL, please refer to StackOverflow

Solution: Open ini file and update line “;extension= php_curl.dll” to “extension=ext/php_curl.dll”

Exception 2:  If you are getting below exception:
PHP Fatal error:  Call to undefined function Codeception\mb_strpos() in phar://E
:/Automation-WorkArea/Codespection/Codecept/codecept.phar/src/Codeception/Config
uration.php on line 305

Fatal error: Call to undefined function Codeception\mb_strpos() in phar://E:/Aut
omation-WorkArea/Codespection/Codecept/codecept.phar/src/Codeception/Configurati
on.php on line 305

Solution: Open ini file and update line “;extension=php_mbstring.dll” to “extension=ext/php_mbstring.dll”

Exception 3: If you are getting exception for time zone:
GuzzleHttp\Exception\RequestException: strtotime(): It is not safe to rely on th
e system's timezone settings. You are *required* to use the date.timezone settin
g or the date_default_timezone_set() function. In case you used any of those met
hods and you are still getting this warning, you most likely misspelled the time
zone identifier. We selected the timezone 'UTC' for now, but please set date.tim
ezone to select your timezone.

Solution: Open ini file and set time zone like “date.timezone = "America/New_York"”

1 comment:

  1. Don´t word this test. Try:
    wantTo('Google search testing keyword');
    $I->amOnPage('/');
    $I->fillField('q','testing');
    $I->click('btnG');
    ?>

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution