Reputation: 1784
I'm try to use regex to validate response text in a webtest. With ValidationRuleFindText class, you have the option to use regex to validate response text from a web request. Example:
Response Text: {"success":true, "data":"foo bar"}
ValidationRuleFindText validationRule = new ValidationRuleFindText();
validationRule.FindText = @"/(/""success/"":true)/ig";
validationRule.IgnoreCase = true;
validationRule.UseRegularExpression = true;
validationRule.PassIfTextFound = true;
foobarRequest.ValidateResponse += new EventHandler<ValidationEventArgs> (validationRule3.Validate);
For someone reason it's not recognizing that validationRule.FindText is regex and it fails because it literally can't find /(/"success/":true)/ig in the response. If anyone is familiar with this, any help would be greatly appreciated :)
Upvotes: 0
Views: 585
Reputation: 14047
This is really easy to work out using Visual Studio. Take any .webtest
file that can be used as a sandbox file. Add a find text validation rule with the correct settings and then run the "generate code" command. The correct code for the validation will be seen in the generated .cs
file.
For the example string in the question, the search string {"success":true, "data":"foo bar"}
generates the line below. The other validation rule lines are the same as in the question.
validationRule1.FindText = "{\"success\":true, \"data\":\"foo bar\"}";
Finally, there is nothing about this search string that needs a regular expression. A non-regular-expression validation rule would suffice.
The line in the question has validationRule.FindText = @"/(/""success/"":true)/ig";
which appears to be appealing to the way regular expressions are used in some other languages. The enclosing /
characters and the trailing ig
should not be used. Their functionality is achieved by other means.
Upvotes: 1