Selenium Issues

This forum is for Selenium specific usage or questions

This is a public Discussion Area  publicRSS

Post

    James O'Sullivan
    Uploading files
    Post posted April 7, 2009 by James O'Sullivan
    10542 Views, 4 Comment
    Topic:
    Uploading files
    Body:

    Hi,

    I'm trying to upload a file as part of a test I'm currently writing, but when I replay the test the filename isn't supplied to the file upload control and therefore the required field validation fails when the form submits.  The selenium code generated is as follows:

    selenium.type(ctl00_cphMain_fvSubjectConsent_fuScannedForm, "test.png");

    Any ideas?

    Thanks,

    James

    Comment

    • selvan
      posted April 9, 2009 by selvan

      Normally IE and Firefox doesn't allow any value to be set in file upload field via Javascript (Security restriction)

      If you are using IE and Proxy Injection Mode, you may do as below

          //Put focus on file upload text box
          selenium.focus("upload_field");

          //Type using Robot
          selenium.typeKeysUsingRobot("c:\\some_xml_doc.xml");

          //Submit the Form
          selenium.submit("form_name");


      If you are using Firefox 3 and Proxy Injection Mode (Firfox 3 doesn't allow programmatically set focus / click on file upload field using Javascript),

          //Note x, y values mentioned below are in pixels, denotes width, height offset of
          //"Browse" button of fileupload relative to the screen, not to browser window / browser document)

         // Change these values according to your environment
          int x, y = 500;

          //Instantiate AWT robot
          java.awt.Robot robot = new java.awt.Robot();

          //Move mouse cursor over "Browser" button
          robot.mouseMove(x, y);

          //Press left mouse button
          robot.mousePress(InputEvent.BUTTON1_MASK);

          //Release left mouse button
          robot.mouseRelease(InputEvent.BUTTON1_MASK);

          //Type file name using Robot
          selenium.typeKeysUsingRobot("c:\\some_xml_doc.xml");

          //Press enter using robot
          selenium.typeKeysUsingRobot("\n");

          //Submit the Form
          selenium.submit("form_name");


      If you are using Non-Proxy Injection mode,
      you may just swich your browser type to "*chrome" or "*iehta" in twist.properties
      These mode allow value to be set in file upload field via Javascript and selenium.type will just work fine.

    • Bogdan Gusiev
      posted April 29, 2009 by Bogdan Gusiev

      This way doesn't work for me.

      There is a solution with robot that doesn't use proxy mode.

      Read more here

    • Manish Chakravarty
      posted May 8, 2009 by Manish Chakravarty

      Bogdan,

      While the JInovke way might work (disclaimer: I havent used it yet) , as Selvan said, using the IEHTA or Chrome mode(s) is certainly the better and preferable option

      1. It allows you complete control over the browser, including the file upload filed
      2. You dont have to worry about including JInvoke in your java source file (in Twist) and deal with any complications that come up.
    • Gopi
      posted August 11, 2010 by Gopi

      Manish,Selvan

      I am having trouble in automating a file upload button in IE browser

      I have tried AutoIt, Robot,Javascript , but all in vain!

      I wanted to try  the solution suggested by Selvan , but could not understand the line

      selenium.typeKeysUsingRobot("string"); command

      As far as i knew selenium supports 2 commands tryp and typekeys,

      Could you please let me know how to use this command  and get the file upload ( IE browser) automated ?

      Very much appreciate your help

      BTW Manish, excellent blog post and articles ! Keep it up !

      Thanks

      Gopi