首页 > 解决方案 > 当在机器人框架中找不到元素时,如何在测试期间在控制台中显示消息?

问题描述

我想知道当机器人框架找不到元素时如何在控制台中显示消息:

我试过这个:

S2L.Wait Until Page Contains Element  ${checkbox}  10s checkbox  not found

它不适用于这个元素,不知道为什么。它适用于任何其他元素,但不适用于我的复选框。

所以现在我有这个:

Wait Until Keyword Succeeds  5 times  2 sec  S2L.Click Element ${checkbox}

但是当它失败时,它只说找不到元素,我更愿意编写个性化消息。

欢迎任何帮助。谢谢

标签: robotframework

解决方案


尽管我确实认为您的问题有所不同,但可以回答您的问题。Run Keyword And Return Status将捕获错误并继续并提供状态。Run Keyword If然后允许使用关键字Fail,它还将在控制台上生成一条消息。

*** Settings ***
Library    SeleniumLibrary

Suite Teardown    Close All Browsers 

*** Test Cases ***
Wait And Click succesfully
    Open Browser    http://google.com    HeadlessChrome
    Wait and Click Element    name:q    This should work

Wait And Click unsuccesfully
    Open Browser    http://google.com    HeadlessChrome
    Wait and Click Element    name:nobtn      This should not work

*** Keywords ***
Wait and Click Element
    [Arguments]    ${locator}    ${message}=None

    ${status}    Run Keyword And Return Status
    ...                Wait Until Keyword Succeeds  
    ...                    5 times  2 sec  
    ...                    Click Element   ${locator}

    Run Keyword If    
    ...    "${status}" == "False"
    ...    Fail     ${message}

推荐阅读