public override void Extract(object sender, ExtractionEventArgs e) { string body = e.Response.BodyString; List <string> formElements = new List <string>(); var processMatches = new Action <MatchCollection, string>((extractMatches, prefix) => { foreach (Match extractMatch in extractMatches) { string name = extractMatch.Groups["name"].Value; string value = extractMatch.Groups["value"].Value; string lastPartOfName = name.Substring(name.LastIndexOf('$') + 1); string keyName = RuleHelper.PlaceUniqueItem(e.WebTest.Context, prefix + lastPartOfName, name); e.WebTest.Context[keyName + VALUE_SUFFIX] = value; // Create a name value pair in context as it is using the form element's name e.WebTest.Context[name] = value; formElements.Add(name); } }); processMatches(_FindInputTags.Matches(body), INPUT_PREFIX); foreach (Match match in _FindSelectTags.Matches(body)) { var extractMatches = _ExtractSelectTags.Matches(match.Value); processMatches(extractMatches, SELECT_PREFIX); } e.WebTest.Context[FORM_ELEMENT_KEYS] = formElements.ToArray(); }
private void ExtractUpdatePanelNamesFromAsyncPostback(string body, WebTestContext context) { RuleHelper.NotAlreadyDone(context, "$UPDATEPANEL.EXTRACTED", () => { int newUpdatePanelsAdded = 0; foreach (Match match in _FindUpdatePanelRegex.Matches(body)) { string updatePanelDivID = match.Groups["name"].Value; string updatePanelFullId = updatePanelDivID.Replace('_', '$'); string updatePanelIdLastPart = updatePanelFullId.Substring(updatePanelFullId.LastIndexOf('$') + 1); string contextKeyName = UPDATE_PANEL_PREFIX + updatePanelIdLastPart; string keyName = RuleHelper.PlaceUniqueItem(context, contextKeyName, updatePanelFullId); int countOfKeys = context.Count; RuleHelper.PlaceUniqueItem(context, UPDATE_PANEL_KEY, updatePanelFullId); if (context.Count > countOfKeys) { newUpdatePanelsAdded++; } } context[UPDATE_PANEL_COUNT_KEY] = ((int)context[UPDATE_PANEL_COUNT_KEY]) + newUpdatePanelsAdded; }); }
/// <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; } }); }