PHP Selenium RC data driven testing

In this post I will cover the procedure of data driven testing in selenium RC with phpunit framework.  PHP unit provide @dataProvider provider annotation.

In this post I have created a data driven test script in phpunit on Wikipedia where I put some country data in to excel file.

Country
India
Brazil
Canada
Shri Lanka
England

Read excel sheet data using library oleread.inc and “reader.php “download from link download  
With help of downloaded library I created below “'ExcelRead.php'” class to read country data.

<?php
require_once 'reader.php';
error_reporting(E_ALL ^ E_NOTICE);

class ExcelReader {

      public function getAllData(){
            $data = new Spreadsheet_Excel_Reader();
            $data->read('DataSheet.xls');
           
            $array = array();
            for ($j = 1; $j < $data->sheets[0]['numRows']; $j++)
            {                
                  $array[$j] = array($data->sheets[0]['cells'][$j+1][2], );                             
            }          
            return $array;
      }    
}
?>

First I included “reader.php” file in above class and “Data.xls” is my data sheet file name.

I have create a “TestScripts.php” phpunit script where created a “provider()” static function to fetch data from excel sheet with help of above created “ExcelRead” class.
By “@dataProvider provider” annotation applied on "testSearchCountry()" function it will automatically pass the array values of returned by “provider()” static function.

<?php
require_once 'Testing/Selenium.php';
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
require_once 'ExcelRead.php';

class TestScripts extends PHPUnit_Framework_TestCase
{    
    public static function provider()
    {
      $excelReader = new ExcelReader();
       return $excelReader->getAllTest();
    }
     
    public function setUp()
    {      
      $this->selenium = new Testing_Selenium("*firefox",   "http://en.wikipedia.org");
        $this->selenium->start();
            $this->selenium->windowMaximize();
    }
     
    /**
     * @dataProvider provider
    */     
    public function testSearchCountry($country)
    {
      $this->selenium->open("/wiki/Main_Page");
      $this->selenium->waitForPageToLoad("30000");
      $this->selenium->type("id=searchInput", $country);         
      $this->selenium->click("id=searchButton");
      $this->selenium->waitForPageToLoad("30000");
      $str = $this->selenium->getText("//h1[@id='firstHeading']/span");
      print($str . "\n");           
    }
     
  //Tear Down set up
   public function tearDown()
    {
       $this->selenium->stop();
    }
}
?>

When I run above test class, it will execute same number of time as data in excel sheet.  

No comments:

Post a Comment

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