DreamsOnCloud
DreamsOnCloud

Reputation: 15

Use CSV file to change URL for web test

In Visual Studio Web test my URL is

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

Here I want to pass values of test1 and test2 from a CSV file. I tried

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

where in table5002, columns objectId and model are added. Values from CSV work fine when I use them in string body.

I tried these:

  1. Context parameters, here I can't bind context parameters with datasource.

  2. Tried giving https://exampl/api/{{DataSource1.Table5002#csv.objectId}}/mode/{{DataSource1.Table5002#csv.model}} in URl. This doesn't take values from data source.

Please help me on how to use CSV values in URL.

Upvotes: 0

Views: 346

Answers (2)

Charlesdwm
Charlesdwm

Reputation: 71

Here is a more sophisticated plugin to do that:

    /// <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);

}

This plug in allows you to specify the URL in an app.config file. Add it to a webtest and it changes just the hostname portion of the URL. You can write more code to get at different segments of the URL you want to replace.

Upvotes: 0

DreamsOnCloud
DreamsOnCloud

Reputation: 15

When i give my URL like this:https://exampl/api/{{DataSource1.Table5002#csv.objectId}}/mode/{{DataSource1.Table5002#csv.model}}, now it is working fine.

Upvotes: 0

Related Questions