示例#1
0
    def load_reference_data(
            self,
            alias_name,
            filename,
            main_column_key,
            custom_parser_module='ExcelDataDriver.ExcelParser.DefaultReferenceParserStrategy',
            custom_parser_class='DefaultReferenceParserStrategy'):
        """
        Load reference data with specific Parser

        Arguments:
        |  alias_name           |   alias_name for refer to the reference data |
        |  filename (string)    |   The file name string value that will be used to open the excel file to perform tests upon. |
        |  main_column_key      |   Identify unique key for use as reference when parse the data |
        |  custom_parser_module |   Test data parser module is ExcelDataDriver.ExcelParser.DefaultReferenceParserStrategy |
        |  custom_parser_class  |   Test data parser class is DefaultReferenceParserStrategy |
        """
        reference_wb = OpenpyxlHelper.load_excel_file(filename)
        CustomExcelParser = getattr(
            importlib.import_module(custom_parser_module), custom_parser_class)
        # CustomExcelParser = __import__(custom_parser_module)
        # parser_context = ParserContext(CustomExcelParser.CustomExcelBreakdownParser())
        parser_context = ParserContext(CustomExcelParser(main_column_key))
        references_data_sheets = parser_context.parse(reference_wb)
        reference_row_data = []
        for sheet_name in references_data_sheets:
            reference_row_data += references_data_sheets[sheet_name]
        self.reference_data[alias_name] = {
            'selected': None,
            'data': reference_row_data
        }
        reference_wb.close()
示例#2
0
    def merged_excel_report(self,
                            main_column_key,
                            data_type='DefaultParserStrategy'):
        """
        Merged all test data from report folder into summary_report.xlsx under summary_report_folder

        Arguments:
            | *data_type* | Test data type [ DefaultParserStrategy, CustomExcelParser ] default=DefaultParserStrategy |

        Example:
            | *Keywords*                        |  *Parameters*       |   *Parameters*      | *Parameters*      |
            | Merged Postcomrun Excel Report    |            	      |                     |                   |
            | Merged Postcomrun Excel Report    |  CustomExcelParser  |                     |                   |
        """
        print('Merged test report...')

        # - List all xlsx file under report_folder
        reports = list(
            glob.iglob(os.path.join(ExcelDataDriver.REPORT_PATH, '*.xlsx')))
        if len(reports) == 0:
            raise IOError('No report xlsx found under "' +
                          ExcelDataDriver.REPORT_PATH + '" folder')

        # - Read all test result
        print('-------------------------------------------------------')
        print('Initial ws test datas')
        summary_wb = OpenpyxlHelper.load_excel_file(reports[0],
                                                    data_only=False,
                                                    keep_vba=False)
        reports.pop(0)

        print('Init Parser')
        overall_test_status_is_pass = True
        summary_error_message = ''

        parser_strategy = DefaultParserStrategy(main_column_key)
        if data_type != 'DefaultParserStrategy':
            CustomExcelParser = self.load_module(data_type)
            parser_strategy = CustomExcelParser.CustomExcelParser()
        parser_context = ParserContext(parser_strategy)

        print('Parse wb')
        summary_wb_test_datas = parser_context.parse(summary_wb)
        for test_datas in summary_wb_test_datas.values():
            for test_data in test_datas:
                if test_data.is_fail():
                    overall_test_status_is_pass = False
                    summary_error_message += str(
                        test_data.get_log_message()) + '\r\n'

        for report in reports:
            print('Merged ws test datas : ' + report)
            wb = OpenpyxlHelper.load_excel_file(report,
                                                data_only=False,
                                                keep_vba=False)
            parser_context = ParserContext(parser_strategy)
            wb_test_datas = parser_context.parse(wb)

            for index_ws, ws_test_datas in enumerate(wb_test_datas.values()):
                for index_test_data, test_data in enumerate(ws_test_datas):
                    if test_data.is_not_run() is False:
                        list(list(summary_wb_test_datas.values())
                             [index_ws])[index_test_data].update_result(
                                 test_data.get_status(),
                                 test_data.get_log_message(),
                                 test_data.get_screenshot())
                        if test_data.is_fail():
                            overall_test_status_is_pass = False
                            summary_error_message += str(
                                test_data.get_log_message()) + '\r\n'
            wb.close()

        # Save the result to a new excel files.
        summary_file = os.path.join(ExcelDataDriver.REPORT_PATH,
                                    'summary_report.xlsx')
        summary_wb.save(summary_file)

        # - Return summary test result if have test failed
        if overall_test_status_is_pass is False:
            raise AssertionError(summary_error_message)
