This blog post will describe the enhancements that have been made to the data source functionality in web tests.  We have heard the following feedback a number of times:

  1. I would like to be able to reload the data source while a load test is running
  2. I want to be able to use a column in my data source that is not bound to any parameter in my web test
  3. I want more control over how the data source is advanced.

We have provided enhancements to each of these areas.

  1. Reloading a data source – Prior to this change, all data for the data source was read once at the beginning of the test.  Now this will allow you to programmatically reload a data source while a test is running.  So if you have a set of web tests that are creating data that other tests need, you will now be able to pull that data in while the test is running.  The way to accomplish this is by using a WebTestPlugin.  The API call is the following:  void WebTest.ReloadDataTable(string dataSourceName, string dataTableName)   Here is a very simple plug-in that would call this.  I have a csv data source called DataSource1 and the table to Products#csv.  
  2. using Microsoft.VisualStudio.TestTools.WebTesting;

    namespace TestProject4
    {
        public class DatasourcePlugin : WebTestPlugin
        {
            public override void PreWebTest(object sender, PreWebTestEventArgs e)
            {
                e.WebTest.ReloadDataTable("DataSource1", "Products#csv");
            }
        }
    }

  3. Using a column in data source not bound to a parameter – This is useful if you are binding some parameter to a column but then want to use another column in a plug-in or custom validation rule.  Prior to this change, you would have to bind all columns that you were going to use to some parameter in the web test.  Now you just do the following. 
    • Expand the data sources node in the web test
    • Click on the table that you want all the data from
    • Bring up the property browser
    • Change the Select Columns property from “Only Select Bound Columns “ to ”Select All Columns”.   This will then add all of the columns into the context and make them available for use in your custom plug-ins and rules.
  4. Controlling how data source is advanced – In VSTS 2008 and VSTS 2005 we had 3 access methods for data sources: Sequential, Random and Unique.  Check out this blog post for a better description of these: Access Methods  We have now added “Do Not Move Cursor Automatically”  Basically, you now have full control on when the cursor is advanced.  If you do nothing, the cursor stays on the first row.  To move the cursor, you will use the following API call: void WebTest.MoveDatatableCursor(string dataSourceName, string dataTableName).  We have also added another overload of this method which is void WebTest.MoveDatatableCursor(string dataSourceName, string dataTableName, int newRowIndex).  Here is a very simple plug-in which will move the cursor to row 3 in the data source. 
  5. using Microsoft.VisualStudio.TestTools.WebTesting;

    namespace TestProject4
    {
        public class DatasourcePlugin : WebTestPlugin
        {
            public override void PreWebTest(object sender, PreWebTestEventArgs e)
            {
                e.WebTest.MoveDataTableCursor("DataSource1", "Products#csv", 3);
            }
        }
    }

    Hopefully these enhancements will make it easier to work with data sources.  These are now available in beta 2.  Please try them out and let us know what you think.