将 name1 和 name 2 验证到 connection,prov

Validate name1 and name 2 to connection,prov

我想使用 Python/Robot Framework 验证两个命名连接及其连接状态和来自给定文本文件的 prov id 值。以下是文本文件 Test.txt

中的示例

name..........: uhn3
connection....: Connected (1)
prov id.......: <[1] id:17/IPv6:240bc0ea01e1554e:a01000000000002/128:5551>
connection....: Connected (1)
prov id.......: <[2] id:17/IPv6:240bc0ea01e1554e:a01000000000002/128:5552>
connection....: Connected (1)
name..........: uhn3k
connection....: Connected (145)
prov id.......: <[3] id:16/IPv6:240bc0ea01e1554e:a01000000000001/128:5551>
connection....: Connected (1)
prov id.......: <[4] id:16/IPv6:240bc0ea01e1554e:a01000000000001/128:5552>
connection....: Connected (1)
*

这是我用过的机器人脚本。

*** Variables ***
@{application_reqfields}  name  connection  prov id
    
*** Test Case ***
Login SG and check the status
    ${Cmd_Output}=  Get File  Test.txt
    log to console  ${Cmd_Output}
    ${retVal}=  application_validation   ${Cmd_Output}      
    ${application_reqfields}
    log to console  ${retVal}
    
*** Keywords ***
application_validation
    [Arguments]     ${output}   ${reqFields}
    &{retVal}=  Create Dictionary
    FOR     ${field}     IN      @{reqFields}
        @{match}=  Get Regexp Matches   ${output}     .*${field}\.*\:\s*(\w+)  1
        set to dictionary   ${retVal}   ${field}    ${match}
    END
    [return]  ${retVal}

关键字application_validation通过,输出看起来像

{ 'name': ['uhn3', 'uhn3k'], 'connection': ['Connected', 'Connected', 'Connected', 'Connected', 'Connected'], 'prov id': [] }

我需要从返回值中检索该连接的名称和相关连接状态。

您的脚本似乎有两个问题,一个与无法访问正确的字典值进行验证有关,另一个是 prov id 值为 return编辑为空列表。

获取正则表达式匹配的一般用法

您收到的输出实际上在关键字 Get Regexp MatchesString Library documentation 中进行了描述。那里声明关键字将 return 所有 非重叠匹配列表 。这意味着您收到的输出符合预期。

您只需使用

即可访问 Robot Framework 中的字典值
${VALUE}=    Set Variable    ${DICT[key]}

因为您将键后面的值作为列表,所以您需要先从字典中获取列表,然后访问您希望打开的列表中的索引。

${name1}=    Get From List    ${retVal["name"]}    0

Get From List is from Collections library。但是当然这只会 return 名称而不是与之相关的任何内容。

此外,您对 prov id 的问题仅仅是因为您的正则表达式调用没有考虑值中的额外字符。如果您更改正则表达式以考虑这些而不是简单地期望空格,它将正常工作。这个正则表达式似乎工作正常。

${field}\.*\:\s(.*\w+)

维护项目关系

为了保持项目之间的关系,以便 name 可以附加到 prov id/connection 的相应条目,您需要有一个关于如何实体在这里工作,例如

A name field starts a new entity, each connection/prov id is related to the name above it.

Only 3 connection fields may exist for name

一旦你弄清楚了,你可以使用下面的脚本分别捕获每个字段,然后从那里构建。我假设无论有多少条目,特定 name 下的任何内容都将分配给该字段。

application_validation
    [Arguments]     ${output}   ${reqFields}
    &{retVal}=  Create Dictionary
    @{entities}=  Split String    ${output}    name  # Splitting the string on name field
    ${removed}=  Remove From List    ${entities}    0  # Removing empty item on index 0
    FOR  ${item}    IN   @{entities}    # Iterating to get data based on names
        @{name}=    Get Regexp Matches    ${item}    ^\.*\:\s(?P<name>\w+)    name  # Expecting only single name but the keyword will return a list anyway
        @{connections}=    Get Regexp Matches    ${item}    connection\.*\:\s*(?P<conn>\w+)  conn
        @{provs}=  Get Regexp Matches    ${item}    prov id\.*\:\s*(?P<prov>.*\w+)  prov

        # Let's assign the connection status to correct prov id
        &{tempdict}=    Create Dictionary
        FOR    ${conn}  ${prov}    IN ZIP    @{connections}  @{provs}
            Set to Dictionary    ${tempdict}    ${prov}=${conn}
        END
        # Then we need to create an output for each name/value entity
        Set to Dictionary    ${retVal}    ${name}[0]=${tempdict}
    END
    [Return]    ${retVal}

上面的脚本将为每个 name 提供一个带有嵌套字典的字典,其中内部字典包含 prov id 作为键和 connection 作为值。例如,您示例中的 uhn3 将 returned 为

&{retVal}= { "uhn3": {"<[1] id:17/IPv6:240bc0ea01e1554e:a01000000000002/128:5551>": "connected", "<[2] id:17/IPv6:240bc0ea01e1554e:a01000000000002/128:5552>": "connected" }

然而,此脚本的弱点是,如果 connectionprov id 字段的数量可能不同,则会出现索引错误,因此您必须相应地进行调整。

调用字典中的值可以通过先获取外部字典来完成

FOR    ${name}    IN     @{retVal.keys()}
    Log    ${name}
    FOR    ${conn}    IN   @{name.keys()}
        Log    ${conn}: ${name[${conn}]}
    END
END