You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需bash robot命令,如何在Robot Framework(Selenium)自定义文件存储路径?

自定义Robot Framework日志、输出、报告及截图路径(无需robot命令)

不用敲robot命令的话,有三种常用方案可以实现自定义路径配置,直接在代码或配置里搞定:

方案1:通过Python API直接控制

这是最灵活的方式,用Python脚本调用Robot的API执行测试,同时指定各类文件路径。

示例代码:

from robot.run import run
import os

# 提前创建目标目录,避免路径不存在报错
output_dir = "./custom_test_results"
screenshot_dir = f"{output_dir}/screenshots"
os.makedirs(screenshot_dir, exist_ok=True)

# 执行测试并指定所有输出路径
run(
    "./your_test_suite.robot",
    log=f"{output_dir}/custom_log.html",
    report=f"{output_dir}/custom_report.html",
    output=f"{output_dir}/custom_output.xml",
    variable=f"SELENIUM_SCREENSHOT_DIRECTORY:{screenshot_dir}",
    loglevel="INFO"
)
  • log/report/output参数直接指定Robot核心输出文件的路径
  • 通过variable参数传递全局变量,设置SeleniumLibrary的截图存储目录
  • 用os.makedirs确保目标目录存在,避免执行时抛出路径错误

方案2:使用Robot配置文件

创建一个robot.cfg配置文件统一管理路径,再用Python API加载配置执行测试,适合需要复用配置的场景。

配置文件示例(robot.cfg):

[Execution]
Log: ./custom_results/test_log.html
Report: ./custom_results/test_report.html
Output: ./custom_results/test_output.xml

[Variables]
${SELENIUM_SCREENSHOT_DIRECTORY} = ./custom_results/screenshots

Python调用代码:

from robot.run import run
import os

# 提前创建配置中定义的目录
os.makedirs("./custom_results/screenshots", exist_ok=True)

run(
    "./your_test_suite.robot",
    config="./robot.cfg",
    loglevel="INFO"
)
  • 配置文件的[Execution]段对应Robot的核心输出路径,[Variables]段设置全局变量(包括截图路径)
  • 执行前仍需确保配置中的目录已存在

方案3:在测试套件内部直接设置

完全在Robot测试代码里实现,无需额外Python脚本,适合纯Robot编写的项目。

示例Suite文件:

*** Settings ***
Suite Setup    Create Directory    ./suite_results/screenshots
Suite Setup    Set Suite Variable    ${LOG_FILE}    ./suite_results/suite_log.html
Suite Setup    Set Suite Variable    ${REPORT_FILE}    ./suite_results/suite_report.html
Suite Setup    Set Suite Variable    ${OUTPUT_FILE}    ./suite_results/suite_output.xml
Library        SeleniumLibrary    screenshot_directory=./suite_results/screenshots

*** Test Cases ***
Sample UI Test
    Open Browser    https://example.com    chrome
    Capture Page Screenshot    sample_shot
    Close Browser
  • ${LOG_FILE}/${REPORT_FILE}/${OUTPUT_FILE}是Robot内置变量,设置后会覆盖默认路径
  • 导入SeleniumLibrary时直接指定screenshot_directory参数,所有截图自动存入该目录
  • 用Create Directory关键字确保截图目录存在,避免截图失败

内容的提问来源于stack exchange,提问作者Mobin Nikrou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:52:58