Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

robotframework - Robot Framework - run keyword if file exists

How can run keyword in robot framework if file exists in the filesystem? For example:

Run Keyword If    ${filename} exists    Delete File

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OperatingSystem library could be used for this, even though there's not exactly any keyword for what you need. But you can get creative and perhaps use Get File, Get File Size, List Files In Directory, Run And Return Rc or even something else. There are also keywords like File Should Exist, File Should Not Exist, Should Exist. Perhaps you can change your code so you can use these.

Or you create your own simple library:

Libraries/file.py

import os

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

import it and use it like you mentioned in your question:

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!         

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...