Upload a file in browser using Robot Framework Test

27,665

As per the documentation given in the Selenium2library, the syntax to use use the Choose File keyword is

Choose File  Locator  File_name

For a dialog or input that WebDriver can interact with the example would do something like this

Choose File id=fileToUpload  C://Downloads/Demo/Abc.txt

Here C://Downloads/Demo/Abc.txt is the location on the system where the file is kept. Change this to your own file location.

Also, I remember this solution also, which ultimately worked for me too - SO Post about uploading from a Windows directory. You can also try this one.

Share:
27,665
Admin
Author by

Admin

Updated on February 23, 2020

Comments

  • Admin
    Admin over 3 years

    I'm writing Test Cases using Robot Framework and Selenium for my Web application. I tried to Upload a file, but I can't its failing.

    My Code is

    *** Variables ***
    ${TVAURL}     http://localhost:1500/
    ${Browser}    Firefox
    
    TC_01: Enter into the application
        [Documentation]   Enter into the application to upload a file
        Open Browser   ${TVAURL}     ${Browser}
        maximize browser window
        Choose File ........
    

    HTML File:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Upload File</title>
    </head>
    <body>
    
    <form action="upload.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    
    </body>
    </html>
    

    I need to know how to use this Choose File for this file upload test. I don't know how to do this. I need to upload a file automatically without any third party tools like Autoit, etc.,

    I referred the following http://robotframework.org/Selenium2Library/Selenium2Library.html#Choose%20File

    Moreover I referred the following questions too

    But I can't get any solutions. Kindly assist me how to do this using Robot Framework, Selenium in Pycharm Studio.

    Reply for Answer #1: @demouser123

    Still I'm having issue, here with I have attached the Screen shot

    enter image description here

    It always opens the Desktop, and fails. Kindly assist me.

    I trided the following code

    *** Variables ***
    ${PO_AddShell}     //*[@id="fileToUpload"]
    
    click button  ${PO_AddShell}
    Choose File   ${PO_AddShell}    E://Project/Publish/SampleTest.1500/rose.jpg
    
  • Admin
    Admin over 6 years
    Its opening the file open dialog but its not selecting the file and not clicking the Open button.
  • demouser123
    demouser123 over 6 years
    what is the path and the locator used by you?
  • Admin
    Admin over 6 years
    Choose File xpath=.//div/input .${CURDIR}\rose.jpg
  • Admin
    Admin over 6 years
    The said picture is exist in the hosted root (i.e., http://localhost:1500/rose.jpg) - Its always opens the desktop and its not hitting the Open button automatically.
  • Admin
    Admin over 6 years
    I getting the following error too Element locator 'xpath=.//div/input' did not match any elements.`
  • demouser123
    demouser123 over 6 years
    The locator that I have mentioned .//div/input is a dummy locator. You need to use the locator that you are getting on your webpage. Please us that locator.
  • Admin
    Admin over 6 years
    I can't able to get you, that file browser is a default feature of the browser how could I get the locator from that. Could you please help me. I updated the question with a sample HTML.
  • demouser123
    demouser123 over 6 years
    Your path needs to be E://Project/Publish/SampleTest.1500/rose.jpg. The file browser is opening which means that the Close File Command and the locator are correct. You need to give the correct file location and it would work.
  • Admin
    Admin over 6 years
    yes, I trided the both version E://Project/Publish/SampleTest.1500/rose.jpg and E://Project/Publish/SampleTest.1500/rose.jpge. But its failing to locate the file, its opening the Desktop. Kindly see dialog box in the screenshot.
  • Admin
    Admin over 6 years
    Yes, not its working E:\\Project\\Publish\\SampleTest.1500\\rose.jpg. But the file browser is not closing.
  • demouser123
    demouser123 over 6 years
  • Auto-learner
    Auto-learner over 6 years
    Hello @Mastero , There are two ways you can solve this issue , one is by going with choose file and selecting the image/file . The other way is set your browser download preferences to your filepath and use sikuli to select the image. Sikuli is nice library if you want to interact with GUI , you can capture a screenshot and use click method. Please let me know if you are still stuck with issue
  • Admin
    Admin over 6 years
    Thanks for your answer. I'm in weekend, so I will test this code on coming Monday...
  • Todor Minakov
    Todor Minakov over 6 years
    Sikuli is a working solution for limited local testing, but it will not work using headless browser (phantoms, chrome in headless mode), nor remote selenium - selenium hub, or the cloud's browserstack, sauce labs, etc. Thus I would not recommend setting this hard dependency in the tests.
  • Auto-learner
    Auto-learner over 6 years
    Thanks @Todor.. I am not aware of this.. is there any alternate option if I want to use sikuli in headless mode..
  • Todor Minakov
    Todor Minakov over 6 years
    Nope, none that I'm aware of; that's the point of the "headless" - there's no UI to interact with, nothing for a person/sikuli to click or send keystrokes to :). Thus using RF's native Choose File is usually the way to go - what it actually does is sending through SE the path as keys to the input field. Which, on the other hand has issues with html5 fancy js uploaders - but that's another topic :)
  • Auto-learner
    Auto-learner over 6 years
    OK Thanks for providing info.
  • Todor Minakov
    Todor Minakov over 6 years
    But it's not the headless mode - rather the remote selenium execution, that is the main driving force for loose copling of tests with the actual operation (the upload in this case). At a certain point the quantity of the cases for the SUT will be too high for serial execution, and you would want to parallelize it (my current UI tests take 40+ hours if ran in serial...). Also with remote exec you easily have mixed browser coverage - osx + safari and win10 + opera for example. This in fact is the main driver for selenium hub, correspondingly all cloud solutions based on it.
  • Todor Minakov
    Todor Minakov over 6 years
    Sorry if these comments sound like a lecture, looks like I'm in that mood :D
  • Admin
    Admin over 6 years
    I need solution for the following question stackoverflow.com/questions/44626501/… Kindly assist me.