2016-02-09 16 views
7

Uso Robot Framework.Download file di robot

Nella mia pagina HTML ho un semplice pulsante. Quando fai clic su di esso, scarica un file pdf.

Come posso verificare con Robot Framework se il file è stato scaricato?

Tks

ho trovato una soluzione, tks a @ ombre42:

${SERVER}     ${SERVER_DEV} 
${NAME}     Robot 
${FILE_NAME}    Robot.pdf 
${CLASS_NAME}    in 
${DOWNLOAD_DIRECTORY}  C:\\robot_download 

Scenario: User can download 
    Create Directory ${DOWNLOAD_DIRECTORY} 
    ${CHROME_OPTIONS}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver 
    ${disabled} Create List  Chrome PDF Viewer 
    ${prefs} Create Dictionary download.default_directory=${DOWNLOAD_DIRECTORY} plugins.plugins_disabled=${disabled} 
    Call Method ${CHROME_OPTIONS} add_experimental_option prefs ${prefs} 
    Create Webdriver Chrome chrome_options=${CHROME_OPTIONS} 
    Goto ${SERVER} 
    Click Element ${NAME} 
    Wait Until Element Is Visible css=div.${CLASS_NAME} 8 
    Page Should Contain ${NAME} 
    Set Selenium Speed 10s 
    Download PDF ${NAME} 
    File Should Exist C:\\robot_download\\${FILE_NAME} 
+0

Quale browser stai utilizzando? – ombre42

+0

Sto usando Chrome. – Raphael

risposta

10

La soluzione è molto specifico browser. Per Chrome, puoi dire a Chrome dove scaricare i file. La scelta di una nuova cartella consente di monitorare lo stato del download. Inoltre, dal momento che stai scaricando un PDF, la disattivazione del plug-in PDF è necessaria per impedire la visualizzazione del PDF anziché il download. Ecco un test che ha funzionato sulla mia macchina utilizzando una semplice pagina e un file PDF.

*** Settings *** 
Test Teardown  Close All Browsers 
Library   Selenium2Library 
Library   OperatingSystem 

*** Test Cases *** 
Download PDF 
    # create unique folder 
    ${now} Get Time epoch 
    ${download directory} Join Path ${OUTPUT DIR} downloads_${now} 
    Create Directory ${download directory} 
    ${chrome options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver 
    # list of plugins to disable. disabling PDF Viewer is necessary so that PDFs are saved rather than displayed 
    ${disabled} Create List Chrome PDF Viewer 
    ${prefs} Create Dictionary download.default_directory=${download directory} plugins.plugins_disabled=${disabled} 
    Call Method ${chrome options} add_experimental_option prefs ${prefs} 
    Create Webdriver Chrome chrome_options=${chrome options} 
    Goto http://localhost/download.html 
    Click Link link # downloads a file 
    # wait for download to finish 
    ${file} Wait Until Keyword Succeeds 1 min 2 sec Download should be done ${download directory} 

*** Keywords *** 
Download should be done 
    [Arguments] ${directory} 
    [Documentation] Verifies that the directory has only one folder and it is not a temp file. 
    ... 
    ... Returns path to the file 
    ${files} List Files In Directory ${directory} 
    Length Should Be ${files} 1 Should be only one file in the download folder 
    Should Not Match Regexp ${files[0]} (?i).*\\.tmp Chrome is still downloading a file 
    ${file} Join Path ${directory} ${files[0]} 
    Log File was successfully downloaded to ${file} 
    [Return] ${file} 

Contenuto del download.html:

<html><body><a href="file.pdf" id="link">Click Here</a></body></html> 
+0

IE è possibile farlo? – user2520217

0

è necessario controllare l'MD5 del file - prima del download e dopo il download. Sia l'MD5 dovrebbe essere lo stesso.

file di Supponendo che si trova sulla macchina Linux - prima e dopo il download:

  1. Accedere al sistema Linux usando SSH libreria
  2. Eseguire il comando: # md5sum path_to_file
  3. memorizzare l'output in una variabile.
  4. Confrontare il valore della variabile

spero informazioni sono state utili.