Forum Discussion

aceishigh's avatar
aceishigh
Contributor
13 years ago

How to Upload a File using Scripting?

Hi There,

I'm trying to automate a File web control. When I click Upload, it opens a file upload screen with a browse button. I browse to the file I want, select it, click OK and the file is uploaded. I'm using IE8 browser and TestComplete 8.5.

I've been trying to do this with xpath and the Text value but this hasn't worked.

Can you tell me what I need to do?

Many Thanks for your time.

J.



HTML:

<input type="file" style="width:85%" size="50" name="extension" id="extension.file.input">



SCRIPT:

BrowseObj = page.EvaluateXPath("//*[@id='extension.file.input']")

BrowseObj(0).Text = "C:\Users\automation\Desktop\VSE 8.8\VIRUSCAN8800.zip"

2 Replies

  • ArtemS's avatar
    ArtemS
    SmartBear Alumni (Retired)
    Hello,

    Unlike other INPUT controls, the values of file controls (<input type="file".../>) in most browsers cannot be programmatically assigned via the Text, Value or any other properties. This is done intentionally as a security precaution.

    The only possible workaround is to simulate the same actions a regular user would perform: click the Browse button, type in the file name in the ensuing Open File Dialog, click Open and then click Submit. A TestComplete script performing these actions would look similar to this:



    function UploadTest()

    {

      var iexplore;

      var dlgChooseFileToUpload;

      var page;

      iexplore = Sys.Process("iexplore", 2);

      page = iexplore.ToUrl("MyPage.htm");

      // Coordinate-based click over the Browse button.

      page.File("extension_file_input").Click(1005, 5);

      // Perform actions in the dialog

      dlgChooseFileToUpload = iexplore.Window("#32770", "Choose File to Upload");

      dlgChooseFileToUpload.Window("ComboBoxEx32").SetText("C:\\Users\\automation\\Desktop\\VSE 8.8\\VIRUSCAN8800.zip");

      dlgChooseFileToUpload.Window("Button", "&Open").ClickButton();

      // Initiate the upload

      page.Button("Submit").Click();

    }



    The drawback of this approach is that the Open File Dialog depends on the browser and OS versions. Besides that, since IE8, the dialogs are created in the helper iexplore processes, whose index may change depending on the order the tested page was opened. Note the index = 2 in Sys.Process("iexplore", 2).  



    I hope this workaround is enough reliable for you. Artem.

  • Thanks a lot for the suggestion. For my needs, I eventually came up with the following solution. It works fine on IE8 and Win 2003 server. Haven't tested it yet on Firefox or other systems.



    ****************************************

    Set Page = iexplore.Page("*")

    ' Click on Install button

    InstallObj = page.EvaluateXPath("//*[@id='install.button']")

    InstallObj(0).Click

    ' Double click on file input to add a file

    BrowseObj = page.EvaluateXPath("//*[@id='file.input']")

    BrowseObj(0).DblClick

    ' Add path to the file using sendkeys

    Sys.Desktop.Keys("C:\Users\me\Desktop\myzip.zip")

    Sys.Desktop.Keys("[Enter]")

    aqUtils.Delay 2000

    ' Click the OK button

    OKObj = page.EvaluateXPath("//*[@id='box.ok']")

    OKObj(0).Click

    OKObj(0).Click

    *****************************************