2016-02-09 23 views
7

Tôi sử dụng Khung công tác Robot.Robot Khung Tải xuống Tệp

Trên trang HTML của tôi, tôi có một nút đơn giản. Khi bạn nhấp vào nó, nó sẽ tải xuống một tệp pdf.

Làm cách nào tôi có thể kiểm tra với Robot Framework nếu tệp đã được tải xuống?

Tks

Tôi tìm thấy một giải pháp, tks để @ 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

Bạn đang sử dụng trình duyệt nào? – ombre42

+0

Tôi đang sử dụng Chrome. – Raphael

Trả lời

10

Giải pháp này rất duyệt cụ thể. Đối với Chrome, bạn có thể cho Chrome biết nơi tải tệp xuống. Chọn thư mục mới cho phép bạn theo dõi trạng thái tải xuống. Ngoài ra, kể từ khi bạn tải xuống một tệp PDF, việc tắt plugin PDF là cần thiết để ngăn tệp PDF hiển thị thay vì tải xuống. Đây là một thử nghiệm đã hoạt động trên máy tính của tôi bằng cách sử dụng một trang đơn giản và tệp 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} 

Nội dung download.html:

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

IE có thể thực hiện điều đó không? – user2520217

0

Bạn cần phải kiểm tra MD5 của tập tin - trước khi tải về và sau khi download. Cả MD5 đều giống nhau.

tập tin Giả sử là trên máy tính Linux - trước và sau khi tải về:

  1. Đăng nhập vào máy linux sử dụng SSH thư viện
  2. Chạy lệnh: # md5sum PATH_TO_FILE
  3. Store đầu ra trong một biến.
  4. So sánh giá trị biến

Hy vọng thông tin này hữu ích.

Các vấn đề liên quan