Validation of WebTest
Hello,
I've got some questions in Webtests but I can't find anything in the online-help:
In my problem I have got a list (html-site) with different facts and cells with different colors like yellow. I want to check the color of the different cells.
I created a extraction rule "Extract Text" and I want to validate the response of it. The result of this rule is the value "yellow" which is in attribute "$HIDDEN1" in the context menu.
I have already created a validation rule called "Find Text" which is searching for the text "yellow".
But how can I assign the result of the extraction rule with the validation rule? Or is this the wrong way? I guess we have to create a new variant with the value yellow and this one should be validated with $HIDDEN1 ?
What should I do if I want to compare the results of different extraction rules?
Thank you very much.
Best regards
Andreas
Validation rules are executed before the extraction rules for a Web-test response in VSTS 2005. So, the result of an extraction rule can only be used on subsequent requests in the web test (e.g. bound to subsequent query/form/rule parameters on the request). If I understand correctly, you would like to use the extraction rule to extract text from the response that you would then use in your validation rule for the same response. Unfortunately, with the order that rules are processed, that is not possible in VSTS 2005. If you are indeed attempting the use the result of an extraction rule in the validation of a subsequent response, then you pass the value into a built-in validation rule by using data binding syntax for the validation rule parameter (e.g {{$HIDDEN1}}).
If you are trying to extract text to use during validation in the same response, then the best approach would be to write a custom validation rule. In this case there would be no need to use extraction rules, you would merely find the text you want to validate from within the validation rule. It's not clear if you actually wanted the extracted rule to appear in the test context or not, but if you do you could plance any values you wanted into the context from within your custom validation rule. The online docs describe how to create custom validation rules.
We've reversed the order of execution for validation/extraction rules in the next release of VSTS so that what you what you attempted would actually be possible.
Does this help? Let me know if you have further question.
Thanks,
Rick
Hello Rick,
thank you for the quick answer.
I am a beginner in VST2005 TE for Testers and it's the task of my thesis to create some automatic tests.
Well I will show you the problem:
There is a "website1" with content, in my example there are some orders. It looks like this one:
| Order | Ordernumber | Orderdate | Order start |
| build cars | bc123 | 06-20-2007 | no |
| build houses | bh789 | 06-25-2007 | yes |
| build streets | bs234 | 07-01-2007 | no |
Code Snippet
<html>
<table border="1">
<tr>
<td>Order</td>
<td>Ordernumber</td>
<td>Orderdate</td>
<td>Order start</td>
</tr>
<tr>
<td id="ordername">build cars</td>
<td id="ordernumber">bc123</td>
<td id="orderdate">06-20-2007</td>
<td id="orderstart">no</td>
</tr>
<tr>
<td id="ordername">build houses</td>
<td id="ordernumber">bh789</td>
<td id="orderdate">06-25-2007</td>
<td id="orderstart" bgcolor="yellow">yes</td>
</tr>
<tr>
<td id="ordername">build streets</td>
<td id="ordernumber">bs234</td>
<td id="orderdate">07-01-2007</td>
<td id="orderstart">no</td>
</tr>
</tr>
</table>
</html>
Each order has got an own page called "website2" with more details like this one:
| Order | Builder | address | Ordernumber | orderowner | Orderdate | Orderstart |
| build houses | Mr. Smith | Street 1 | bh789 | J.K. | 06-20-2007 | yes |
Code Snippet
<html>
<table border="1">
<tr>
<td>Order</td>
<td>Builder</td>
<td>address</td>
<td>Ordernumber</td>
<td>orderowner</td>
<td>Orderdate</td>
<td>Orderstart</td>
</tr>
<tr>
<td id="ordername">build houses</td>
<td id="builder">Mr. Smith</td>
<td id="address">Street 1</td>
<td id="ordernumber">bh789</td>
<td id="orderowner">J.K.
<td id="orderdate">06-20-2007</td>
<td id="orderstart" bgcolor="red">yes</td>
</tr>
</table>
</html>
I want to validate, if the facts for the order "bh789" of website1 are the same on "website" for order "bh789". I want to validate the facts (like date) and the color of the last cell (like yellow).
So I thought I could extract the data of website1 and then I could extract the data of "website" and after this a can validate the both to check if there are some mistakes?
Please tell me if this is the wrong way. How would you do this test for this example? The problem is there is not such a good help and here in germany you can't buy any books for automatic testing with VS2005 TE. I can't find good examples on the web. There is also nobody who can be called up to help in some facts so the forum is my last hope.
Thank you very much.
Best regards
Andreas
Ok, so you have two separate requests one with the summary of orders and one with the detail of a specific order. In this case, you should be able to extract a value (such as "yellow") and then pass that value into a validation rule on the second request (details of a specific order).
Let's consider a simple case:
Use the extraction rule "Extract Text" on the first request and have the extracted value placed in the test context as name "BGCOLOR"
StartWith=bgcolor="
EndsWith=">
ContextParameterName=BGCOLOR
Use a validation rule "Find Text" on the second request and pass in the extracted value from the first request. For example
FindText=bgcolor="{{BGCOLOR}}">
Given the code snippets provided, when you run the web test the second request in the web test would be tagged as failed with a validation failure since the colors do not match, which is exactly what you want to verify. In your example it would be harder if you had to extract a value for a specific order ID. That might require a custom validatoin rule or a longer "StartsWith" string abd "FindText" string in the respective extraction/validation rules.
Does this help?
Thanks,
Rick
Dear Rick,
that's exactly this I want to do, I used a longer "startswith"-string for the order. Thank you very much - it works great!
But in this situation I have got an other question:
Is it possible to add or subtract some facts?
For example:
| Order | Ordernumber | Orderdate | Order start | Amount | additional Amount |
| build cars | bc123 | 06-20-2007 | no | 100 | 37 |
Code Snippet
<html>
<table border="1">
<tr>
<td>Order</td>
<td>Ordernumber</td>
<td>Orderdate</td>
<td>Order start</td>
<td>Amount</td>
<td>additional Amount</td>
</tr>
<tr>
<td id="ordername">build cars</td>
<td id="ordernumber">bc123</td>
<td id="orderdate">06-20-2007</td>
<td id="orderstart">no</td>
<td id="amount">100</td>
<td id="additionalamount">37</td>
</tr>
</table>
</html>
I want to check if "amount" + "additional Amount" = 137
I can extract the two cells - the extracted values would be 100 and 37. But how can I add these two values and check if they are an amount of 137?
Best regards,
Andreas
Hello,
is there no chance to check the sum of two numbers?
Best regards
Andreas
Here is a brief walk through of how you might do this using both the built-in extraction rules and a custom validation rule.
Let's assume you have a web test with two requests. The first request uses two extractions rules to extract two integer values from its response. Also, let's assume the response to the second request has the value that should represent the sum of the two integers extracted in the first response.
In my example, the first response contains the following unique character sequence that I want to extract (without the quotes)...
">P1=10<"
">P2=20<"
And the second response contains the following unique character sequence that i want to validate (without the quotes)...
">P3=30<"
Request1 contains the following two extraction rules...
ExtractText
ContextParameterName=P1
StartsWith=>P1=
EndsWith=<
ExtractText
ContextParameterName=P2
StartsWith=>P2=
EndsWith=<
Request2 constains the following validation rule...
SumValidationRule (a custom rule shown below)
StartsWith=>P3=
EndsWith=<
Arg1={{P1}}
Arg2={{P2}}
Note that the notation for Arg1 and Arg2 in the validation rule indicates that we are passing in the values from the test context which were extracted by the rules for request1.
Here's the code for the sample custom validation rule...
publicclassSumValidationRule : ValidationRule
{
privatestring
m_arg1; privatestring
m_arg2; privatestring
m_startsWith; privatestring
m_endsWith;
publicstring Arg1 { set { m_arg1 = value; } }
publicstring Arg2 { set { m_arg2 = value; } }
publicstring StartsWith { set { m_startsWith = value; } }
publicstring EndsWith { set { m_endsWith = value; } }
publicoverridevoid Validate(object sender, ValidationEventArgs e)
{
try
{
string
body = e.Response.BodyString; int
startIndex = body.IndexOf(m_startsWith); if
(startIndex != -1) {
int
endIndex = body.IndexOf(m_endsWith, startIndex + m_startsWith.Length); if
(endIndex != -1) {
string
extractedString = body.Substring(startIndex + m_startsWith.Length, endIndex - (startIndex + m_startsWith.Length)); int
extractedValue = Int32.Parse(extractedString); int
arg1 = Int32.Parse(m_arg1); int
arg2 = Int32.Parse(m_arg2); e.IsValid = (arg1 + arg2 == extractedValue);
}
}
}
catch
{
e.IsValid =
false
; }
}
}
Here's the steps I followed to create the sample custom validation rule...
- Create a new class in the project containing the web test (it could have been created in a different project as well, but a project reference would have to be added to the project containing the web test in this case).
- Name the class SumValidationRule.cs
- Extend the class from ValidationRule by typing ": ValidationRule" at the end of the class definition.
- Add the using statement for Microsoft.VisualStudio.TestTools.WebTesting (note, the little red underline that appears under the text ValidationRule when you type it, hover it, then choose to automatically insert the using... or just type it in manually).
- Make the class public. (type "public" in front of the class definition). Note, if you forget to make the class public, it will not show up in the validation rule selection dialog in the web test editor.
- Add the properties that I want to have supplied to my validation rule (Arg1, Arg2, StartsWith, EndsWith).
- Override the "Validate" method with the code that I want executed.
- Add the code shown above.
- Get access to the response body
- Find the index of startWith
- Find the index of endsWith
- Extract the string between startWith and EndsWith
- Convert the extracted value and arguments to integers
- test the sum
- IsValid is true if the validation rule passes, false otherwise.
- You could also use e.Message to indicate reasons for failure.
To test this, I placed a breakpoint on the first line of the Validate method (F9), and then chose "Debug Test" from the toolbar in the web test editor to start running the web test under control of the debugger. When the web test reached my custom validation rule, VS broke into the debugger. I then single stepped through the code to verify it was working correctly. Pressed F5 to resume execution, and the test passed.
Hope this helps.
Rick
Hello Rick,
thank you very much for this great walk through! After some problems I realized it and the rule works also fine. But i think it is necessary to get the results in the context-menu:
The testers want to see the result in the context like:
Code Snippet
...
$HIDDEN1.HD_SiteOptions 0
$HIDDEN1.HD_filter_logtable orderlist
$HIDDEN1.Filter_QN_collectionvalue
$HIDDEN1.ypos 0
$HIDDEN1 System.String[]
P1 120,00
P2 120,00
Amount 1932,00
Validation correct
How is it possible to print the result of your custom validation rule in the contextmenu like "validation = correct" or "validation = false" ?
For many tests it is the best solution to show the testers the contextmenu and so they can see which items/ numbers are correct and which are incorrect.
And I have got an other question to you:
Is there anywhere a source or a collection of custom validation rules or custom extraction rules? In every product or on every website you can find templates or many files, but I didn't find anything in relation with custom validation rules (only one sample in the help and one sample in joshs blog and your sample in top of this thread - but this is everything, isn't it?)
The validation and extraction tests are the main-part of the Team Edition for Testers and so I think it would be helpful for many people to get a source of some different custom rules?
Best regards
Andreas