How to upload a file with unique name in Visual Studio Webtest

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

visual-studio
load-testing
web-testing
webtest
asked on Stack Overflow Mar 4, 2018 by pavelicii • edited Mar 6, 2018 by pavelicii

2 Answers

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

answered on Stack Overflow Mar 4, 2018 by Falco Alexander • edited Mar 6, 2018 by Falco Alexander
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.

answered on Stack Overflow Mar 19, 2018 by pavelicii • edited Mar 19, 2018 by pavelicii

User contributions licensed under CC BY-SA 3.0