zoukankan      html  css  js  c++  java
  • Robot Framework 源码解析(1)

    一直很好奇Robot Framework 是如何通过关键字驱动进行测试的,好奇它是如何支持那么多库的,好奇它是如何完成截图的。所以就打算研究一下它的源码。

    这是官方给出的Robot framework模块化结构:

    它的用法暂且不说,网上有很多. 这是我的一个实例。“打开浏览器,baidu搜索一个字符串。”

    Robot Framework有很多入口点,比如

    1. java -jar robotframework.jar test.robot

    2. robot path/to/tests.robot

    3. robot --include tag1 --include tag2 --splitlog tests.robot

    4. robot --name Example --log NONE t1.robot t2.robot > stdout.txt

    因为对java相对比较熟悉一些,所以就从java的入口的点org.robotframework.RobotFramework开始看起。

    java -jar robotframework.jar mytests.robot
    java -jar robotframework.jar run mytests.robot
    java -jar robotframework.jar rebot --log mylog.html out.xml
    java -jar robotframework.jar tidy --format robot mytests.html

     1 package org.robotframework;
     2 
     3 /**
     4  *
     5  * Entry point for using Robot Framework from Java programs.
     6  *
     7  */
     8 public class RobotFramework {
     9 
    10     public static void main(String[] args) {
    11         int rc = run(args);
    12         System.exit(rc);
    13     }
    14 
    15     public static int run(String[] args) {
    16         try (RobotRunner runner = new RobotRunner()) {
    17             return runner.run(args);
    18         }
    19     }
    20 }

     main方法,是程序的入口,命令行传入的参数传递给main方法。通过  int rc = run(args); 将命令后参数传递给run方法。

    接下来看run方法是做什么的。

    在这里创建了一个RobotRunner的实例,调用该实例的run方法,并且将命令行参数传递给了RobotRunner的run方法。

    继续看RobotRunner都做了些什么。

    构造函数

    1 public RobotRunner() {
    2       interpreter = new PythonInterpreter();
    3       runner = createRunner();
    4 }

    run方法:

    1     public int run(String[] args) {
    2         return runner.run(args);
    3     }

    RobotRunner 内部创建一个Jython解释器PythonInterpreter对象和 robot.JarRunner 来运行Robot测试。另外RobotRunner 是一个AutoCloseable接口的实现类,可以在try-cache块中自动的关闭资源,以清理解释器。

    RobotRunner 的run方法中可以看到,它是通过RobotPythonRunner的run方法来执行Robot测试的

    接下来看 RobotPythonRunner是如何执行Robot测试的:

    可是RobotPythonRunner是一个接口!只能去找它的实现类了。src/robot/jarrunner.py 是RobotPythonRunner的实现类。

     1 class JarRunner(RobotPythonRunner):
     2     """Used for Java-Jython interop when RF is executed from .jar file."""
     3     _commands = {'run': run_cli, 'rebot': rebot_cli, 'tidy': tidy_cli,
     4                  'libdoc': libdoc_cli, 'testdoc': testdoc_cli}
     5 
     6     def run(self, args):
     7         try:
     8             self._run(args)
     9         except SystemExit as err:
    10             return err.code
    11 
    12     def _run(self, args):
    13         if not args or args[0] in ('-h', '--help'):
    14             print(USAGE)
    15             raise SystemExit(INFO_PRINTED)
    16         command, args = self._parse_command_line(args)
    17         command(args)  # Always calls sys.exit()
    18 
    19     def _parse_command_line(self, args):
    20         if args[0] in self._commands:
    21             return self._commands[args[0]], args[1:]
    22         return run_cli, args

    这个JarRunner其实也没有做什么, 除了解析main(String[] args)方法的参数(第16行),也就是命令行的参数,并且根据参数判断调用哪个方法(第17行)。

    例如:java -jar robotframework.jar run mytests.robot这个命令,经过JarRunner解析会最终调用run_cli(mytests.robot)这个方法。

              java -jar robotframework.jar rebot --log mylog.html out.xml这个命令,经过JarRunner解析会最终调用rebot_cli(--log,mylog.html,out.xml)这个方法。

    java的命令行入口其实最终还是转到了其它入口点:

    下一章,我接着来分析执行测试的入口点robot.run.

    如果喜欢作者的文章,请关注"写代码的猿"订阅号以便第一时间获得最新内容。本文版权归作者所有,欢迎转载. 

  • 相关阅读:
    springboot笔记(五章)整合持久层技术
    Java基础复习(一)
    springboot复习
    JUC(2)
    JUC(1)
    springboot笔记(一~四章)入门、基础配置、整合视图技术、整合Web开发
    JVM复习
    NIO(New IO)
    hive 函数
    hive基础知识
  • 原文地址:https://www.cnblogs.com/yuan1225/p/10642348.html
Copyright © 2011-2022 走看看