代码之家  ›  专栏  ›  技术社区  ›  pavelicii

如何在Visual Studio Webtest中上载具有唯一名称的文件

  •  1
  • pavelicii  · 技术社区  · 7 年前

    我使用Visual Studio 2015录制了一个web测试,其中包括将文件上载到网站。此网站不允许上载同名文件。在webtest场景中,我有一个 File Upload Parameter 使用该属性 Generate Unique Name 设置为 True :

    enter image description here

    但每次我尝试运行此webtest时,在尝试上载文件时都会失败,因为具有给定名称的文件已经存在。

    在我的项目文件夹中,我选中了TestResults文件夹,它复制了我需要上传的文件,文件名与记录的文件名相同,而不是给它一个唯一的名称。

    我看到了一些关于这个问题的建议,并尝试检查 Enable deployment my中的选项 .testsettings 文件,并在此检查下的“要部署的文件”部分中添加此文件的确切路径。但这仍然不能解决问题。

    UPD。调试单个webtest运行。

    请求: enter image description here 答复:

    标题:

    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
    

    正文:

    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                                    ......"}   
    

    细节: enter image description here

    2 回复  |  直到 7 年前
        1
  •  2
  •   Falco Alexander    7 年前

    如果你以毫秒为单位上传许多文件,你可能会遇到麻烦,是吗?

    供您参考,这是生成唯一文件名的实际代码,它使用毫秒分辨率的时间戳。

    <!-- 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;
        }
    

    我试图重现你的错误,但它的表现与预期相符。 我所做的唯一更改是在PostParameter中设置一个绝对文件路径 FileName 使其运行,因为未将其正确复制到MSTest的Out文件夹。

    能否显示单次运行webtest的更详细调试?

    请检查表单post参数,它应该如下所示:

    enter image description here

    和:如果生成唯一名称,请将文件上载名称保留为空:

    enter image description here

        2
  •  0
  •   pavelicii    6 年前

    想出了一些解决办法。我从中生成了代码。webtest和Add Guid.NewGuid() 价值 fileName 参数因此,生成Form Post参数的代码如下所示:

    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;
    

    做了一个。基于此编码的负载测试。webtest,它最终开始在loadtest期间上载的文件的名称中添加GUID。 也许这不是最好的解决方案,但尝试删除 文件名 参数,更改 Name 参数值来自 null 去做其他事情等都没用。