Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k 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
question from:https://stackoverflow.com/questions/65600479/robot-framework-run-keyword-if-file-exists

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

1 Answer

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!         

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...