I am new to the validation rules using VSTS 2008 Test Edition. Any suggestions would be much appreciated.
One of things in my script is to verify a list of values (i.e. Monday – Sunday) is listed in a combo box. How to do it?
Thanks in advance!
HaYin
-------------------------------------------------------------------------------------------------
Yin
The "Find Text" validation rule can solve your problem. It can be used to check whether html code contains a specified string. For example, you can try to create a Find Text rule and set the text to find as some thing like "".
Bill Wang
-----------------------------------------------------------------------------------------------
To check a range of values like this will require a custom validation rule. The following article explains how to create a custom validation rule.
http://msdn.microsoft.com/en-us/library/ms182556(VS.80).aspx
You will be implementing a method called "Validate" in the in the rule. THe rule will set a pass/fail indicator via the ValidationEventArgs parameter. For instance…
public override void Validate(object sender, ValidationEventArgs e)
{
e.Message = "It didn't work";
e.IsValid = false;
}
The validate method will be supplied with the response body as a string and an HTMLDocument via the event args (e.Response.BodyAsString and e.Response.HTMLDocument). The HTMLDocument that is supplied by the response is very limited in what it can provide you. It parses HTML tags out of the response but does not provide any information about HTML structure. It is intentionally lightweight to avoid unnecessary overhead in the web test (especially when the web test may be executions thousands of time during a load test). It is sufficient for many validation scenarios, but not all.
For instance, you could do something like...
public override void Validate(object sender, ValidationEventArgs e)
{
WebTestResponse response = e.Response;
HtmlDocument doc = response.HtmlDocument;
IEnumerable
foreach (HtmlTag tag in tags)
{
foreach (HtmlAttribute attribute in tag.Attributes)
{
if (string.Equals(attribute.Name, "value", StringComparison.OrdinalIgnoreCase))
{
string value = attribute.Value; // note this is the attribute "value" not the inner text of the tag.
}
}
}
}
Again, the HTMLDocument class only finds tags in the response but does not provide structure information. So, in the code snippet above you will not know which combo box each of the options belongs to. If this is sufficient for your needs then you can use the HTMLDocument class.
A colleague of mine is about to post a Validation Rule sample to CodePlex that will demonstrate how to do more complex searches through the HTML using e.Response.HtmlDocument. It would be a sample that you could refer to if needed. There should be an announcement on the forums when this sample is posted.
You could also consider using a full HTML Dom to parse the response and then navigate the document.
Let me know if you need additional information.
Thanks,
Rick
------------------------------------------------------------------------------------------------
This should work however you may need to tweak it depending on your source.
1) Add a Find Text validation rule
2) Change Use Regular Expression to True
3) Plop the following into Find Text
(?=[\w\W]*Sunday)(?=[\w\W]*Monday)(?=[\w\W]*Tuesday)(?=[\w\W]*Wednesday)(?=[\w\W]*Thursday)(?=[\w\W]*Friday)(?=[\w\W]*Saturday)
I tested this in a RegEx tester using the following source:
------------------------------------------------------------------------------------------
Thank you all for such quick response!
Looks like writing custom validation rules will be a common way to verify rules because the out-of-box validation rules are so limited (which is not a good thing for testers, like me, who are not good at writing code).
For this case, I tried to use Lewis’ suggestion and it worked! Thanks Lewis. One question – actually the range of values I want to check is the 12 months, so I tried to copy & paste the following to the Find Text parameter in the Add Validation Rule dialog box:
(?=[\w\W]*January)(?=[\w\W]*February)(?=[\w\W]*March)(?=[\w\W]*April)(?=[\w\W]*May)(?=[\w\W]*June)(?=[\w\W]*July)(?=[\w\W]*August)(?=[\w\W]*September)(?=[\w\W]*October)(?=[\w\W]*November)(?=[\w\W]*December)
However, it doesn’t work due to the text is too long. So what I did was to have 2 validation rules – one is “(?=[\w\W]*January)(?=[\w\W]*February)(?=[\w\W]*March)(?=[\w\W]*April)(?=[\w\W]*May)(?=[\w\W]*June)”;
another is “(?=[\w\W]*July)(?=[\w\W]*August)(?=[\w\W]*September)(?=[\w\W]*October)(?=[\w\W]*November)(?=[\w\W]*December)”. Is it right way to do it OR there are other better ways to do it without writing a custom validation rule? What does ‘[w\W]’ mean? Thanks for your time Lewis.
Rick – I am looking forward to seeing the Validation Rule sample posting. It will for sure give users great ideas on what and how to do with the custom validation rules.
Thanks all.
HaYin
-----------------------------------------------------------------------------------------------
If you can get away with it, you can shorten the months to read:
(?=[\w\W]*Jan)(?=[\w\W]*Feb)(?=[\w\W]*Mar)(?=[\w\W]*Apr)(?=[\w\W]*May)(?=[\w\W]*Jun)”“(?=[\w\W]*Jul)(?=[\w\W]*Aug)(?=[\w\W]*Sep)(?=[\w\W]*Oct)(?=[\w\W]*Nov)(?=[\w\W]*Dec)
According to my handy Regular Expressions book [\w\W]* means
\w = any alphanumeric in upper or lower case and undersore
\W = any non-alphanumeric or non underscore
putting both in brackets tells it to match one or the other
and the asterisk tells it to match zero or more of the set
----------------------------------------------------------------------------------------------------
No comments:
Post a Comment