如何将selenium webdriver phpunit测试结果导出到excel文件

时间:2017-03-27 10:33:09

标签: php selenium-webdriver phpunit

当然C:\ xampp \ htdocs \ testcases \ MySelenium> phpunit --verbose --log-tap tap.log LoginTest.php 通过此命令,我可以在文本文件中获得结果, 但我想通过使用phpunit框架

在excel文件中得到结果

1 个答案:

答案 0 :(得分:0)

// download phpexcel by composer and make file as below
<?php


/**
 * Description of General
 *
 * @author suchishah
 */
date_default_timezone_set('Asia/Kolkata');

class General {

    protected static $objPHPExcel;
    protected static $highestRow;
    protected static $filename;

    //put your code here
    public function createExcelReport11($fname) {
        /* @var $objPHPExcel type */
        self::$objPHPExcel = new PHPExcel();
        self::$filename = $fname;
// Set properties
        echo date('H:i:s') . " Set properties\n";
        self::$objPHPExcel->getProperties()->setCreator("Suchi Shah")
                ->setLastModifiedBy("Suchi Shah")
                ->setTitle("Test Case Report")
                ->setSubject("Excelsheet for testcase report")
                ->setDescription("Excel file contains testcase name,method name and all ")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");


        //Add some data
        echo date('H:i:s') . " Add some data\n";
        self::$objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', self::$filename)->mergeCells('A1:F1')
                ->setCellValue('A2', 'Action')
                ->setCellValue('B2', 'method')
                ->setCellValue('C2', 'DateTime')
                ->setCellValue('D2', 'Action pefromed')
                ->setCellValue('E2', 'Data Used')
                ->setCellValue('F2', 'Data Get')
                ->setCellValue('G2', 'Assertion')
                ->setCellValue('H2', 'Exe.Time(s)')
        ;
        foreach (range('A', 'F') as $columnID) {
            self::$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
                    ->setAutoSize(true);
        }

        echo date('H:i:s') . " Rename sheet\n";
        self::$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
        self::$objPHPExcel->setActiveSheetIndex(0);


// Save Excel 2007 file
        echo date('H:i:s') . " Write to Excel2007 format\n";
        $objWriter = PHPExcel_IOFactory::createWriter(self::$objPHPExcel, 'Excel2007');
        $objWriter->save(str_replace('.php', '.xlsx', __FILE__));


// Echo memory peak usage
        // echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";
// Echo done
        echo date('H:i:s') . " Done writing file.\r\n";
        $highestColumm = self::$objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
        echo '        ' . $highestColumm . '      highest colum';
        self::$highestRow = self::$objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
        echo self::$highestRow . '  rowh';
    }

    public function writeData($action, $method, $performedon, $dataused,$dataget,$assert, $interval) {
        self::$objPHPExcel->setActiveSheetIndex(0)
                ->setCellValueByColumnAndRow('0', self::$highestRow + 1, $action)
                ->setCellValueByColumnAndRow('1', self::$highestRow + 1, $method)
                ->setCellValueByColumnAndRow('2', self::$highestRow + 1, date('m/d/Y h:i:s a', time()))
                ->setCellValueByColumnAndRow('3', self::$highestRow + 1, $performedon)
                ->setCellValueByColumnAndRow('4', self::$highestRow + 1, $dataused)
                ->setCellValueByColumnAndRow('5', self::$highestRow + 1, $dataget)
                ->setCellValueByColumnAndRow('6', self::$highestRow + 1, $assert)
                ->setCellValueByColumnAndRow('7', self::$highestRow + 1, $interval);
// Call callback method
        self::$highestRow = self::$highestRow + 1;
        // echo self::$highestRow;
        $objWriter = PHPExcel_IOFactory::createWriter(self::$objPHPExcel, 'Excel2007');
        $a = 'Log' . self::$filename . date("Y-m-d") . '.xlsx';
        $objWriter->save($a);
    }
}

// include that file in the test case for which you want to generate report by 
// create its object
<?php

require_once '../vendor/phpexcel/phpexcel/Classes/PHPExcel.php';
require_once 'General.php';

class Sample extends PHPUnit_Extensions_Selenium2TestCase {

    private $data = array(
        'username' => 'me'
    );
    private static $highestRow;
    private static $lib_obj;

    public static function setUpBeforeClass() {
        self::shareSession(true);
        self::$lib_obj = new General();
        self::$lib_obj->createExcelReport11(__CLASS__);
    }

    // set url you want to visit

    public function testSetUserName() {
        $start = time();
        $this->byId('username')->value($this->data['username']);
        $end = time();
        $a = $end - $start;
        self::$lib_obj->writeData('setvalue', __FUNCTION__, 'username', $this->data['username'], '', '', $a);
    }

}