首页 > 解决方案 > 如果在机器人框架上如何使用 run 关键字

问题描述

我正在编写关于机器人框架的代码,它是关于比较 excel 文件和 mysql 数据库之间的数据。在我的代码中,我可以成功比较数据,但如果存在不匹配,我也想知道在哪一列。如果我需要更详细地解释,我有 66 列并且我知道它们的名称,因为它是用 excel 文档编写的,让我们认为excel和数据库之间有2个不匹配,我写的代码给了我错误的值,比如

1) Lists are different:
Index 0: 905390000510 != 905390000511 
2) Lists are different:
Index 0: 88975322100222 != 88975322100332

但是每次都写Index 0,所以我不知道错误在哪里。因此,我需要在任何地方将 if 子句添加到我的代码中,但我还没有实现它,因为当我想使用RUN KEYWORDIF 语句时,我得到了所有列名,但我只需要得到那个 2。我该如何解决它?我在下面添加我的代码。

*** Test Cases ***
open first excel file

    connect to database  pymysql  testdb  root  rootpass  localhost  3306
    :FOR    ${i}    IN RANGE    2  67
    \   open excel document  ${path1}  ${i}
    \   ${cname}  read excel cell  1  ${i}  Sheet2
    \   ${value1}=  read excel cell  2  ${i}  Sheet2
    \   @{list1}  create list  ${value1}
    \   @{queryResults}=  query  select a_${i} from test_table where a_1 = 'filename1'
    \   run keyword and continue on failure  lists should be equal  ${list1}  @{queryResults}
    \   run keyword if  "${list1}" !=" ${queryResults}"  log to console  ${cname}

我的excel文件在第一行包含列名,在第二行有数据。

标签: robotframework

解决方案


尽管您的脚本包含一个列表比较,但当仔细观察时,在我看来这不是一个列表比较,而是一个值比较,它应该会产生如下消息

在行“1”列“BBBB”值 2222 != 4444。

下面是一个示例,其中比较了两个列表的值,并在生成错误消息时从第三个列表中检索列名:

*** Settings ***
Library    Collections

*** Test Cases ***
Compare List
    ${ListColumns}    Create List    AAAA    BBBB    CCCC     
    ${ListA}          Create List    1111    2222    3333
    ${ListB}          Create List    1111    4444    3333

    FOR    ${i}    IN RANGE    0    2
        run keyword and continue on failure  
        ...       should be equal as Strings      
        ...            @{ListA}[${i}]      
        ...            @{ListB}[${i}]
        ...            values=False
        ...            msg=In Row "${1}" Column "${ListColumns}[${i}]" value @{ListA}[${i}] != @{ListB}[${i}].
    END

此脚本将导致此控制台输出:

==============================================================================
Compare List                                                          | FAIL |
In Row "1" Column "BBBB" value 2222 != 4444.
------------------------------------------------------------------------------
SO004.Compare                                                         | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
SO004                                                                 | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================

考虑到这一点,您可以以这种方式将其应用于您的脚本。请记住,以下代码未经测试,但应该可以工作。

Open First Excel File

    Connect To Database  pymysql  testdb  root  rootpass  localhost  3306

    FOR    ${i}    IN RANGE    2    67
        # Get the value and column
        open excel document  ${path1}  ${i}
        ${ColumnName}      read excel cell  1  ${i}  Sheet2
        ${Excelvalue}      read excel cell  2  ${i}  Sheet2   

        # This is a nested list where the @{queryResults}[0][0] means the first column from the first row.
        @{queryResults}    query  select a_${i} from test_table where a_1 = 'filename1'

        run keyword and continue on failure  
        ...       should be equal as Strings      
        ...            @{ListA}[${i}]      
        ...            @{ListB}[${i}]
        ...            values=False
        ...            msg=In Row "${1}" Column " ${ColumnName}" value ${Excelvalue} != @{queryResults}[0][0].
    END

推荐阅读