Search This Blog

Wednesday, September 17, 2014

Codeception - Modules & Helpers

Modules

  1. Modules play an important role in Codeception framework and helps :  
    • The target user by giving a key to define their own categorize their modules/functionality
    • Defining Folder Structure
    • Specifying module specific helper method  
  2. Modules are php file which is always stored under <Project Home> : <Name of module.php> eg : CC_90_ActionsHelper.php
  3. Modules can be turned on/off from acceptance.suite.yml
     class_name: AcceptanceTester  
     modules:  
       enabled:   
         - WebDriver  
         - AcceptanceHelper  
         - CC_90_ActionsHelper  
       config:  
        WebDriver:  
          url: 'xxx'  
          browser: chrome  
          window_size: 1124x850  
          wait: 10  
          delay: 2  
          capabilities:  
            unexpectedAlertBehaviour: 'accept'  
    
  4. Modules are always inherited from \Codeception\Module class, hence structure looks like below:
     <?php  
     class CC_90_ActionsHelper extends \Codeception\Module{  
      ........  
     }  
    

Helpers

  1. Helpers are custom actions which are created to reuse them multiple of times in the test
  2. Helpers are always created in modules (/support
      /*  
        * Login to customercare  
        *   
        * Example: -  
        *   
        * $I->loginToCC(&I, 'AlliedBuilding', 'btbt', btbt);  
        *   
        *   
        * @param  String $customername  : Name of the Customer  
        * @param  String $userName    : User Name  
        * @param  String $password    : Password  
        *   
        */  
       public function loginToCC($I, $custName, $userName, $password){  
         $I->fillField(['xpath' => Locators::$cc_custName], $custName);  
         $I->fillField(['xpath' => Locators::$cc_userName], $userName);  
         $I->fillField(['xpath' => Locators::$cc_password], $password);  
         $I->click(['xpath' => Locators::$cc_signIn]);  
         return $this;  
       }  
    
  3. Build command is used to add the helper method to acceptancetester.php  
    Command : [php codecept.phar build]
  4. Once method added, it can be used in the tests as usual.

Tuesday, September 16, 2014

Headless Testing [Codeception+Phantomjs]

Installation

  • phantomjs can be downloaded from http://phantomjs.org/
  • Once downloaded,unzip it and update the path variable under 'Env Variables > user variables' to path of 'phantomjs.exe'. Eg : C:\Download\phantomjs-1.9.7-windows\phantomjs-1.9.7-windows










  • Check the installation from a command prompt using:
     phantomjs –version  








  • Start the ghost driver , it needs to run on the port on which selenium server is running using this command :
  •  phantomjs --webdriver=4444  
    






Configurations needed in Codeception

  • Change 'browser' parameter inside 'acceptance.suite.yml.template' to specify 'phantomjs'

Run Tests

  • Tests can be run as usual using :
     php codecept.php run <cept structure> --steps  
    
Note: When applications are browse via 'https' , Please include phantomjs commandline option of turning the ignore ssl errors ON. Use below command : 
 phantomjs –ignore-ssl-errors=true –webdriver=4444  

Xpath Formation using preceding-sibling,following-sibling & descendant

'descendant' attribute is used to select the first element on a page always

 <ul>  
   <li> Buy</li>  
   <li> Buy using Credit Card</li>  
   <li> Buy using Debit Card</li>  
   <li> Buy using Cash On Delivery </li>  
 </ul>  
  • Select the very first `li`
     /descendant::li[contains(text(),'Buy')][1]  
    
  • Select the preceding Sibling of the a node
  •  /descendant::li[contains(text(),'Buy')][2]/preceding-sibling::li  
    
    Selection
     <li> Buy</li>  
    
  • Select the following Sibling of a node
  •   /descendant::li[contains(text(),'Buy')][3]/following-sibling::li  
    
    Selection
     <li> Buy using Cash On Delivery </li>