0

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.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • What happened when you tried (2)? I have not had any problems doing what you describe. Context parameters (CPs) work fine in the URL field. Data source values are automatically included in the CPs so I do not understand your (1). Please [edit] the question to include the actual text of the URI (from the ".webtest" file) and also the URI that is actually sent (from the results window). Include the details as text, not as pictures. – AdrianHHH Jun 13 '19 at 16:27
  • You have not stated what is not working. If the datasource values are not visible in the CPs then perhaps you need to set the "Select columns" property of the data source to "Select all columns". – AdrianHHH Jun 13 '19 at 16:35
  • @AdrianHHH In first appraoch: Context parameters when i provide as below:`'https://xyz/api/v1.0/objects/{{t1}}/models`' and in context parameter i gave: "some value" for `t1`, it works fine.When i give in context parameter `DataSource1.Table5002#csv.model` for `t1`, it doesn't take form data source and while running it url is shown like `https://xyz/api/v1.0/objects/DataSource1.Table5002#csv.model/models`.When i give my URL like this:`https://exampl/api/{{DataSource1.Table5002#csv.objectId}}/mode/{{DataSource1.Table5002#csv.model}}`, now it is working fine. – DreamsOnCloud Jun 17 '19 at 05:28
  • I tried like this way exampl`/api{{DataSource1.Table5002#csv.objectId}}/mode/{{DataSource1.Table5002#csv.model}}` and dint know for what reasons it dint work(while running, the url was showing as `https://exampl/api/{{DataSource1.Table5002#csv.objectId}}/mode/{{DataSource1.Table5002#csv.model}}`, it was not able to take from datasource).I changed my machine , and same code worked fine in another machine.Imported that code again in my machine and magically it is working fine in my machine now. – DreamsOnCloud Jun 17 '19 at 05:29
  • @AdrianHHH can we connect offline so that i can share my project – DreamsOnCloud Jun 17 '19 at 08:26
  • If the problem has been resolved then I do not see how sharing the project would help. The purpose of SO is have a Q&A site for the benefit of the community. I think it is better to allow the community access to any communications regarding this question and answer. There is a chat facility within Stack Overflow which allows for an extended conversation. – AdrianHHH Jun 17 '19 at 12:02

2 Answers2

0

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

  • The URL in this answer is identical to the URL given in the question. Something else must have changed. – AdrianHHH Jun 17 '19 at 11:56
0

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.

Charlesdwm
  • 71
  • 5