2

我有一个机器人测试用例来升级我的盒子...如果有任何错误,机器人框架会截取屏幕截图并将 screenshot.png 保存在报告目录中。

现在我如何将其发送到 reportportal.in

我在运行机器人时传递报告门户信息,如下所示。

robot --outputDir /opt/robotframework/reports --listener robotframework_reportportal.listener -v RP_UUID:07-aeb0-315c81358edd -v RP_ENDPOINT:http://<reportportalipaddress>:8080  -v RP_LAUNCH:TEST_UPGRADE  -v RP_PROJECT:TEST_UPGRADE /opt/robotframework/tests

我的机器人测试用例

***Test Cases***
FROM_GUI
    Close All Browsers
    Open Browser    ${URL}    gc
    Input Text    name:username    admin
    Input Password    name:password    &{${CPE}}[cpe_password]
    Click Button    name:Continue
    Log to Console    Inside GUI ${uploadPath}//${uploadFile}
    input text    name=uploadFile    ${uploadPath}//${uploadFile}
    Page Should Contain    firmware update is in progress
    sleep    10 seconds
    click link    link=Logout
    Close All Browsers
    Sleep     180 seconds
    with open("../", "rb") as image_file:
        file_data = image_file.read()

    rp_logger.info("Some Text Here",
                            attachment={"name": "selenium-screenshot-1.png",
                                                 "data": file_data,
                                                 "mime": "image/png"})
    [Teardown]

在报告门户中

我只看到下面的,看不到截图

</td></tr><tr><td colspan="3"><a href="selenium-screenshot-1.png"><img src="selenium-screenshot-1.png" width="800px"></a>
4

1 回答 1

0

我已经尝试了 2 种方法来做到这一点(可以有更多):

  1. 将图像嵌入到报告中,它也将嵌入到报告门户报告中。

使用 SeleniumLibrary 提供的 Run-on-failure 功能:SeleniumLibrary 有一个方便的功能,如果它自己的任何关键字失败,它可以自动执行关键字。默认情况下,它使用Capture Page Screenshot 关键字。更多信息可在官方文档中找到

默认情况下,捕获页面屏幕截图的文件名值为

文件名 = selenium-screenshot-{index}.png

您可以在加载 Selenium 库时为 run_on_failure 参数提供自定义关键字。

在自定义关键字中,使用 filename=EMBED 调用 Capture Page Screenshot,如下所示。

*** Settings ***
Library           SeleniumLibrary    run_on_failure=Capture Screenshot and embed it into the report

*** Keywords ***
Capture Screenshot and embed it into the report
    Capture Page Screenshot    filename=EMBED

注意:当您将图像嵌入到报告中时,图像文件不会作为单独的文件出现在您的 Result 文件夹中。

  1. 捕获屏幕截图并将其作为附件发送:使用以下函数创建自定义 python 库robotsframework-reportportal 文档,例如:utilityLib.py:
from robotframework_reportportal import logger

@keyword
def send_attachment_to_report_portal(path):
  with open(path, "rb") as image_file:
    file_data = image_file.read()
  logger.info("Some Text Here",
    attachment={"name": "test_name_screenshot.png",
                "data": file_data,
                "mime": "image/png"})

与第一种解决方案中的步骤类似:通过 Run-on-failure 功能调用自定义关键字

*** Settings ***
Library           SeleniumLibrary    run_on_failure=Capture Screenshot and attach it to reportportal
Library           utilityLib.py

*** Keywords ***
Capture Screenshot and attach it to reportportal
    ${path} =    Capture Page Screenshot
    Send attachment to report portal    ${path}
于 2021-07-01T11:34:20.350 回答