【问题标题】:Robot Framework - run keyword if file existsRobot Framework - 如果文件存在则运行关键字
【发布时间】:2022-03-07 17:28:03
【问题描述】:

如果文件存在于文件系统中,如何在机器人框架中运行关键字? 例如:

Run Keyword If    ${filename} exists    Delete File

【问题讨论】:

  • 使用OS lib检查文件是否退出,然后将返回值放入run关键字中。

标签: robotframework


【解决方案1】:

OperatingSystem 库可以用于此,即使没有任何关键字可以满足您的需要。但是你可以发挥创造力,也许可以使用Get File、Get File Size、List Files In Directory、Run And Return Rc 甚至其他东西。还有File Should Exist、File Should Not Exist、Should Exist等关键字。也许您可以更改您的代码以便使用这些代码。

或者您创建自己的简单库:

库/file.py

import os

def file_exists(file):
    return os.path.isfile(file)

像您在问题中提到的那样导入并使用它:

Tests/test.robot

*** Settings ***
Library    ../Libraries/file.py    

*** Test Cases ***
Test File Exists
    ${fileExists}=    File Exists    test.robot
    Run Keyword If    ${fileExists} is True    Log To Console    Exists!         

【讨论】:

    【解决方案2】:

    当我需要这种操作时,我会编写类似的代码......也许对你有用。

    *** Settings ***
    Library    Process
    
    *** Variables ***
    @{FILES}    ${CURDIR}/someFolder/aaa.pdf
    ...         ${CURDIR}/someFolder/bbb.pdf
    
    *** Test Cases ***
    Check and Delete Files
        FOR    ${file}    IN    @{FILES}
            ${out} =    run process    ls    ${file}
            run keyword if    ${out.rc} == 0    Delete File    ${file}    
        END
    
    *** Keywords ***
    Delete File
        [Arguments]    ${f}
        ${o} =    run process    rm    -f    ${f}
        Should Be Equal As Integers   ${o.rc}    0
    

    【讨论】:

      猜你喜欢
      • 2015-08-17
      • 2015-07-13
      • 2016-12-16
      • 2011-11-26
      • 2021-07-17
      • 2023-04-02
      • 2020-10-20
      • 2016-09-17
      • 2015-12-04
      相关资源
      最近更新 更多