In Web Tests, How to Validate the values on a grid ?
1) Create a test project If you need to extract a value from one reqeust and access it in another request, following similar steps, except implement the extraction rule class. In the extraction rule, add the value you want to access to to the context. The context is available off of the WebTest property of the event args class. Anything you add to the context is available to future requests.
2) Add a class library project to the solution with the test project.
3) In the class library project add a reference to the Microsoft.VisualStudio.QualityTools.WebTestFramework.dll. This can be found on the .NET tab of the Add Reference Dialog.
4) In the class in the Class Library project add a using statement for Microsoft.VisualStudio.QualityTools.WebTestFramework
5) Have the class extend the ValidationRule class.
6) Right Click on ValidationRule and choose, Implement Abstract Class. This will create the method that you need to implement which is the Validate method.
7) One of the properties of the ValidationEventArgs class which is passed into the validate method is the Response. This object gives you access to the HTML Response. You can get the response as a string and then parse it to find the values you are looking for or you can access the tags collection and pull out the html tags you are interested in.
8) In the Validate method, set the IsValid property to indication success or failure for the rule.
9) Then add the class library project as a reference to the Test Project.
10) Find the request in your web test which you want to validate data for and right click on the request and choose add validation rule.
11) Select your rule and choose OK.
12) Play back the test to verify the rule did what you expected.
13) As you are developing the rule, it is useful to run the web test under the debugger. You can add break points in your rule and then run the web test under the debugger from the test view window. Open this window by Clicking the top level Test menu -> Windows - > Test View. Your test should appear in this window. Select your test and then right click on it and choose Debug Selection.
Hi,
Can someone point me to a code example? I'm really new to this stuff and need some help. :)
Thank you.
Hi, Slumley.
I can create a custom validation rule. I was hoping someone will have an example of a custom validation rule, validating a value in a grid control or any other type of control, so I can see how it's done. Can you point me to such example?
Thank you.
Hi, all.
Can someone point me to a code sample where a control value is being validated?
Any help will be really appreciated.
Thank you.
All,
I tried the following steps, but still i am not able to find an extra validation rule getting added.
The code which i have added is
===================================
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Text;using
Microsoft.VisualStudio.TestTools.WebTesting;using
Microsoft.VisualStudio.QualityTools.WebTestFramework;namespace
OcracokeSamples{
public class ValidateResponseUrl : ValidationRule{
private string _urlStringToFind = String.Empty; private bool _failIfFound = true;[Description(
"If true, validation fails if the specified string is found in the response URL. If false, validation fails if the specified string is not found in the response URL.")] public bool FailIfFound{
get { return _failIfFound; } set { _failIfFound = value; }}
[Description(
"The string to search for in the response URL. For example, enter 'Error.aspx' if you want to make sure the server does not redirect to that error page.")] public string UrlStringToFind{
get { return _urlStringToFind; } set { _urlStringToFind = value; }}
public override string RuleName{
get { return "Validate Response URL"; }}
public override string RuleDescription{
get { return "Verifies the response URL. This rule can be used to make sure a redirect to an error page does not occur, for example."; }}
public override void Validate(object sender, ValidationEventArgs e){
//make sure the string to find has a value if (String.IsNullOrEmpty(_urlStringToFind)){
throw new ArgumentException("The UrlStringToFind property cannot be null or empty string.");}
bool found = e.Response.ResponseUri.OriginalString.IndexOf(_urlStringToFind, StringComparison.OrdinalIgnoreCase) != -1; string foundMessage = found ? "was" : "was not"; //set the result message that will appear in the web test viewer details tab and the load test error tablee.Message =
String.Format("The string '{0}' {1} found in the response URL.", _urlStringToFind, foundMessage); //set whether the validation passed or failede.IsValid = found != _failIfFound;
}
}
}
======================================
When i build the solution the message what i got is below
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Vbc.exe /noconfig /imports:Microsoft.VisualBasic,System /rootnamespace:TestProject10 /doc:obj\Debug\TestProject10.xml /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,PLATFORM=\"AnyCPU\"" /reference:..\..\..\..\..\..\..\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.UnitTestFramework\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll,"d:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.WebTestFramework.dll",C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll,..\..\TestProject11\TestProject11\bin\TestProject11.dll,..\..\TestProject8\TestProject8\bin\TestProject8.dll /debug+ /debug:full /out:obj\Debug\TestProject10.dll /target:library "My Project\AssemblyInfo.vb" UnitTest1.vb WebTest1Coded.vb WebTest2Coded.vb
TestProject10 -> C:\Documents and Settings\jrajeev\My Documents\Visual Studio 2005\Projects\TestProject10\TestProject10\bin\TestProject10.dll
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
Please let me know what am i missing here, or in case you need some more details
For a custom validation or extraction rule to show up in the rules dialog, there must be reference in the project containing the web test to the assembly containing the rule. Also, the rule must be declared as a public class (which is the case in the code you posted).
You rule you posted is C# and the Web tests are VB. So, you'll need a reference in the VB project to the assembly containing the rule.
Thanks,
Rick
Thanks Rick,
I have added the code in C# and also the web test in C#.
Even then i was not able to find the extra validation rule.
I believe when we add reference to the C# code from the webtest it should create a reference to the web test and the C# code, let me know if my understanding is correct.
If not then i like to get how to do the following :
"For a custom validation or extraction rule to show up in the rules dialog, there must be reference in the project containing the web test to the assembly containing the rule."
Thanks for your support
Here's one example...
A solution contains two projects, one project is a class library project which contains a class implementing a custom validation rule (RuleProject). The custom validation rule class must be declared as public and extend ValidationRule. The other project is a test project containing a web test (WebTestProject).
To add the validation rule from RuleProject to the web test in WebTestProject, you would need to add a project refernce for RuleProject to WebTestProject (right click on the references node and choose add reference; then choose Projects tab and select RuleProject). You should now see the custom validation rule listed in the dialog when you attempt to add a validation rule to a web test request.
Note, if the custom validation rule resides in the same project as the web test, there should be no need for the project reference. The rule should appear in the dialog as long as it is declared as public.
Does this help?
Thanks,
Rick