Advertisement
Merx3

Untitled

Jul 16th, 2012
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using Microsoft.VisualStudio.TestTools.WebTesting;
  2. using System.Text.RegularExpressions;
  3. using System.ComponentModel;
  4.  
  5. namespace Homework
  6. {
  7.     [DisplayName("Text Search Cutsom Validation")]
  8.     public class TestSearchCustomValidation : ValidationRule
  9.     {
  10.         [DisplayName("Search Text"), Description("The text that your search result should contain.")]
  11.         public string SearchText { get; set; }
  12.  
  13.         [DisplayName("Search List Regex Pattern"), Description("The regex pattern, with which to get the html tag containing " +
  14.             "all of the search results.")]
  15.         public string SearchListRegexPattern { get; set; }
  16.  
  17.         [DisplayName("Search Element Regex Pattern"), Description("The regex pattern, with which to get the html tag containing " +
  18.             "a single element of the search results.")]
  19.         public string ElementRegexPattern { get; set; }
  20.  
  21.         [DisplayName("Ignore Case"), Description("Specifies if the searched text will be case sensitive.")]
  22.         public bool IgnoreCase { get; set; }
  23.  
  24.         public override void Validate(object sender, ValidationEventArgs e)
  25.         {
  26.             if (IgnoreCase)
  27.             {
  28.                 SearchText = SearchText.ToUpper();
  29.             }
  30.             e.IsValid = Find(e.Response.BodyString);
  31.         }
  32.  
  33.         private bool Find(string body)
  34.         {
  35.             string pattern = SearchListRegexPattern; // "<ul class=\"tSearchResultList tClear\">(.|\\s)*?</ul>"
  36.             Match searchList = Regex.Match(body, pattern);
  37.             Match searchListElement = Regex.Match(searchList.Value, ElementRegexPattern); //"<li>(.|\\s)*?</li>"
  38.             while (searchListElement.Success)
  39.             {
  40.                 string searchElementInnerHTML = searchListElement.Value;
  41.                 if (IgnoreCase)
  42.                 {
  43.                     searchElementInnerHTML = searchElementInnerHTML.ToUpper();
  44.                 }
  45.                 string[] searchTextElements = SearchText.Split(' ');
  46.                 foreach (var word in searchTextElements)
  47.                 {
  48.                     if (!searchElementInnerHTML.Contains(word))
  49.                     {
  50.                         return false;
  51.                     }
  52.                 }
  53.                 searchListElement = searchListElement.NextMatch();
  54.             }
  55.             return true;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement