首页 > 解决方案 > 无法在 RobotFramework 中写入现有文本文件

问题描述

无法在 RobotFramework 中写入现有文本文件。我的目标是从 for 循环获取输出并将值写入文本文件,目前我能够创建文本文件并从 for 循环打印输出值,但无法将值写入文本文件。

我试过的:

*** Settings ***
Library           OperatingSystem

*** Variables ***
${PATH}           ${CURDIR}/write_one_to_five.txt

*** Test Cases ***
For_Loop
    Create File    ${PATH}    # Text file created at current directory
    : FOR    ${i}    IN RANGE    1    6
    \    Log    ${i}
    File Should Exist    ${PATH}    ${i}
    Log    Exited

标签: pythontext-filesrobotframework

解决方案


您可以使用来自操作系统库http://robotframework.org/robotframework/latest/libraries/OperatingSystem.html的关键字Append To File

使用Append To File关键字对您的代码进行了一些小修改,它起作用了!!

*** Settings ***
Library           OperatingSystem

*** Variables ***
${PATH}           ${CURDIR}/write_one_to_five.txt

*** Test Cases ***
For_Loop
    Create File    ${PATH}    # Text file created at current directory
    : FOR    ${i}    IN RANGE    1  6
    \    log to console  ${i}
    \    ${b}=  Convert To String  ${i}     #conversion was required as it was throwing encoding error for integer
    \    Append To File  write_one_to_five.txt  ${b}
    #File Should Exist    ${PATH}    ${i}      #This was causing error to me, hence commented
    Log    Exited

推荐阅读