问题
I have some difficulties understanding how the access method "Do not move cursor automatically" works for a web test data source. And the documentation out there seems to be far from exhaustive.
First of all, let's assume there is a simple web test with only one request. It uses a data source called DS with a table called StepList, that contains 5 rows.
The Access Method for the table is set to "Do not move cursor automatically" and the current test settings have "One run per data source row" enabled in the web test properties. The web test is not part of a load test.
In these conditions, the test is run 5 times, for each row, so it basically moves the cursor automatically. Is this intended?
Now, on to the practical question, which is the subject of this post.
My StepList table above has, among others, an identity column and a StepId column. The web test in question is actually called from another web test, and it's supposed to run only for the table rows that have a StepId set in the test context.
Id StepId
1 1
2 1
3 2
4 2
5 2
In order to achieve this, I followed the guidelines from here and wrote a web test plugin with the PreWebTest method looking like this:
public override void PreWebTest(object sender, PreWebTestEventArgs e)
{
string stepId = string.Empty;
if (e.WebTest.Context.ContainsKey(ContextParameterName))
{
stepId = e.WebTest.Context[ContextParameterName].ToString();
}
while (e.WebTest.Context["DS.StepList.StepId"].ToString() != stepId)
{
e.WebTest.MoveDataTableCursor("DS", "StepList");
}
}
The code above seems to work properly on the example table only if the stepId has the value "2". If it's "1", then MoveDataTableCursor throws WebTestEndOfDataException exception: "No more rows of data in a data source table with AccessMethod Unique".
I tried catching the exception and stopping the web test, but this doesn't work, as the test seems to never end.
回答1:
To answer your first question: the setting "Do not move cursor automatically" applies to runs within a load test only, which is why the cursor still moves automatically during a web test where you are using the setting "One run per data source row". Of course, the latter setting applies to web test runs only.
The reason you are getting the WebTestEndOfDataException is:
- The way "One run per data source row" is implemented by the Visual Studio test engine includes the fact that there is only one data cursor for all five tests; i.e. the state of the cursor is remembered between all 5 tests.
- Each test iteration automatically increments the data cursor by itself.
- Your plugin advances the data cursor within this set of 5 tests, necessarily exhausting the available rows before the 5 tests have completed.
The simplest way to make your test work is to manually set the number of iterations instead of using "One run per data source row". For example, there are only two valid rows in your data, so set the iterations to 2. However, this is also a bad solution because you would have to manually count rows and change the value.
The next best way is to change your WebTestPlugin such that all it does is check the value of DS.StepList.StepId and do WebTest.Stop() if it doesn't match the desired value. This way, the unwanted iterations will terminate immediately. However, they will still waste time and add visual clutter.
If the parent test should always run, and it is only the called test that should be skipped, then wrap the "Call to WebTest" in a "String Comparison" Conditional Rule that checks the value of DS.StepList.StepId against the stepId context parameter, thus only calling the child test when the StepId is correct.
来源:https://stackoverflow.com/questions/9704992/how-to-call-movedatatablecursor-in-a-web-test-plugin-to-go-through-a-limited-set