Exemplo n.º 1
0
def main_template():
    from iparrot.parser import CaseParser
    from iparrot.modules.logger import logger, set_logger

    opt = ArgumentParser(usage=usage)
    opt.add_argument('-t',
                     '-T',
                     '--target',
                     '-o',
                     '-O',
                     '--output',
                     dest="target",
                     action="store",
                     default='ParrotProject',
                     help="target output path, 'ParrotProject' as default")

    opt.add_argument(
        '--log-level',
        dest="log_level",
        action="store",
        default="info",
        help="log level: debug, info, warn, error, info as default")
    opt.add_argument(
        '--log-mode',
        dest="log_mode",
        action="store",
        default=1,
        help="log mode: 1-on screen, 2-in log file, 3-1&2, 1 as default")
    opt.add_argument('--log-path',
                     dest="log_path",
                     action="store",
                     default=None,
                     help="log path: <project path> as default")
    opt.add_argument('--log-name',
                     dest="log_name",
                     action="store",
                     default="parrot.log",
                     help="log name: parrot.log as default")
    args = opt.parse_args()

    set_logger(mode=int(args.log_mode),
               level=args.log_level,
               path=args.log_path if args.log_path else args.target,
               name=args.log_name)
    parser = CaseParser()
    parser.auto_template(target=args.target)
Exemplo n.º 2
0
    @staticmethod
    def __if_exclude(url, exclude):
        if not exclude:
            return False
        if not isinstance(exclude, (list, tuple, set)):
            exclude = [
                exclude,
            ]
        for _ex in exclude:
            if format(_ex).strip() in url:
                return True
        return False


if __name__ == '__main__':
    set_logger(mode=1, level='debug')
    cp = CaseParser()
    cp.auto_template(target='../demo')
    cp.case_replace(
        suite_or_case='../demo/test_cases',
        target='../demoNew',
        rules=['xxxId=>abcedjlsje', 'apiX::ABCD=>HAHAHA', 'date=>${{today}}'])

    case = cp.source_to_case(source='../demo/parrot-demo.har',
                             target='../demo',
                             include=[],
                             exclude=['.js', '.css'],
                             validate_include=[],
                             validate_exclude=['timestamp', 'tag', 'token'],
                             auto_extract=False)
    print(json.dumps(case, ensure_ascii=False))
