質問

I have a data driven web performance test in Visual Studio 2012. I want to access the values from the data source within a PreRequest() or a PostRequest() plugin. The values are being accessed already for form post parameters via bindings like {{DataSource1.PaymentAccounts#csv.USER_ID}}.

My end goal is to write the values into a web log comment so the data source values of failed tests are easier to identify. So the values would be passed into a call of e.WebTest.AddCommentToResult(string.Format(...)).

役に立ちましたか?

解決

The values are stored in the text context, so can just access the value with WebTestRequestPlugin code such as:

object contextParameterObject;
if ( e.WebTest.Context.TryGetValue("DataSource1.PaymentAccounts#csv.USER_ID",
                out contextParameterObject) ) {
    string contextParameter = contextParameterObject.ToString();
    e.WebTest.AddCommentToResult(contextParameter);
}
else {
    throw new WebTestException("'DataSource1.PaymentAccounts#csv.USER_ID' not found");
}

By default the context only holds those fields (columns) of the data source that are explicitly used in the web test file. To make all the fields in the datasource available set the Select columns property of the datasource file to Select all columns.

他のヒント

[Description("Captures the datasource value used in a request and writes it to a file.")]
    public class WriteDataSourceValueToFile : WebTestRequestPlugin
    {
        [Description("The name of the file to save the scraped values to. E.g., ResponseTimes.log. ")]
        public string OutputFileName { get; set; }

        [Description(@"Path of the file to save the scraped values to. Format: C:\temp. (Note no trailing backslash.)")]
        public string OutputPathName { get; set; }

        // The LogWriter class is in the main project, not in the utilities project.
        private LogWriter writer = LogWriter.Instance;

        [System.ComponentModel.Description("Name of the datasource.")]
        [System.ComponentModel.DefaultValue("UserIds")]
        public string DatasourceName { get; set; }

        [System.ComponentModel.Description("Name of the CSV file. Use the syntax that Visual Studio uses, with a # sign in place of the period. E.g., UserIds#csv")]
        [System.ComponentModel.DefaultValue("UserIds#csv")]
        public string CsvFileName { get; set; }


        [System.ComponentModel.Description("Field name in the CSV file")]
        [System.ComponentModel.DefaultValue("UserIds")]
        public string FieldName { get; set; }

        public override void PreRequest(object sender, PreRequestEventArgs e)
        {   
        }

        public override void PostRequest(object sender, PostRequestEventArgs e)
        {
            object contextParameterObject;
            if (e.WebTest.Context.TryGetValue(DatasourceName + "." + CsvFileName + "." + FieldName,
                            out contextParameterObject))
            {
                writer.WriteToLog($"Value chosen from {DatasourceName } ={contextParameterObject.ToString()}"  );

            }
            else
            {
                throw new WebTestException(DatasourceName + "." + CsvFileName + "." + FieldName + " not found");
            }

        }
    }

Two crucial Tips:

  1. Beware lack of ANSI! Make sure you have saved the DS file (csv file) as ANSI per this stackoverflow post. Those unicode byte order marks at the beginning will make it difficult to refer to the column name when you write the DS string. "DataSource1.mycsvfile#csv.myDSColumnName" is so much better than: "DataSource1.mycsvfile#csv.myDSColumnName"

Then, in addition to trygetvalue (which is good) you should also simply be able to write:

string currentRowFromDS = e.WebTest.Context["DataSource1.mycsvfile#csv.myDSColumnName"].ToString();
  1. After adding the DS to the webtest, be sure to change the "Select Columns" property to "Select all Columns" (and NOT "select only bound columns"). If you leave it at the default, "select only bound columns," and you have not actually bound the columns yet, Visual Studio will NOT place the row value into the web test. The context parameter will not be there at all.

This behavior is different than a SQL datasource in a unit test for instance, where you simply assign the variable. In general, the API's for adding data to unit tests (Microsoft.VisualStudio.TestTools.UnitTesting) are slightly different than the API's for adding data to a web test Microsoft.VisualStudio.TestTools.WebTesting).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top