机器人框架 - 如果文件存在则运行关键字

Sha*_*yal 1 robotframework

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

Run Keyword If    ${filename} exists    Delete File
Run Code Online (Sandbox Code Playgroud)

pav*_*man 7

操作系统库可以用于此目的,即使没有任何关键字满足您的需要。但您可以发挥创意,也许可以使用、、、Get File甚至其他东西。还有诸如、、之类的关键字。也许您可以更改代码以便可以使用它们。Get File SizeList Files In DirectoryRun And Return RcFile Should ExistFile Should Not ExistShould Exist

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

库/file.py

import os

def file_exists(file):
    return os.path.isfile(file)
Run Code Online (Sandbox Code Playgroud)

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

测试/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!         
Run Code Online (Sandbox Code Playgroud)