Exemplo n.º 3
0
            - ${{function(params)}} : exec function
            - else : exec code or print message
        :return:
        """
        if not actions:
            return
        for _action in actions:
            if isinstance(_action, dict):  # set variables
                self.__set_variables(variables=_action)
            elif re.search(re.compile(r"(\${{([^{}]+)}})"), _action):  # exec function
                # in __get_variables, ${{function(params)}} will be executed via eval()
                self.__get_variables(source=_action)
            else:  # exec code or print message
                try:
                    eval(_action)
                except SyntaxError:
                    print(_action)
                    continue

    def generate_report(self, output):
        report_file = "{}/parrot_{}.html".format(output, now_timestamp())
        make_dir(output)
        Report(stream=open(report_file, 'w', encoding='utf-8')).generate_report(result=self.report)
        logger.info("You could check the report: {}".format(report_file))


if __name__ == '__main__':
    set_logger(level='info')
    player = Player()
    player.run_cases(suite_or_case='../demo/test_cases')
Exemplo n.º 4
0
def main_playback():
    from iparrot.player import Player
    from iparrot.modules.logger import logger, set_logger

    opt = ArgumentParser(usage=usage)
    opt.add_argument('-s',
                     '-S',
                     '--suite',
                     '-c',
                     '-C',
                     '--case',
                     dest="suite_or_case",
                     action="store",
                     default=None,
                     help="test suite or case with path, *.yml or directory")
    opt.add_argument(
        '-t',
        '-T',
        '--target',
        '-o',
        '-O',
        '--output',
        dest="target",
        action="store",
        default='ParrotProject',
        help="output path for report and log, 'ParrotProject' as default")
    opt.add_argument(
        '-i',
        '-I',
        '--interval',
        dest="interval",
        action="store",
        default='ms',
        help=
        "interval time(ms) between each step, use the recorded interval as default"
    )
    opt.add_argument(
        '-env',
        '--environment',
        dest="environment",
        action="store",
        default=None,
        help="environment tag, defined in project/environments/*.yml")
    opt.add_argument('-reset',
                     '--reset-after-case',
                     dest="reset_after_case",
                     action="store_true",
                     default=False,
                     help="reset runtime environment or not, False as default")

    opt.add_argument(
        '--fail-stop',
        dest="fail_stop",
        action="store_true",
        default=False,
        help=
        "stop or not when a test step failed on validation, False as default")
    opt.add_argument(
        '--fail-retry-times',
        dest="fail_retry_times",
        action="store",
        default=0,
        help=
        "max retry times when a test step failed on validation, 0 as default")
    opt.add_argument(
        '--fail-retry-interval',
        dest="fail_retry_interval",
        action="store",
        default=100,
        help=
        "retry interval(ms) when a test step failed on validation, 100 as default"
    )

    opt.add_argument(
        '--log-level',
        dest="log_level",
        action="store",
        default="info",
        help="log level: debug, info, warn, error, info as default")
    opt.add_argument(
        '--log-mode',
        dest="log_mode",
        action="store",
        default=1,
        help="log mode: 1-on screen, 2-in log file, 3-1&2, 1 as default")
    opt.add_argument('--log-path',
                     dest="log_path",
                     action="store",
                     default=None,
                     help="log path: <project path> as default")
    opt.add_argument('--log-name',
                     dest="log_name",
                     action="store",
                     default="parrot.log",
                     help="log name: parrot.log as default")
    args = opt.parse_args()
    if not args.suite_or_case:
        print(playback_usage)
        opt.exit()

    set_logger(mode=int(args.log_mode),
               level=args.log_level,
               path=args.log_path if args.log_path else args.target,
               name=args.log_name)
    player = Player()
    player.run_cases(suite_or_case=args.suite_or_case,
                     environment=args.environment,
                     interval=args.interval,
                     reset_after_case=args.reset_after_case,
                     fail_stop=args.fail_stop,
                     retry_times=args.fail_retry_times,
                     retry_interval=args.fail_retry_interval,
                     output=args.target)
Exemplo n.º 5
0
def main_replace():
    from iparrot.parser import CaseParser
    from iparrot.modules.logger import logger, set_logger

    opt = ArgumentParser(usage=usage)
    opt.add_argument('-s',
                     '-S',
                     '--suite',
                     '-c',
                     '-C',
                     '--case',
                     dest="suite_or_case",
                     action="store",
                     default=None,
                     help="test suite or case with path, *.yml or directory")
    opt.add_argument('-t',
                     '-T',
                     '--target',
                     '-o',
                     '-O',
                     '--output',
                     dest="target",
                     action="store",
                     default='ParrotProjectNew',
                     help="target output path, 'ParrotProjectNew' as default")
    opt.add_argument(
        '-r',
        '-R',
        '--rule',
        '--rules',
        dest="rules",
        action="store",
        default=None,
        help=
        "replace rules, key=>value or value1=>value2 or apiA::key=>value, separated by ',' if multiple"
    )

    opt.add_argument(
        '--log-level',
        dest="log_level",
        action="store",
        default="info",
        help="log level: debug, info, warn, error, info as default")
    opt.add_argument(
        '--log-mode',
        dest="log_mode",
        action="store",
        default=1,
        help="log mode: 1-on screen, 2-in log file, 3-1&2, 1 as default")
    opt.add_argument('--log-path',
                     dest="log_path",
                     action="store",
                     default=None,
                     help="log path: <project path> as default")
    opt.add_argument('--log-name',
                     dest="log_name",
                     action="store",
                     default="parrot.log",
                     help="log name: parrot.log as default")
    args = opt.parse_args()
    if not args.suite_or_case or not args.rules:
        print(replace_usage)
        opt.exit()

    set_logger(mode=int(args.log_mode),
               level=args.log_level,
               path=args.log_path if args.log_path else args.target,
               name=args.log_name)
    parser = CaseParser()
    parser.case_replace(suite_or_case=args.suite_or_case,
                        target=args.target,
                        rules=args.rules.split(','))
Exemplo n.º 6
0
def main_record():
    from iparrot.parser import CaseParser
    from iparrot.modules.logger import logger, set_logger

    opt = ArgumentParser(usage=usage)
    opt.add_argument('-s',
                     '-S',
                     '--source',
                     dest="source",
                     action="store",
                     default=None,
                     help="source file with path, *.har or directory")
    opt.add_argument('-t',
                     '-T',
                     '--target',
                     '-o',
                     '-O',
                     '--output',
                     dest="target",
                     action="store",
                     default='ParrotProject',
                     help="target output path, 'ParrotProject' as default")
    opt.add_argument(
        '-i',
        '-I',
        '--include',
        dest="include",
        action="store",
        default=None,
        help="include filter on url, separated by ',' if multiple")
    opt.add_argument(
        '-e',
        '-E',
        '--exclude',
        dest="exclude",
        action="store",
        default=None,
        help="exclude filter on url, separated by ',' if multiple")
    opt.add_argument(
        '-vi',
        '-VI',
        '--validation-include',
        dest="v_include",
        action="store",
        default=None,
        help=
        "include filter on response validation, separated by ',' if multiple")
    opt.add_argument(
        '-ve',
        '-VE',
        '--validation-exclude',
        dest="v_exclude",
        action="store",
        default=None,
        help=
        "exclude filter on response validation, separated by ',' if multiple")
    opt.add_argument(
        '-ae',
        '-AE',
        '--auto-extract',
        dest="auto_extract",
        action="store_true",
        default=False,
        help="automatic identification of interface dependencies or not")

    opt.add_argument(
        '--log-level',
        dest="log_level",
        action="store",
        default="info",
        help="log level: debug, info, warn, error, info as default")
    opt.add_argument(
        '--log-mode',
        dest="log_mode",
        action="store",
        default=1,
        help="log mode: 1-on screen, 2-in log file, 3-1&2, 1 as default")
    opt.add_argument('--log-path',
                     dest="log_path",
                     action="store",
                     default=None,
                     help="log path: <project path> as default")
    opt.add_argument('--log-name',
                     dest="log_name",
                     action="store",
                     default="parrot.log",
                     help="log name: parrot.log as default")
    args = opt.parse_args()
    if not args.source:
        print(record_usage)
        opt.exit()

    set_logger(mode=int(args.log_mode),
               level=args.log_level,
               path=args.log_path if args.log_path else args.target,
               name=args.log_name)
    parser = CaseParser()
    parser.source_to_case(
        source=args.source,
        target=args.target,
        include=args.include.split(',') if args.include else [],
        exclude=args.exclude.split(',') if args.exclude else [],
        validate_include=args.v_include.split(',') if args.v_include else [],
        validate_exclude=args.v_exclude.split(',') if args.v_exclude else [],
        auto_extract=args.auto_extract)