Visual Studio 使用CSV文件更改Web测试URL

lf3rwulv  于 2022-12-14  发布在  其他
关注(0)|答案(2)|浏览(118)

在Visual Studio Web测试中,我的URL是

https:example//api/{{test1}}/mode/{{test2}}

这里我想从CSV文件传递test1test2的值。

https://exampl/api/{{DataSource1.Table5002#csv.objectId}}/mode/{{DataSource1.Table5002#csv.model}}

table5002中,列objectIdmodel相加。当我在字符串体中使用CSV中的值时,它们工作正常。
我试过这些:

  1. Context参数,这里我无法将context参数与数据源绑定。
    1.尝试在URl中提供https://exampl/api/{{DataSource1.Table5002#csv.objectId}}/mode/{{DataSource1.Table5002#csv.model}}。这不会从数据来源取得值。
    请帮助我如何在URL中使用CSV值。
muk1a3rh

muk1a3rh1#

当我这样给予我的URL时:https://exampl/api/ {{数据源1.表5002#csv.objectId}}/模式/{{数据源1.表5002#csv.model}},现在它工作正常。

lmvvr0a8

lmvvr0a82#

下面是一个更复杂的插件来做到这一点:

/// <summary>
    /// https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.openmappedexeconfiguration?view=windowsdesktop-7.0
    /// https://stackoverflow.com/questions/32439489/how-to-change-the-web-performance-test-execution-path
    /// </summary>
    [System.ComponentModel.Description("Creates a string based on the current date, time, and sequential increment. Useful to avoid duplicates when creating many entities of the same type.")]
    public class PluginWtConfigReader : WebTestPlugin
    {      
        static string _webServer = "webServer not set";
        static string executingDir = Environment.CurrentDirectory; // usually puts us in the TestResults directory
        static string solutionRoot = Directory.GetParent(executingDir).Parent.Parent.Parent.Parent.FullName; // back out to root 

        [System.ComponentModel.Description("Name of the Config file E.g. Best stored in current project.")]
        [System.ComponentModel.DefaultValue("ConfigFileName.config")]
        public string configFileName { get; set; }       

        public override void PreRequest(object sender, PreRequestEventArgs e)
        {
            string pathToTargetConfigFile = "";
            try
            {
                string[] fileArray = Directory.GetFiles(solutionRoot, configFileName, SearchOption.AllDirectories);
                pathToTargetConfigFile = fileArray[0];
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Web Test Plugin: Could not find config file. Did you populate the plugin property?");
            }

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap()
            {
                ExeConfigFilename = pathToTargetConfigFile
            };

            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                        
            _webServer = config.AppSettings.Settings["webServer"].Value.ToString();

            string fullOldUrl = e.Request.Url;
            Uri newUri = new Uri(fullOldUrl);
            string oldHost = newUri.DnsSafeHost; // gets just a segment of the URI, just the host.
            e.Request.Url = e.Request.Url.Replace(oldHost, _webServer);

}

此插件允许您在app.config文件中指定URL。将其添加到webtest中,它只会更改URL的主机名部分。您可以编写更多代码来获取要替换的URL的不同段。

相关问题