1

Using Visual Studio 2015 I've recorded a web test which includes uploading a file to the website. This site doesn't allow to upload files with the same name. In webtest scenario I have a File Upload Parameter with the property Generate Unique Name set to True:

enter image description here

But every time I try to run this webtest it will fail when trying to upload a file, because file with given name already exists.

In my project folder I checked TestResults folder, which copies the file I need to upload with the same name it was recorded, rather then giving it a unique name.

I've seen some suggestion about this problem and tried to check Enable deployment option in my .testsettings file among with adding the exact path to this file in the Files to deploy section under this check. But still it doesn't solve the problem.

UPD. Debug of a single webtest run.

Request: enter image description here Response:

Headers:

HTTP/1.1 500 Internal Server Error
X-AspNetMvc-Version : 5.2
Persistent-Auth : true
Content-Length : 312
Cache-Control : private, s-maxage=0
Content-Type : application/json; charset=utf-8
Date : Tue, 06 Mar 2018 14:42:48 GMT
Server : Microsoft-IIS/8.5
X-AspNet-Version : 4.0.30319
X-Powered-By : ASP.NET

Body:

0x00000000  7B  22  63  6F  6E  66  69  72  6D  61  74  69  6F  6E  22  3A    {"confirmation":
0x00000010  7B  22  6D  65  73  73  61  67  65  22  3A  22  D0  94  D0  BE    {"message":"....
0x00000020  D0  BA  D1  83  D0  BC  D0  B5  D0  BD  D1  82  20  D1  81  20    ............ .. 
0x00000030  D0  B8  D0  BC  D0  B5  D0  BD  D0  B5  D0  BC  20  5C  75  30    ............ \u0
0x00000040  30  32  37  32  6D  62  2E  70  64  66  5C  75  30  30  32  37    0272mb.pdf\u0027
0x00000050  20  D1  83  D0  B6  D0  B5  20  D1  81  D1  83  D1  89  D0  B5     ...... ........
0x00000060  D1  81  D1  82  D0  B2  D1  83  D0  B5  D1  82  2E  20  D0  A1    ............. ..
0x00000070  D0  BE  D0  B7  D0  B4  D0  B0  D1  82  D1  8C  20  D0  BD  D0    ............ ...
0x00000080  BE  D0  B2  D1  83  D1  8E  20  D0  B2  D0  B5  D1  80  D1  81    ....... ........
0x00000090  D0  B8  D1  8E  20  D0  B4  D0  BE  D0  BA  D1  83  D0  BC  D0    .... ...........
0x000000A0  B5  D0  BD  D1  82  D0  B0  3F  22  2C  22  73  68  6F  72  74    .......?","short
0x000000B0  4D  65  73  73  61  67  65  22  3A  22  D0  92  D0  B5  D1  80    Message":"......
0x000000C0  D1  81  D0  B8  D1  8F  20  D0  B4  D0  BE  D0  BA  D1  83  D0    ...... .........
0x000000D0  BC  D0  B5  D0  BD  D1  82  D0  B0  20  D0  BD  D0  B5  20  D0    ......... .... .
0x000000E0  BE  D0  B1  D0  BD  D0  BE  D0  B2  D0  BB  D0  B5  D0  BD  D0    ................
0x000000F0  B0  22  7D  2C  22  6D  65  73  73  61  67  65  22  3A  22  D0    ."},"message":".
0x00000100  92  D0  B5  D1  80  D1  81  D0  B8  D1  8F  20  D0  B4  D0  BE    ........... ....
0x00000110  D0  BA  D1  83  D0  BC  D0  B5  D0  BD  D1  82  D0  B0  20  D0    .............. .
0x00000120  BD  D0  B5  20  D0  BE  D0  B1  D0  BD  D0  BE  D0  B2  D0  BB    ... ............
0x00000130  D0  B5  D0  BD  D0  B0  22  7D                                    ......"}   

Details: enter image description here

pavelicii
  • 1,590
  • 11
  • 21
  • 1
    Several questions: (1) What does *"In my project folder I checked TestResults folder ..."* mean? (2) What information, if any, is is the response header and body? (3) What is shown in the "Detail" tab? (4) After running the test once, what is the name of the file that has been saved on the webserver? – AdrianHHH Mar 05 '18 at 17:10
  • 1
    I suspect that the `Name` parameter may be wrong. Did the name `null` come from a recording? Are you trying to use the C# value `null` (meaning no string)? The `name` field is [described as](https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.webtesting.fileuploadparameter.aspx) *"Gets or sets the parameter name"*. – AdrianHHH Mar 05 '18 at 17:22
  • @AdrianHHH, (1) When you run a webtest Visual Studio creates `TestResult` folder in the project directory with the copy of the file to upload. I expect that file to have unique name with which it will be uploaded. (2) [Response tab](https://i.imgur.com/ZGOMlYA.png) (3) [Details tab](https://i.imgur.com/gm44nxX.png) (4) 2mb.pdf - accordingly to `fileName` parameter. (5) Yes, the name `null` comes from a recording. Now I also understand that it sets the parameter name. But why do I need random parameter name? And what is the proper way to set random **file name** then? – pavelicii Mar 06 '18 at 15:11
  • 1
    I expected you to [edit] the extra details into the question. (1) The `TestResult\...` folder holds the source file for the upload. It does not hold the unique name. That name should be inserted into the upload request. (2 and 3) Pictures are no use, [edit] the text into the question. (4) I did not ask about the parameter. I asked about the file name when you look in the server. (5) It is your website and webtest, how should we know why or even whether it needs a random file name. – AdrianHHH Mar 06 '18 at 16:25
  • @AdrianHHH, (2 and 3) Done. (4) I said you the file name I see when I look in the server. It's `2mb.pdf`. I also mentioned `fileName` parameter because I think this is what determines the name of the file to be uploaded. And I don't know how to make it unique. (5) I indeed need random **file** name. But now for me it looks like some **parameter** name become unique instead and I can't think of a reason for that in any case possible. That's what I meant and of course I could be wrong. – pavelicii Mar 06 '18 at 16:45

2 Answers2

2

You may get into trouble if you upload many files in term as per millisecond, are you?

for your reference, this is the actual code that generates the unique filename, it uses a time stamp with millisecond resolution.

<!-- language: c# -->
internal string GenerateUniqueFileName()
    {
        string fileName = this.FileName;
        if (!string.IsNullOrEmpty(this.FileName))
        {
            string str = Path.GetFileName(this.FileName);
            if (!string.IsNullOrEmpty(str))
            {
                if (!this.m_useGuids)
                {
                    CultureInfo invariantCulture = CultureInfo.get_InvariantCulture();
                    object[] objArray = new object[2];
                    DateTime now = DateTime.get_Now();

                    //--- Here:
                    objArray[0] = now.ToString("ddMMyyyyhhmmssfff", CultureInfo.get_InvariantCulture());
                    objArray[1] = str;
                    str = string.Format(invariantCulture, "{0}{1}", objArray);
                }
                else
                {
                    Guid guid = Guid.NewGuid();
                    string str1 = guid.ToString().Replace("-", string.Empty);
                    str = string.Format(CultureInfo.get_InvariantCulture(), "{0}{1}", new object[] { str1, str });
                }
                fileName = str;
            }
            if (!string.IsNullOrEmpty(Path.GetDirectoryName(this.FileName)))
            {
                fileName = Path.Combine(Path.GetDirectoryName(this.FileName), fileName);
            }
        }
        return fileName;
    }

I tried to reproduce your error, but it behaves as expected. The only change I did was to set an absolute file path in PostParameter FileName to get it running, because it was not copied correctly to the Out Folder for MSTest.

Can you show a more detailed debug of a single run webtest?

please check your form post parameter, it should look like this:

enter image description here

and: leave the file upload name empty if generate unique name:

enter image description here

Falco Alexander
  • 3,092
  • 2
  • 20
  • 39
  • it's not about milliseconds yet. I just try to run it for the second time after it was recorded and it fails on uploading a file. If I go to the website and delete the uploaded file manually, then after that first single test would run successfully and I can see the file was uploaded. Added some debug information to the main post. Is it enough? – pavelicii Mar 05 '18 at 03:47
  • 1
    @PavelNazimok do you upload 2 files? one static filename with "filename" and a unique one with "null"??? see my edit in my answer. – Falco Alexander Mar 05 '18 at 22:23
  • **(1)** No, I only upload one file named 2mb.pdf. `Name: null` was recorded automatically. From [this](https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.webtesting.fileuploadparameter.aspx) MSDN article looks like it's the name of some parameter and not the name of the file. Now I'm a little bit confused... **(2)** Tried your suggestion. Now the file wouldn't upload even for the first time. And `Form Post Parameters` from `Request` tab now only has one parameter `file` with random name (which is `current timestamp + 2mb.pdf`). Before it also had `fileName` parameter. – pavelicii Mar 06 '18 at 14:20
  • And also I don't think that setting an absolute file path is that essential, because after recording the file was copied to the project folder and added to Solution explorer. I tried to delete it from the project - the test fails. Then I changed it to the absolute file path - the test passes, but the name of uploaded file still comes from `fileName` parameter, which is not unique. – pavelicii Mar 06 '18 at 15:18
  • 1
    @PavelNazimok please check my 2nd edit with 2nd screenshot. I still do not sure whether you really need 2 form post parameter entry. both have static upload names. Forget my comment about absolut path when you get no error from the test on runtime – Falco Alexander Mar 06 '18 at 19:13
  • As you can see from main post I have that parameter unfilled. Still no success. – pavelicii Mar 07 '18 at 03:21
  • 1
    but your 1st post parameter entry line still exists? – Falco Alexander Mar 07 '18 at 08:18
  • Do you mean `fileName` parameter? I tried to run test with it and without - no success. And `File Upload Name` parameter was always empty during my attempts. – pavelicii Mar 07 '18 at 08:47
0

Came up with some workaround. I generated code out of my .webtest and added Guid.NewGuid() to the value of fileName parameter. So the piece of code which generates Form Post Parameters looks like this:

FormPostHttpBody request2Body = new FormPostHttpBody();
request2Body.FormPostParameters.Add(new FileUploadParameter("null", "2mb.pdf", "application/pdf", true));
request2Body.FormPostParameters.Add("fileName", Guid.NewGuid() + "2mb.pdf");
request2.Body = request2Body;

Made a .loadtest based on this coded .webtest and it finally started adding Guids to the name of the files which are being uploaded during loadtest. Perhaps it's not the best solution, but attempts to delete fileName parameter, change the Name parameter value from null to something else, etc. didn't help.

pavelicii
  • 1,590
  • 11
  • 21