通过Robot Framework Test在Selenium Grid中下载文件

时间:2018-02-19 18:53:29

标签: robotframework selenium-grid

${options}=     Call Method     ${chrome_options}    to_capabilities 
Create Webdriver    Remote   command_executor=${REMOTE_URL}   desired_capabilities=${options}
Go To    ${url}

获取测试的节点IP和SCP文件的可能解决方案之一。 有没有办法可以在Robot Framework中获取Node详细信息。

2 个答案:

答案 0 :(得分:1)

我为此使用了一个有趣的解决方法。在我的套件设置中:

Set Global Variable    ${global_downloadDir}    ${CURDIR}\\Downloads\\${suiteName}
${prefs} =    Create Dictionary    download.default_directory=${global_downloadDir}
Call Method    ${chromeOptions}    add_experimental_option    prefs    ${prefs}
Create Webdriver    Chrome    chrome_options=${chromeOptions}

在我的测试拆解中,我需要清空mi下载目录:

Run Keyword And Ignore Error    Empty Directory    ${global_downloadDir}

然后我设置了两个关键字:

Download Should Be Done
    [Arguments]    ${directory}=${global_downloadDir}
    [Documentation]    Verifies that the directory has only one folder and it is not a temp file and returns path to the file
    ${files} =    List Files In Directory    ${directory}
    Length Should Be    ${files}    1
    Should Not Match Regexp    ${files[0]}    (?i).*\\.tmp
    Should Not Match Regexp    ${files[0]}    (?i).*\\.crdownload
    ${file}    Join Path    ${directory}    ${files[0]}
    Log    File was successfully downloaded to ${file}
    [Return]    ${file}

Wait Until File Download is Finished
    [Arguments]    ${directory}=${global_downloadDir}    ${timeout}=${global_timeout}    ${retry}=2s
    ${fileName} =    Wait Until Keyword Succeeds    ${timeout}    ${retry}    Download should be done    ${directory}
    [Return]    ${fileName}

现在,使用等待文件下载已完成关键字应该返回您的文件名:

Click Button    id=downloadFile
${filename} =    Wait Until File Download is Finished
${content} =    Get File    ${fileName}

答案 1 :(得分:0)

如果文件是你之后的文件,那么我将共享的驱动器/文件夹映射到执行节点,然后让Chrome将文件存储在那里。如果文件具有与在同一位置存储文件的其他测试不会发生冲突的唯一文件名,则此方法可以正常工作。

另一种方法是使用主机名或IP作为唯一节点标识符。获取主机名的详细信息请参见StackOverflow answerHow to display/log which node ran Selenium test session?。这段代码背后的原理最好在StackOverflow answer中解释。

如果您还在网格上运行无头Chrome并希望将文件下载到特定目录,那么此StackOverflow answer将为您提供必要的Python和机器人代码,以实现它本机不受支持。