class ExcelTestDataService(object):

    # Static properties
    select_test_data = None
    parser_strategy = None

    def __init__(self):
        self.wb = None
        self.ws_test_datas = {}
        self.total_datas = []
        self.rerun_failed = True

    ####################################################
    #
    # Manage Excel Keywords
    #
    ####################################################
    def load_test_data(self, filename, custom_parser):
        """
        Load excel test data

        Arguments:
        |  filename (string)    |   The file name string value that will be used to open the excel file to perform tests upon. |
        |  data_type            |   Test data type [Default=DefaultParserStrategy]                                             |

        Examples:
        | *Keywords*           |  *Parameters*                                      |
        | Open Excel           |  C:\\Python27\\XLSXRobotTest\\XLSXRobotTest.xlsx   |

        """
        self.fileName = filename
        self.custom_parser = custom_parser

        self.ws_test_datas = {}
        self.total_datas = []
        self.wb = OpenpyxlHelper.load_excel_file(self.fileName)
        ExcelTestDataService.parser_strategy = custom_parser
        self.parser_context = ParserContext(
            ExcelTestDataService.parser_strategy)
        self.ws_test_datas = self.parser_context.parse(self.wb)

    def get_all_test_data(self,
                          rerun_only_failed=False,
                          offset_row=0,
                          maximum_row=None):
        """
        Get all test datas from current excel.

        Arguments:
            |  rerun_only_failed    |   Rerun only failed case default is False              |
            |  offset_row           |   Number of offset row. default is 0                   |
            |  maximum_row          |   Maximum row record. default is None mean no limit    |

        Examples:
            | *Keywords*           |  *Parameters*      |   *Parameters*      | *Parameters*      |
            | Get all test datas   |                    |                     |                   |
            | Get all test datas   |  ${True}           | ${10}               | ${10}             |
        """
        # Get all test data from worksheet
        if len(self.total_datas) > 0:
            return self.total_datas
        if offset_row is not None:
            offset_row = int(offset_row)
        if maximum_row is not None:
            maximum_row = int(maximum_row)
        for test_datas in self.ws_test_datas.values():
            self.total_datas = self.total_datas + test_datas
        # Filter if rerun_failed case only
        self.total_datas = list(
            filter(
                lambda test_data: test_data.is_pass() is False or
                rerun_only_failed is False, self.total_datas))
        # Cut the array offset and max
        self.total_datas = self.total_datas[offset_row:(
            maximum_row + offset_row if maximum_row is not None else None)]
        self.__clear_all_test_result()
        return self.total_datas

    ####################################################
    #
    # Default Test Data Keywords
    #
    ####################################################
    def select_validation_data(self, test_data):
        ExcelTestDataService.select_test_data = test_data

    def get_test_data_property(self, property_name):
        return ExcelTestDataService.select_test_data.get_test_data_property(
            property_name)

    def update_test_result(self, status, log_message=None, screenshot=None):
        ExcelTestDataService.select_test_data.update_result(
            status, log_message, screenshot)

    def get_test_result(self):
        return ExcelTestDataService.select_test_data.get_status()

    def get_test_log_message(self):
        return ExcelTestDataService.select_test_data.get_log_message()

    def get_test_screen_shot(self):
        return ExcelTestDataService.select_test_data.get_screenshot()

    def save_report(self, newfile=None):
        if newfile is None:
            newfile = self.fileName
        OpenpyxlHelper.save_excel_file(newfile, self.wb)
        self.wb = None
        self.ws_test_datas = {}
        ExcelTestDataService.select_test_data = None

    def __clear_all_test_result(self):
        """
        Clear test result for all rerun test cases
        :return:
        """
        print('Clear test result...')
        for test_data in self.total_datas:
            test_data.clear_test_result()