/// <summary> /// Find all javascript:__doPostback(...) type declarations which indicates /// all controls that support postback. It finds all such controls that support /// postback and then stores the full client ID of the control in Context /// using the last part of the ID as key in Context. For example: /// $POSTBACK.1.AddNewWidget = WidgetUpdatePanel001$ctl_002$AddNewWidget /// This way you can find a paricular controls full client ID when you know only the /// server ID of the control. /// </summary> /// <param name="bodyHtml">Body HTML</param> /// <param name="context">WebTest Context</param> public static void ExtractPostBackNames(string bodyHtml, WebTestContext context) { RuleHelper.NotAlreadyDone(context, "$POSTBACK.EXTRACTED", () => { var matches = _FindPostbackNames.Matches(bodyHtml); foreach (Match match in matches) { string fullID = match.Groups[1].Value; string lastPartOfID = fullID.Substring(fullID.LastIndexOf('$') + 1); string contextKeyName = "$POSTBACK." + lastPartOfID; RuleHelper.PlaceUniqueItem(context, contextKeyName, fullID); } }); }
public static void ExtractUpdatePanelNamesFromHtml(string body, WebTestContext context) { RuleHelper.NotAlreadyDone(context, UPDATEPANEL_EXTRACTED_KEY, () => { // Do not extract update panel names twice int pos = body.IndexOf(UPDATE_PANEL_DECLARATION); if (pos > 0) { // found declaration of all update panels on the page pos += UPDATE_PANEL_DECLARATION.Length; int endPos = body.IndexOf(']', pos); string updatePanelNamesDelimited = body.Substring(pos, endPos - pos); string[] updatePanelNames = updatePanelNamesDelimited.Split(','); int updatePanelCounter = 1; foreach (string updatePanelName in updatePanelNames) { // Create a unique key in the context using the UpdatePanel's Last part of the ID which is usually the // ID specified in aspx page string updatePanelFullId = updatePanelName.TrimStart('\'').TrimEnd('\'').TrimStart('t'); string updatePanelIdLastPart = updatePanelFullId.Substring(updatePanelFullId.LastIndexOf('$') + 1); string contextKeyName = UPDATE_PANEL_PREFIX + updatePanelIdLastPart; string keyName = RuleHelper.PlaceUniqueItem(context, contextKeyName, updatePanelFullId); // Store all update panels as $UPDATEPANEL.1, $UPDATEPANEL.2, ... context[UPDATE_PANEL_PREFIX + updatePanelCounter] = updatePanelFullId; // Find the position of the UpdatePanel string updatePanelDivId = updatePanelFullId.Replace('$', '_'); // Look for a div with id having the updatepanel ID, e.g. <div id="UserTabPage_TabUpdatePanel"> string lookingFor = "<div id=\"" + updatePanelDivId + "\""; int updatePanelDivIdPos = body.IndexOf(lookingFor); context[UPDATE_PANEL_PREFIX + updatePanelCounter + UPDATE_PANEL_POS_KEY] = updatePanelDivIdPos; context[keyName + UPDATE_PANEL_POS_KEY] = updatePanelDivIdPos; updatePanelCounter++; } context[UPDATE_PANEL_COUNT_KEY] = updatePanelCounter; } }); }
public override void Validate(object sender, ValidationEventArgs e) { using (new RuleCheck(e, this.StopOnError)) { Cookie cookie = RuleHelper.GetCookie(this.CookieName, this.Domain, e.Response.Cookies); if (!this.Exists) { if (null != cookie) { e.IsValid = false; e.Message = this.CookieName + " must not be in response"; return; } } else { // If no cookie found, it fails if (null == cookie) { e.IsValid = false; e.Message = this.CookieName + " cookie not found"; return; } // Cookie will have expires set in future if it's a persistent cookie if (this.IsPersistent) { if (DateTime.Now > cookie.Expires) { e.IsValid = false; e.Message = "Cookie does not expire in future. It has expired on: " + cookie.Expires.ToString(); return; } } else { // Cookie must be non-persistent if (cookie.TimeStamp < cookie.Expires) { e.IsValid = false; e.Message = "Cookie must not be persistent. It has expiry on: " + cookie.Expires.ToString(); } } if (!string.IsNullOrEmpty(this.Domain)) { if (this.Domain != cookie.Domain) { e.IsValid = false; e.Message = "Cookie domain does not match. Found: " + cookie.Domain + " expected: " + this.Domain; return; } } else { if (cookie.Domain != string.Empty && cookie.Domain != e.Response.ResponseUri.Host) { e.IsValid = false; e.Message = "Cookie must not have any domain or should be '" + e.Response.ResponseUri.Host + "', but it has '" + cookie.Domain + "'"; } } if (this.MatchValue) { if (cookie.Value != this.CookieValueToMatch) { e.IsValid = false; e.Message = "Cookie value does not match. Expected: [" + this.CookieValueToMatch + "] Found [" + cookie.Value + "]"; } } } } }