[robot framework] 脚本解析模块

来源:互联网 发布:word mac破解版 编辑:程序博客网 时间:2024/06/10 19:23

负责加载测试脚本文件,模块代码路径./src/robot/parsing/

一、解析逻辑

我们以keyword_driven.robot测试脚本为例,入口点是./src/robot/parsing/model.py的TestData()函数,分析其中的解析过程。

1.1 表格解析器

解析模块

  1. TestCaseFile构造函数中会实例化四个表格对象:设置表格、变量表格、测试用例表格、用户关键字表格,并在populate()函数中调用FromFilePopoulator(self).populate(self.source)
  2. 打开脚本文件,并从路径中获取后缀名,再到READERS字典获取到对应的文件格式读取器;
  3. TxtReader继承了TsvReader,读取出文件中的每一行;
  4. TxtReader根据定义的格式将一行字符划分成列(cells),其分隔符为两个既以上的空格,“|”也是分隔符;
  5. DataRow会区分cells中的注释部分和与非注释部分;
  6. TsvReader中判断cell[0]是否为星号“*”字符开始,以此解决用解析到时的关键字去获取相应的表格对象;
  7. 通过表格对象type属性查找出对应的解析器,赋值给FromFilePopulator._populator,作为当前表格解析器;
  8. 如果cell[0]不是星号“*”字符开始,则调用当前表格解析器的add()方法。

1.2 表格内解析器

解析模块--表格内

  1. 文件格式读取器解析出每一行的每一列;
  2. DataRow判断第一列不为连续行标识“…”,便用名称从ResourceFileSettingTable找到Documentation相应对象;
  3. Documentation对象创建的表格内解析器DocumentationPopulator
  4. 本行后续列调用DocumentationPopulator.add()方法添加;
  5. 若第一列为连续行标识,则继续使用上一次的解析器,调用add()方法,直到不是连续行标识为止;
  6. 解析到设置项Library,同样从ResourceFileSettingTable找到相应对象ImportList
  7. 创建相应的表格内解析器SettingPopulator,替换成为当前解析器;
  8. 同时,调用原解析器对应的设置项对象的Documentation.populate()方法,将其解析器收集到的所有列合并成为Documentation对象的value。

此时,完整的表格内设置项的内容读取完成,其中的设置项类似。

二、组织结构

脚本文件会生成TestCaseFile对象,根据脚本中各区域形成四个表对象,顶级解析器是FromFilePopulator,包含了对应的四个解析器,见下表

区域块 表对象 解析器 Setting | Settings TestCaseFileSettingTable SettingTablePopulator Variable | Variables VariableTable VariableTablePopulator Test Case | Test Cases TestCaseTable TestTablePopulator Keyword | Keywords KeywordTable KeywordTablePopulator

2.1、设置表

关键字 对象 解析器 Documentation Documentation DocumentationPopulator Library Library SettingPopulator Resource Resource SettingPopulator Variables Variables SettingPopulator Metadata MetadataList MetadataPopulator Suite Setup Fixture SettingPopulator Suite Teardown Fixture SettingPopulator Force Tags Tags SettingPopulator Default Tags Tags SettingPopulator Test Setup Fixture SettingPopulator Test Teardown Fixture SettingPopulator Test Template Template SettingPopulator Test Timeout Timeout SettingPopulator

2.2、变量表

表对象为VariableTable,解析器为VariableTablePopulator,表内的解析器VariablePopulator将脚本中的每个变量解析成Variable对象添加到VariableTable的variables列表。这里对$变量、@列表、&字典类型并无区别处理。

2.3、测试用例表

表对象为TestCaseTable,解析器为TestTablePopulator,表内的解析器TestCasePopulator将脚本中的每个测试用例解析成TestCase对象添加到TestCaseTable的tests列表。其中测试用例中的每个action会组合成数据一块放入同一个TestCase对象。表内解析器会调用TestCasePopulator

  • 根据“[]”字符选择不同的关键字解析器,如下表,复用了设置表中的类
  • 根据“:FOR”字符会选择ForLoopPopulator解析器,解析循环语句,在TestCase对象的steps列表中添加ForLoop对象:拆分为变量、FOR和集合部分,FOR后续语句需要用”\“字符使DataRow对象解析成缩进
  • 否则使用StepPopulator解析器,解析后续语句为Step对象添加到steps列表:拆分为赋值变量列表、关键字和参数列表,解析过程:如果第一列是变量(除掉赋值符)则把该列添加到赋值变量列表里,并弹出该列后迭代判断,接下来的一列为关键字,后面的放入参数列表。
关键字 对象 解析器 [Documentation] Documentation DocumentationPopulator [Template] Template SettingPopulator [Tags] Tags SettingPopulator [Setup] Fixture SettingPopulator [Teardown] Fixture SettingPopulator [Timeout] Timeout SettingPopulator

2.4、关键字表

表对象为KeywordTable,解析器为KeywordTablePopulator,表内的解析器UserKeywordPopulator将脚本中的每个测试用例解析成UserKeyword对象添加到KeywordTable的keywords列表。表内解析器调用UserKeywordPopulator,与测试用例逻辑一样,因为都继承于_TestCaseUserKeywordPopulator,支持“[]”、“:FOR”、普通语句,其中支持的关键字如下表

关键字 对象 解析器 [Documentation] Documentation DocumentationPopulator [Arguments] Arguments SettingPopulator [Return] Return SettingPopulator [Timeout] Timeout SettingPopulator [Teardown] Fixture SettingPopulator [Tags] Tags SettingPopulator
原创粉丝点击