【问题标题】:"Wait Until Keyword Succeeds" doesn't execute the whole keyword“等到关键字成功”不会执行整个关键字
【发布时间】:2020-11-18 22:44:35
【问题描述】:

这是一段文字墙,但我尽量保持简短。

情况: 我正在打开一个显示任务和加载栏的网站。加载栏可以显示 0-100%、完成或停止在 0-100% 的任一数字。 理念: 我使用“获取文本”来读取加载栏并检查它是“完成”还是“停止”。要忽略百分比,我只想要文本的第一部分(例如,仅“停止”)。 我用“等待关键字成功”重复关键字,直到文本停止或完成。

代码:

Observe Task
    Wait Until Keyword Succeeds    120 min     60 sec    Check Progress Status    ${task_progress_bar}

Check Progress Status
    [Arguments]    ${task_progress_bar}
    ${task_status} =    Get Text    ${task_progress_bar}
    Log to Console    \n ${task_status}
    @{words} =    Split String    ${test_string}    ${SPACE}
    ${first_word} =     @{words}[1]
    Log to Console    \n ${first_word}
    Log to Console     'this is a test message' 

我记录状态以检查它是否有效。确实如此。控制台显示 0%、0%、停在 0% 等 但这是我得到的唯一输出。它既不显示 first_word 变量,也不显示测试消息。 它似乎在拆分字符串时中断

我试图解决的问题:

    ${test_string}    Set Variable    'Robot is not working'
    Log to Console    \n ${test_string}
    @{words} =    Split String    ${test_string}    ${SPACE}
    ${first_word} =     @{words}[1]
    Log to Console    \n ${first_word}

如果阅读文本有问题,我使用了一个简单的字符串。控制台上只显示“机器人不工作”。

    #${split} =    Fetch From Left    ${test_text}    ${SPACE}
    #Log to Console    \n ${split}

使用从左侧获取。同样的错误。 我在不同的测试服中尝试了 Fetch 和 Split。两者都有效,所以语法应该是正确的。

    Run Keyword And Ignore Error    Wait Until Keyword Succeeds    120 min     120 sec    Check Progress Status    ${task_progress_bar}

忽略错误并将时间间隔增加到 2 分钟。

不管我做什么,控制台只显示:

'Robot is not working'

'Robot is not working'

'Robot is not working'

或“停止在 0%”。 我的猜测是“等待关键字成功”在执行整个“检查进度状态”之前以某种方式循环。或者字符串拆分会破坏关键字,但我不知道为什么会发生这种情况。 我真的很感激一些帮助。谢谢。

编辑:这里建议的是我正在使用的缺少的 xpath。由于我用一个简单的字符串替换了它们(参见修复尝试 2)并且它也不起作用,我认为这不是问题的一部分。无论如何:

direct xpath: 
//*[@id="app"]/div/main/div/section/div[2]/div[2]/table/tbody/tr/td[2]/div/a/div/div[2]/span
xpath im using: 
xpath=//tr[contains(., '${task_name}')]/td[2]/div/a/div/div[2]/span
Code snippet to read the text: 
${task_progress_bar} =    Set Variable    xpath=//tr[contains(., '${task_name}')]/td[2]/div/a/div[2]/span

${task_name} 只是名称,显示在进度条的左侧(例如 Process 1)

【问题讨论】:

  • 你操作的元素的html代码是什么?请不要添加它的图片,这是一种不好的做法,没有人会为您编写该代码。目前帖子的描述很简洁,只缺少元素或页面的 html 代码。
  • 这不是有效的机器人语法:${first_word} = @{words}[1]。要进行变量赋值,您必须调用关键字。

标签: python robotframework


【解决方案1】:

我认为可能有一些小问题可能会滚雪球成为无效的关键字。我不能 100 % 确定我的更改会解决整体问题,但我会从它们开始,看看我会得到什么。

当我执行时:

*** Test Cases *** 
Split Example
    ${test_string}    Set Variable    'Robot is not working'
    @{words} =    Split String    ${test_string}    ${SPACE}
    ${first_word} =     @{words}[1]
    Log to Console    \n ${first_word}

我收到一个错误:

未找到名称为“@{words}[1]”的关键字。

所以我解决了这个问题:

${first_word} =     Set Variable    @{words}[1]

并再次执行相同的测试用例。现在我收到两个警告:

[ WARN ] 不推荐使用“@{words}[1]”语法访问变量项。请改用“${words}[1]”。
[ WARN ] 不推荐使用 '\n' 后忽略空格。欲了解更多信息,请参阅:https://github.com/robotframework/robotframework/issues/3333

我正在使用 RF 3.2.1,它可能在这里发挥作用。

我又修复了这两个,所以整个测试用例是这样的:

*** Test Cases *** 
Split Example
    ${test_string}    Set Variable    'Robot is not working'
    @{words} =    Split String    ${test_string}    ${SPACE}
    ${first_word} =     Set Variable    ${words}[1]
    Log to Console    \n${first_word}

现在我将is 输入控制台,这似乎是此处所需的输出。

我也注意到你的例子:

Check Progress Status
    [Arguments]    ${task_progress_bar}
    ${task_status} =    Get Text    ${task_progress_bar}
    Log to Console    \n ${task_status}
    @{words} =    Split String    ${test_string}    ${SPACE}
    ${first_word} =     @{words}[1]
    Log to Console    \n ${first_word}
    Log to Console     'this is a test message' 

${test_string} 来自哪里?它没有在关键字的任何地方初始化,也不是一个参数。确保变量存在。

【讨论】:

  • 我正在使用 RF 3.1.2 并更改了您的建议($words 和 Set Var),但它不起作用。奇怪的是,我没有收到任何错误消息。另外,当我在一个空的测试用例中使用 split 时,它起作用了。并且列表问题不应该是问题,因为从左侧获取也不起作用。感谢您的快速帮助。对此,我真的非常感激。编辑: test_string 在我的第一次修复尝试中使用。我目前正在使用简单字符串来排除任何与读取相关的错误
  • 那我的答案并不是真正的答案。至少我帮助改进了一点,但主要问题仍然存在。如果我有其他想法,我会在这里编辑我的答案。
  • @Saccarine:如果您进行了这些更改,请更新您的问题以包含更正后的代码。
【解决方案2】:

在关键字里面,这个语法:

${first_word} =     @{words}[1] 

无效 - 要进行变量赋值,您必须有一个关键字调用,它返回一个值。所以改成:

${first_word} =     Get From List    ${words}    1

,或

${first_word} =     Set Variable    ${words[1]}

顺便说一下,列表是从 0 开始的,因此索引 1 将获得其中的第二个元素 - 我希望这是您的意图。

【讨论】:

    猜你喜欢
    • 2019-01-01
    • 2021-09-18
    • 1970-01-01
    • 2018-08-03
    • 2015-07-13
    • 2021-01-26
    • 2020-10-06
    • 2010-12-26
    • 1970-01-01
    相关资源
    最近更新 更多