如何在数据驱动测试中读取 excel 中的选定字段?

How to read selected fields from excel in data driven testing?

在我的网络应用程序中,我必须更新 25 个字段才能创建用户。 Excel 中的测试数据包含 100 个用户的列表,其中包含所有 25 个字段的必填信息。

使用 Robot 框架,测试脚本的编写方式是从 excel 中读取单行(单个用户信息)并更新 Web 应用程序中的所有相应字段。

如果我只想更新 10 个字段(或“n”个字段),我该如何在脚本中处理它?

注意:要更新的字段可能会根据测试用例发生变化。

据我了解你的情况,这对机器人来说不是一件容易的事。但是,有一种相当简单的方法可以通过使用通用测试脚本和特定于案例的 excel 文件来处理这个问题。

  1. 使用 FieldNameTabName 填充您的 excel 电子表格,以便能够在页面上找到输入。我的做法是:

  1. 使用 ExcelLibrary 将 TabName、FieldName 和 Value 检索到列表中(您似乎比我更熟悉它的使用,所以我将其留给您)。作为此步骤的结果,您将获得一个包含 3 个项目的列表,例如 ${SingleFieldOfSingleUser} # ['Tab1', 'Field1', 'aaa']

  2. 现在创建这些字段的列表:

        \  Insert Into List  ${SingleUser}  ${ColIndex}  ${SingleFieldOfSingleUser}
    
  3. 与其手动遍历每个可能的输入字段,不如使用通用自定义关键字:

        :FOR  ${UserAttribute}  IN  @{SingleUser}
        \  Input Attribute Into User Form  ${UserAttribute}
    
  4. 关键字可能如下所示:

    Input Attribute Into User Form
        [Arguments]  ${Attribute}
        # Try switching to the correct Tab. If already there, don't break upon error
        Run Keyword And Ignore Error  Click Element  ${Attribute[0]}
        # Input Value
        Input Text  ${Attribute[1]}  ${Attribute[2]}
    
  5. 赚了!您现在可以根据要更新的字段提供包含任意数量列的 Excel 电子表格。

这里给大家留下完整代码清单的模型:

*** Keywords ***

(...)

Read Column Data For User
    ${SingleUser}  Create List

    :FOR  ${ColIndex}  IN RANGE  ${COLCOUNT}
    ( ... Excel-related code here ... )
    \  ${SingleFieldOfSingleUser}  Create List  ${TabName}  ${FieldName}  ${Value}
    \  Insert Into List  ${SingleUser}  ${ColIndex}  ${SingleFieldOfSingleUser}

    :FOR  ${UserAttribute}  IN  @{SingleUser}
    \  Input Attribute Into User Form  ${UserAttribute}

    (...)

Input Attribute Into User Form
        [Arguments]  ${Attribute}
        Run Keyword And Ignore Error  Click Element  ${Attribute[0]}
        Input Text  ${Attribute[1]}  ${Attribute[2]}

P.秒。你真的不需要那么多 FOR 循环和列表来使这个东西工作,但我试图让我的解决方案尽可能接近你的原始编码模式,以防它有一些隐藏的个人价值。