一直很好奇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
entry point for executing tests.robot.rebot
entry point for post-processing outputs (Rebot).robot.libdoc
entry point for Libdoc tool.robot.testdoc
entry point for Testdoc tool.robot.tidy
entry point for Tidy tool
下一章,我接着來分析執行測試的入口點robot.run.
如果喜歡作者的文章,請關注"寫代碼的猿"訂閱號以便第一時間獲得最新內容。本文版權歸作者所有,歡迎轉載.