【问题标题】:How do I proccess a string of numbers as string in a VS webtest datasource?如何在 VS webtest 数据源中将一串数字作为字符串处理?
【发布时间】:2017-12-18 16:04:41
【问题描述】:

我有问题。我正在使用 Visual Studio Web 性能测试,并且我有一个 csv 文件,其中包含我需要在 Web 请求的字符串正文中发送的数据。问题是,当 Web 测试从 accountID 检索数据时,它将数据作为 int 而不是字符串。因此,如果帐号是 000005 或 000016,则测试会放置 5 和 15,忽略左侧的零。这让我的网络服务出错。

有没有办法强制 web 测试将所有数据视为字符串?谢谢

您可以在下面看到一个 csv 文件的示例。数据不多,只有 2 列,所以我不想为此创建数据库

AccountsNames, AccountID
Nombre1, 00001
Nombre3, 00002
Nombre4, 00003
Nombre5, 00004
Nombre6, 00005
Nombre7, 00006
Nombre8, 00007

【问题讨论】:

    标签: visual-studio webtest


    【解决方案1】:

    我最终要做的是创建一个 Web 请求插件,从 webtest 上下文中获取数据源并编辑值。

    这是一个代码示例:

    namespace WebTestProjectToPlayArround
    {
        [DisplayName("Add Padding Zeroes")]
        [Description("")]
        public class EditContextParameter : WebTestRequestPlugin
        {
    
    
            [DisplayName("DataSourceContext")]
            [Description("")]
            public string DataSourceContextParam { set; get; }
    
            [DisplayName("ContextParamToStoreValue")]
            [Description("")]
            public string ContextParam { set; get; }
    
    
            public override void PreRequest(object sender, PreRequestEventArgs e)
            {
                base.PreRequest(sender, e);
                var value = (string) e.WebTest.Context[DataSourceContextParam];
                string newValue = "";
                object contextParam;
                string contextName = value.Replace("{","").Replace("}", "");
                if (e.WebTest.Context.TryGetValue(contextName, out contextParam))
                {
                    newValue = contextParam.ToString();
                    newValue = newValue.PadLeft(10, '0');
                }
                e.WebTest.Context[ContextParam] = newValue;
            }
        }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 2020-10-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      相关资源
      最近更新 更多