File Upload in Selenium Robot Framework
In Robot Framework, you can upload files using SeleniumLibrary's Choose File keyword. It allows you to upload files such as PDF, Excel, CSV, images, Word documents, and text files through an HTML file input element.
1. Install Required Libraries
Install Robot Framework and SeleniumLibrary using the following commands:
pip install robotframework
pip install robotframework-seleniumlibrary
2. Basic File Upload Example
Consider the following HTML file upload element:
<input type="file" id="uploadFile" name="uploadFile">
Robot Framework test case:
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://example.com/upload
${FILE_PATH} ${CURDIR}/testdata/sample.pdf
*** Test Cases ***
Upload File
Open Browser ${URL} chrome
Maximize Browser Window
Choose File id=uploadFile ${FILE_PATH}
Sleep 2s
Close Browser
Explanation:
Choose Fileselects a file from the local system.id=uploadFileidentifies the HTML file input element.${FILE_PATH}specifies the file path.
3. Upload Different Types of Files
You can upload different file formats using the same Choose File keyword.
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${UPLOAD_URL} https://example.com/upload
*** Test Cases ***
Upload PDF File
Open Browser ${UPLOAD_URL} chrome
Choose File id=fileUpload ${CURDIR}/files/sample.pdf
Close Browser
Upload Excel File
Open Browser ${UPLOAD_URL} chrome
Choose File id=fileUpload ${CURDIR}/files/data.xlsx
Close Browser
Upload Image File
Open Browser ${UPLOAD_URL} chrome
Choose File id=fileUpload ${CURDIR}/files/image.png
Close Browser
Upload CSV File
Open Browser ${UPLOAD_URL} chrome
Choose File id=fileUpload ${CURDIR}/files/data.csv
Close Browser
4. Upload Multiple Files
If an HTML file input supports multiple file selection, you can upload multiple files by separating their absolute paths with a newline (\n).
HTML:
<input type="file" id="uploadFiles" multiple>
Robot Framework:
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${FILE1} ${CURDIR}/files/sample1.pdf
${FILE2} ${CURDIR}/files/sample2.pdf
${FILE3} ${CURDIR}/files/sample3.pdf
*** Test Cases ***
Upload Multiple Files
Open Browser https://example.com/upload chrome
Choose File id=uploadFiles ${FILE1}${\n}${FILE2}${\n}${FILE3}
Close Browser
Important: The HTML input must contain the multiple attribute to support multiple file uploads.
5. Upload File Using XPath
You can also locate the file input element using XPath.
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
Upload File Using XPath
Open Browser https://example.com/upload chrome
Choose File xpath=//input[@type='file'] ${CURDIR}/files/sample.pdf
Close Browser
6. Upload File and Click Submit Button
Consider a file upload form with a submit button.
HTML:
<input type="file" id="fileUpload">
<button id="submitBtn">Upload</button>
Robot Framework:
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
Upload And Submit File
Open Browser https://example.com/upload chrome
Maximize Browser Window
Choose File id=fileUpload ${CURDIR}/files/sample.pdf
Click Button id=submitBtn
Wait Until Page Contains File uploaded successfully
Close Browser
7. File Upload Keywords in SeleniumLibrary
| Keyword | Description |
|---|---|
| Choose File | Selects a file for upload. |
| Get Element Attribute | Retrieves the input value or attribute. |
| File Should Exist | Checks whether the file exists. |
| File Should Not Exist | Checks whether the file does not exist. |
| Get File Size | Returns the file size. |
| Wait Until Element Is Visible | Waits until an element becomes visible. |
| Click Button | Clicks the upload button. |
| Wait Until Page Contains | Waits for confirmation text. |
8. Real-Time Example: Complete File Upload Test
This example combines file existence validation, file selection, submission, and upload confirmation.
*** Settings ***
Library SeleniumLibrary
Library OperatingSystem
*** Variables ***
${URL} https://example.com/upload
${FILE_NAME} sample.pdf
${FILE_PATH} ${CURDIR}${/}testdata${/}sample.pdf
*** Test Cases ***
Verify Complete File Upload
Open Browser ${URL} chrome
Maximize Browser Window
# Verify file exists
File Should Exist ${FILE_PATH}
# Wait for upload element
Wait Until Element Is Visible id=fileUpload
# Select file
Choose File id=fileUpload ${FILE_PATH}
# Verify selected file name
${VALUE}= Get Element Attribute id=fileUpload value
Should Contain ${VALUE} ${FILE_NAME}
# Submit file
Click Button id=submitBtn
# Verify upload success
Wait Until Page Contains File uploaded successfully timeout=15s
Close Browser
9. Choose File vs Upload Using AutoIT
| Feature | Choose File | AutoIT |
|---|---|---|
| File selection | Yes | Yes |
| Windows file dialog | Not required | Interacts with dialog |
| Browser support | Chrome, Firefox, Edge | Windows desktop automation |
| Hidden file input | Often supported | Can interact with dialog |
| External dependency | No | Yes |
| Recommended | Yes | Only when necessary |
Best practice: Always prefer SeleniumLibrary's Choose File keyword instead of automating the Windows file chooser dialog. It directly sets the file on the HTML input type="file" element and is generally more reliable.
Conclusion
Use the Choose File keyword for Selenium Robot Framework file uploads. You do not need to open or interact with the Windows file selection dialog. It is suitable for PDF, Excel, CSV, image, and other supported file formats.

No comments:
Post a Comment