Click here to Skip to main content
15,886,724 members
Articles / Code generation

Semi generated crawler

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Jun 2012CPOL4 min read 21.6K   599   10  
Leverage Visual studio Web Test Framework for your crawling needs...
using System;
using System.ComponentModel;
using System.Globalization;
using System.Net;
using System.Text.RegularExpressions;
namespace LightWebTestFramework.Rules
{
	[LocalizableDescription("ConditionalRuleDescriptionCookieComparison"), LocalizableDisplayName("ConditionalRuleNameCookieComparison")]
	public class CookieComparisonRule : ConditionalRule
	{
		private string m_webpage;
		private string m_cookieName;
		private StringComparisonOperator m_comparisonOperator;
		private string m_cookieValue;
		private bool m_IgnoreCase;
		private bool m_useRegularExpression;
		private string m_cookieDomainName;
		private string m_cookiePath;
		[LocalizableDescription("PropertyDescriptionWebPageUri"), LocalizableDisplayName("PropertyNameWebPageUri")]
		public string Webpage
		{
			get
			{
				return this.m_webpage;
			}
			set
			{
				this.m_webpage = value;
			}
		}
		[LocalizableDescription("PropertyDescriptionCookieNameToFind"), LocalizableDisplayName("PropertyNameCookieName")]
		public string CookieName
		{
			get
			{
				return this.m_cookieName;
			}
			set
			{
				this.m_cookieName = value;
			}
		}
		[LocalizableDescription("PropertyDescriptionComparisonOperator"), LocalizableDisplayName("PropertyNameComparisonOperator"), DefaultValue(StringComparisonOperator.Equality)]
		public StringComparisonOperator ComparisonOperator
		{
			get
			{
				return this.m_comparisonOperator;
			}
			set
			{
				this.m_comparisonOperator = value;
			}
		}
		[LocalizableDescription("PropertyDescriptionCookieValue"), LocalizableDisplayName("PropertyNameCookieValue")]
		public string CookieValue
		{
			get
			{
				return this.m_cookieValue;
			}
			set
			{
				this.m_cookieValue = value;
			}
		}
		[LocalizableDescription("PropertyDescriptionCookieComparisonIgnoreCase"), LocalizableDisplayName("PropertyNameIgnoreCase"), DefaultValue(true)]
		public bool IgnoreCase
		{
			get
			{
				return this.m_IgnoreCase;
			}
			set
			{
				this.m_IgnoreCase = value;
			}
		}
		[LocalizableDescription("PropertyDescriptionCookieComparisonUseRegularExpression"), LocalizableDisplayName("PropertyNameUseRegularExpression"), DefaultValue(false)]
		public bool UseRegularExpression
		{
			get
			{
				return this.m_useRegularExpression;
			}
			set
			{
				this.m_useRegularExpression = value;
			}
		}
		[LocalizableDescription("PropertyDescriptionCookieDomainName"), LocalizableDisplayName("PropertyNameCookieDomainName")]
		public string CookieDomainName
		{
			get
			{
				return this.m_cookieDomainName;
			}
			set
			{
				this.m_cookieDomainName = value;
			}
		}
		[LocalizableDescription("PropertyDescriptionCookiePath"), LocalizableDisplayName("PropertyNameCookiePath")]
		public string CookiePath
		{
			get
			{
				return this.m_cookiePath;
			}
			set
			{
				this.m_cookiePath = value;
			}
		}
		public override void CheckCondition(object sender, ConditionalEventArgs e)
		{
			Cookie cookie = null;
			foreach (Cookie cookie2 in e.WebTest.Context.CookieContainer.GetCookies(new Uri(this.m_webpage)))
			{
				if (this.CheckCookieExistence(cookie2))
				{
					cookie = cookie2;
					break;
				}
			}
			if (cookie == null)
			{
				e.IsMet = false;
				string message = string.Format(CultureInfo.CurrentCulture, Resources.ErrorCookieNotFound, new object[]
				{
					this.m_cookieName,
					this.m_webpage
				});
				throw new ConditionalRuleException(message);
			}
			if (this.m_useRegularExpression)
			{
				e.IsMet = this.CheckCookieValueWithRegularExpression(cookie.Value);
				return;
			}
			e.IsMet = this.CheckCookieValueWithSimpleComparison(cookie.Value);
		}
		public override string StringRepresentation()
		{
			string format;
			if (this.m_comparisonOperator == StringComparisonOperator.Equality)
			{
				if (BindingHelperMethods.IsFullyBoundValue(this.m_cookieValue))
				{
					format = Resources.StringRepresentationCookieEqualityComparisonWithBinding;
				}
				else
				{
					format = Resources.StringRepresentationCookieEqualityComparison;
				}
			}
			else
			{
				if (BindingHelperMethods.IsFullyBoundValue(this.m_cookieValue))
				{
					format = Resources.StringRepresentationCookieInequalityComparisonWithBinding;
				}
				else
				{
					format = Resources.StringRepresentationCookieInequalityComparison;
				}
			}
			return string.Format(CultureInfo.CurrentCulture, format, new object[]
			{
				this.m_cookieName,
				this.m_webpage,
				this.m_cookieValue
			});
		}
		private bool CheckCookieExistence(Cookie cookie)
		{
			return string.Equals(cookie.Name, this.m_cookieName, StringComparison.OrdinalIgnoreCase) && (string.IsNullOrEmpty(this.m_cookieDomainName) || string.Equals(cookie.Domain, this.m_cookieDomainName, StringComparison.OrdinalIgnoreCase)) && (string.IsNullOrEmpty(this.m_cookiePath) || string.Equals(cookie.Path, this.m_cookiePath, StringComparison.OrdinalIgnoreCase));
		}
		private bool CheckCookieValueWithSimpleComparison(string cookieValue)
		{
			StringComparison comparisonType = this.m_IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
			return (this.m_comparisonOperator == StringComparisonOperator.Equality && string.Compare(cookieValue, this.m_cookieValue, comparisonType) == 0) || (this.m_comparisonOperator == StringComparisonOperator.Inequality && string.Compare(cookieValue, this.m_cookieValue, comparisonType) != 0);
		}
		private bool CheckCookieValueWithRegularExpression(string cookieValue)
		{
			RegexOptions regexOptions = RegexOptions.None;
			if (this.m_IgnoreCase)
			{
				regexOptions |= RegexOptions.IgnoreCase;
			}
			Regex regex = new Regex(this.m_cookieValue, regexOptions);
			Match match = regex.Match(cookieValue);
			return (this.m_comparisonOperator == StringComparisonOperator.Equality && match.Success) || (this.m_comparisonOperator == StringComparisonOperator.Inequality && !match.Success);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions