Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.VisualStudio.TestTools.WebTesting;
- using System.Text.RegularExpressions;
- using System.ComponentModel;
- namespace Homework
- {
- [DisplayName("Text Search Cutsom Validation")]
- public class TestSearchCustomValidation : ValidationRule
- {
- [DisplayName("Search Text"), Description("The text that your search result should contain.")]
- public string SearchText { get; set; }
- [DisplayName("Search List Regex Pattern"), Description("The regex pattern, with which to get the html tag containing " +
- "all of the search results.")]
- public string SearchListRegexPattern { get; set; }
- [DisplayName("Search Element Regex Pattern"), Description("The regex pattern, with which to get the html tag containing " +
- "a single element of the search results.")]
- public string ElementRegexPattern { get; set; }
- [DisplayName("Ignore Case"), Description("Specifies if the searched text will be case sensitive.")]
- public bool IgnoreCase { get; set; }
- public override void Validate(object sender, ValidationEventArgs e)
- {
- if (IgnoreCase)
- {
- SearchText = SearchText.ToUpper();
- }
- e.IsValid = Find(e.Response.BodyString);
- }
- private bool Find(string body)
- {
- string pattern = SearchListRegexPattern; // "<ul class=\"tSearchResultList tClear\">(.|\\s)*?</ul>"
- Match searchList = Regex.Match(body, pattern);
- Match searchListElement = Regex.Match(searchList.Value, ElementRegexPattern); //"<li>(.|\\s)*?</li>"
- while (searchListElement.Success)
- {
- string searchElementInnerHTML = searchListElement.Value;
- if (IgnoreCase)
- {
- searchElementInnerHTML = searchElementInnerHTML.ToUpper();
- }
- string[] searchTextElements = SearchText.Split(' ');
- foreach (var word in searchTextElements)
- {
- if (!searchElementInnerHTML.Contains(word))
- {
- return false;
- }
- }
- searchListElement = searchListElement.NextMatch();
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement