机器人框架:从远程位置获取多个文件到目标目录

时间:2015-09-16 09:00:33

标签: automation robotframework

使用Robot Framework,有没有办法将多个文件从目录复制到本地目录?

我在SSHLibrary中看到Get File个关键字。 它支持复制多个文件,但在指定目标目录时不起作用。

如果我们只指定一个要复制的文件,则目标目录参数有效 如果在源中指定了多个文件(例如/path/*.ext)并且指定了目标目录,那么我会收到此错误:

Cannot copy multiple source files to one destination file

如果我删除目标目录作为参数,那么所有文件都会被复制到执行目录,我想避免这样做。

这是一个存根:

Get Remote files 
     [Arguments]    ${HOST} ${ROOT_PASS}    ${TARGET_DIR}
     Open Connection  ${HOST}
     Login  ${ROOT_USER}    ${ROOT_PASS}
     Log To Console     Logged in to server
     SSHLibrary.Get File    //*.ext ${TARGET_DIR}
     Log To Console     Files copied to ${TARGET_DIR}

3 个答案:

答案 0 :(得分:1)

如果要从其他文件夹中的文件夹下载多个文件,则可以使用SSH库的**Get Directory****List Directories In Directory**方法。

  1. 创建关键字'从远程服务器获取文件夹并将其复制到'它有两个参数:源文件夹路径和目标文件夹路径。

    ***Keywords***
    Get Folder from remote server and copy it
        [Documentation]    Used to copy Folder within Folder from some source location(can be from local or remote machine) to destination location(can be into local or remote machine)
        [Arguments]    ${source}    ${destination}
        Log To Console    \n--------------------------------Copying Result folder from remote machine to local machine      
        Get Directory    ${source}    ${destination}
        @{directories}    List Directories In Directory    ${source}
        : FOR    ${dir}    IN    @{directories}
        \    ${currentSrc}    Catenate    SEPARATOR=     ${source}    ${dir}           
        \    ${currentDestination}    Catenate    SEPARATOR=     ${destination}    ${dir}
        \    Get Directory    ${currentSrc}    ${currentDestination}
    
  2. 在测试用例中调用此关键字。

    *** Test Cases ***
    Copy multiple files    
        Get Folder from remote server and copy it locally    ${Resultsource}  ${Resultdestination}
    

答案 1 :(得分:0)

这是测试机器人脚本,它将所有文件成功地从远程机器到本地机器的目标目录。

Trick是用/结束目标目录(使用$ {/}表示平台独立,比如C:$ {/} temp $ {/}告诉C:/ temp / on windows,无论如何linux示例如下所示)

*** Settings ***

Library                SSHLibrary

*** Variables ***
${SERVER}                   192.168.0.1
${SYSTEM_USER}                  app1
${SYSTEM_PASSWORD}              app1xxx
${QUERY}                        /home/app1/test/configuration/*.props
${TARGET_DIR}                   /tmp/test/
*** Test Cases ***


Get Multiple Files To A Directory
    [tags]              multi-files
    Get Remote files  ${SERVER}     ${SYSTEM_USER}  ${SYSTEM_PASSWORD}  ${QUERY}    ${TARGET_DIR}

*** Keywords ***
Get Remote files
     [Arguments]    ${host}     ${user}     ${pwd}    ${query}     ${dir}
     Open Connection  ${host}
     Login  ${user}    ${pwd}
     Log To Console     Logged in to server
     SSHLibrary.Get File    ${query}    ${dir}
     Log To Console     Files copied to ${TARGET_DIR}
     close all connections

答案 2 :(得分:-1)

我假设SSHLibrary文档中存在错误。

来自get file的文档:

1. If the destination is an existing file, the source file is downloaded over it.
2. If the destination is an existing directory, the source file is downloaded into it. Possible file with the same name is overwritten.
3. If the destination does not exist and it ends with the path separator of the local operating system, it is considered a directory. The directory is then created and the source file is downloaded into it. Possible missing intermediate directories are also created.
4. If the destination does not exist and does not end with the local path separator, it is considered a file. The source file is downloaded and saved using that file name, and possible missing intermediate directories are also created.

我假设对于(2)就像(3)一样,你需要在目标文件夹上有一个尾随/。

你也可以尝试指向一个不存在的文件夹(将在现场创建) - 再次使用尾随/