【问题标题】:How do I iterate through verifying different texts on a page in Robot Framework?如何在 Robot Framework 中遍历验证页面上的不同文本?
【发布时间】:2022-12-29 02:31:49
【问题描述】:

我有一个功能正常的 Robot Framework 测试,用于检查页面上的不同文本。这是非常基本的。扫描页面以查找特定字符串,然后在找到该字符串时记录通过/失败。这是我的代码。

Test Keyword
    ${p1}=  Run Keyword And Return Status   Page Should Contain Element   xpath=//*[contains(text(), "A")]
    Run Keyword If  ${p1}  Log To Console  "(A) Present"  ELSE  Log To Console  "(A) Not Present"

    ${p2}=  Run Keyword And Return Status   Page Should Contain Element   xpath=//*[contains(text(), "B")]
    Run Keyword If  ${p2}  Log To Console  "(B) Present"  ELSE  Log To Console  "(B) Not Present"

    ${p3}=  Run Keyword And Return Status   Page Should Contain Element   xpath=//*[contains(text(), "C")]
    Run Keyword If  ${p3}  Log To Console  "(C) Present"  ELSE  Log To Console  "(C) Not Present"

这运行得很好,但我无法将其放入列表中。或者也许是一个数组?我不确定。

我是否在列表中创建 xpaths 变量?我是否可以将 Run Keyword If 语句作为自己的关键字然后传递它们?我不确定。请让我知道我哪里出错了。谢谢!

【问题讨论】:

    标签: automation automated-tests robotframework


    【解决方案1】:

    您在示例中所拥有的是仅具有一个变量的相同代码的重复 - 这始终是一个很好的循环候选者。这样你就可以传递一个不同的值,做同样的检查。

    FOR    ${a}    IN    A    B    C
    
        ${p}=  Run Keyword And Return Status   Page Should Contain Element   xpath=//*[contains(text(), "${a}")]
        IF  ${p}  Log To Console  "(${a}) Present"  ELSE  Log To Console  "(${a}) Not Present"
    
    END
    

    如果您正在检查的值更多(或动态)并且将它们列为循环不方便,您可以将它们放在列表中并对其进行迭代:

    ${values}=    Create List   A    B    C
    FOR    ${a}    IN    @{values}
    # the same code follows here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-12
      • 2020-05-02
      • 2018-01-16
      • 2020-01-19
      • 2019-05-15
      • 2022-11-01
      • 1970-01-01
      • 2016-01-09
      相关资源
      最近更新 更多