private static void WriteHeaderManager(XmlWriter xmlWriter, Session request) { JMeterWrapper.WriteStartElement(xmlWriter, "HeaderManager", "HeaderPanel", "HeaderManager", "HTTP Header Manager", "true"); JMeterWrapper.WriteStartElement(xmlWriter, "collectionProp", "HeaderManager.headers"); for (var i = 0; i < request.oRequest.headers.Count(); i++) { var header = request.oRequest.headers[i]; if (!IsHeaderExcluded(header)) { AddHeaderToCollectionProp(xmlWriter, header.Name, header.Value); } } // </collectinProp> xmlWriter.WriteEndElement(); // </HeaderManager> xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("hashTree"); // </hashTree> xmlWriter.WriteEndElement(); }
public override string ToString() { /* * <RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="RegExp Extractor - URL_Params1" enabled="true"> * <stringProp name="TestPlan.comments">commmmmmmmeent</stringProp> * <stringProp name="RegexExtractor.useHeaders">false</stringProp> * <stringProp name="RegexExtractor.refname">URL_Params1</stringProp> * <stringProp name="RegexExtractor.regex">"historiaclinicaprincipalv2\?([^"]+)"</stringProp> * <stringProp name="RegexExtractor.template">$1$</stringProp> * <stringProp name="RegexExtractor.default">NOT FOUND</stringProp> * <stringProp name="RegexExtractor.match_number">1</stringProp> * </RegexExtractor> * */ string result; var useHeaders = "false"; switch (ExtractParameterFrom) { case ExtractFrom.Headers: useHeaders = "true"; break; case ExtractFrom.Url: useHeaders = "URL"; break; } using (var stream = new MemoryStream()) { // Create an XMLWriter object var xmlWriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8) { Formatting = Formatting.Indented }; JMeterWrapper.WriteStartElement(xmlWriter, "RegexExtractor", "RegexExtractorGui", "RegexExtractor", "RegExp Extractor - " + VariableName, "true"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "TestPlan.comments", Description); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "RegexExtractor.useHeaders", useHeaders); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "RegexExtractor.refname", VariableName); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "RegexExtractor.regex", RegularExpression); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "RegexExtractor.template", Group); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "RegexExtractor.default", "NOT FOUND"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "RegexExtractor.match_number", "1"); // </RegexExtractor> xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("hashTree"); xmlWriter.WriteEndElement(); xmlWriter.Flush(); stream.Position = 0; var streamReader = new StreamReader(stream); result = streamReader.ReadToEnd(); } return(result); }
public static string CreateResponseCodeValidation(string title, int responseCodeValidate, string assertionsTestType, string errorDescription) { string result; using (var stream = new MemoryStream()) { // Create an XMLWriter object var xmlWriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8) { Formatting = Formatting.Indented }; // <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assert - " enabled="true"> JMeterWrapper.WriteStartElement(xmlWriter, "ResponseAssertion", "AssertionGui", "ResponseAssertion", title, "true"); // <collectionProp name="Asserion.test_strings"> JMeterWrapper.WriteStartElement(xmlWriter, "collectionProp", "Asserion.test_strings"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "49586", responseCodeValidate.ToString(CultureInfo.InvariantCulture)); // </collectionProp> xmlWriter.WriteEndElement(); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "Assertion.test_field", "Assertion.response_code"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "Assertion.assume_success", "false"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "intProp", "Assertion.test_type", assertionsTestType); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "Assertion.comments", ""); if (errorDescription != string.Empty) { JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "TestPlan.comments", errorDescription); } // </ResponseAssertion> xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("hashTree"); xmlWriter.WriteEndElement(); xmlWriter.Flush(); stream.Position = 0; var streamReader = new StreamReader(stream); result = streamReader.ReadToEnd(); } return(result); }
public override string ToString() { //<TransactionController guiclass="TransactionControllerGui" testclass="TransactionController" testname="Step 00 - Event Go" enabled="true"> // <boolProp name="TransactionController.parent">false</boolProp> // <stringProp name="TestPlan.comments">Go( "http://localhost/CursoGXtest.NetEnvironment/home.aspx" )</stringProp> //</TransactionController> string result; using (var stream = new MemoryStream()) { // Create an XMLWriter object var xmlWriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8) { Formatting = Formatting.Indented }; JMeterWrapper.WriteStartElement(xmlWriter, "TransactionController", "TransactionControllerGui", "TransactionController", Name, "true"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "TransactionController.parent", "false"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "TestPlan.comments", Desc); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "TransactionController.includeTimers", "false"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("hashTree"); foreach (var pageRequest in Requests) { xmlWriter.WriteRaw(pageRequest.ToString()); } // </hashTree> xmlWriter.WriteEndElement(); xmlWriter.Flush(); stream.Position = 0; var streamReader = new StreamReader(stream); result = streamReader.ReadToEnd(); } return(result); }
public override string ToString() { string result; using (var stream = new MemoryStream()) { // Create an XMLWriter object var xmlWriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8) { Formatting = Formatting.Indented }; WriteHTTPSample(xmlWriter, MyStep, FiddlerSession, InfoPage, GetFullURL(InfoPage), Validations, ParametersToExtract); // if there aren't follow redirects, then add validations to the primary request if (FollowRedirects.Count > 0) { // Adding follow Redirects // the last request must have the validations var lastRequestForValidation = GetLastRequestForValidation(FollowRedirects); // Add JMeter GenericController to group the requests JMeterWrapper.WriteStartElement(xmlWriter, "GenericController", "LogicControllerGui", "GenericController", "Follow Redirects", "true"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("hashTree"); foreach (var followRedirect in FollowRedirects) { if (followRedirect.FiddlerSession == lastRequestForValidation) { WriteHTTPSample(xmlWriter, MyStep, followRedirect.FiddlerSession, followRedirect.InfoPage, followRedirect.GetFullURL(followRedirect.InfoPage), followRedirect.Validations, followRedirect.ParametersToExtract); } // Disable the follow redirects by response code // Change :: JMeter encodes the parameters of the redirect URL, producing bad bahaviour else { WriteHTTPSample(xmlWriter, MyStep, followRedirect.FiddlerSession, followRedirect.InfoPage, followRedirect.GetFullURL(followRedirect.InfoPage), followRedirect.Validations, followRedirect.ParametersToExtract); } } // </hashTree> xmlWriter.WriteEndElement(); } //// Adding secondary Requests //if (SecondaryRequests.Count > 0) //{ // JMeterWrapper.WriteStartElement(xmlWriter, "GenericController", "LogicControllerGui", "GenericController", "Secondary Requests", "true"); // xmlWriter.WriteEndElement(); // xmlWriter.WriteStartElement("hashTree"); // // if controller // JMeterWrapper.WriteStartElement(xmlWriter, "IfController", "IfControllerPanel", "IfController", "If Controller", "true"); // JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "IfController.condition", "${" + HTTPConstants.VariableNameDebug + "} == 0"); // JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "IfController.evaluateAll", "false"); // xmlWriter.WriteEndElement(); // xmlWriter.WriteStartElement("hashTree"); // // write response assertion to skip HTTP response code validation // JMeterWrapper.WriteResponseAssertionSkipHTTPResponse(xmlWriter); // foreach (var secondaryRequest in SecondaryRequests) // { // WriteHTTPSample(xmlWriter, MyStep, secondaryRequest, null, secondaryRequest.fullUrl); // } // // </hashTree> 'if' controller // xmlWriter.WriteEndElement(); // // </hashTree> 'generic' controller // xmlWriter.WriteEndElement(); //} xmlWriter.Flush(); stream.Position = 0; var streamReader = new StreamReader(stream); result = streamReader.ReadToEnd(); } return(result); }
private static void WriteHTTPSample(XmlWriter xmlWriter, AbstractStep myStep, Session request, Page page, string fullURL, IEnumerable <AbstractValidation> validations, IEnumerable <AbstractParameterExtractor> parametersToExtract, bool followRedirects = false, bool enable = true, bool isPrimary = true) { var httpMethod = request.oRequest.headers.HTTPMethod; string urlRequest; var serverName = ""; //"${" + HTTPConstants.VariableNameServer + "}"; var serverPortName = ""; //"${" + HTTPConstants.VariableNamePort + "}"; const string webAppName = "${" + HTTPConstants.VariableNameWebApp + "}"; if (request.host == myStep.ServerNameAndPort) { urlRequest = httpMethod + " " + fullURL;//fullURL.Replace(myStep.ServerName, serverName); // when URL shows the port if (!myStep.IsDefaultPort()) { // if the port is the same as ServerName, parametrize it if (request.port.ToString(CultureInfo.InvariantCulture) == myStep.ServerPort) { urlRequest = urlRequest.Replace(":" + myStep.ServerPort, ":" + serverPortName); } else { // don't parametrize the port serverPortName = request.port.ToString(CultureInfo.InvariantCulture); } } if (myStep.WebApp != null) { if (myStep.WebApp.Length > 0) { urlRequest = urlRequest.Replace(myStep.WebApp, webAppName); } } } else { urlRequest = httpMethod + " " + fullURL; serverName = request.host; serverPortName = request.port.ToString(CultureInfo.InvariantCulture); } if (urlRequest == string.Empty) { return; } // "GET http://${server}/${webApp}/home.aspx?${param1},etc" JMeterWrapper.WriteStartElement(xmlWriter, "HTTPSamplerProxy", "HttpTestSampleGui", "HTTPSamplerProxy", myStep.Index + " " + RemoveParameters(urlRequest), (enable ? "true" : "false")); switch (httpMethod.ToLower()) { case "post": var body = page == null?request.GetRequestBodyAsString().Replace("'", "'") : page.Body; if (page != null && !FiddlerSessionComparer.FiddlerSessionComparer.ReplaceInBodies) { Logger.GetInstance().Log("Body is not parametrized. Differences detected in page: " + request.id); foreach (var parameter in page.GetParametersToUse()) { Logger.GetInstance().Log(parameter); } } // <boolProp name="HTTPSampler.postBodyRaw">true</boolProp> JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "HTTPSampler.postBodyRaw", "true"); JMeterWrapper.WriteStartElement(xmlWriter, "elementProp", "HTTPsampler.Arguments", "Arguments"); JMeterWrapper.WriteStartElement(xmlWriter, "collectionProp", "Arguments.arguments"); JMeterWrapper.WriteStartElement(xmlWriter, "elementProp", string.Empty, "HTTPArgument"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "HTTPArgument.always_encode", "false"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "Argument.value", body); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "Argument.metadata", "="); // elementProp xmlWriter.WriteEndElement(); // collectionProp xmlWriter.WriteEndElement(); // elementProp xmlWriter.WriteEndElement(); break; case "get": JMeterWrapper.WriteStartElement(xmlWriter, "elementProp", "HTTPsampler.Arguments", "Arguments", "HTTPArgumentsPanel", "Arguments", "User Defined Variables", "true"); JMeterWrapper.WriteStartElement(xmlWriter, "collectionProp", "Arguments.arguments"); // collectionProp xmlWriter.WriteEndElement(); // elementProp xmlWriter.WriteEndElement(); break; } //var index = elementName.IndexOf("/${webApp}", StringComparison.Ordinal); //var path = elementName.Substring(index); var protocol = request.isHTTPS ? "HTTPS" : "HTTP"; string path; int index; if (request.host == myStep.ServerName) { index = urlRequest.IndexOf("/" + serverName, StringComparison.Ordinal); index += ("/" + serverName).Length; path = urlRequest.Substring(index); } else { index = (request.isHTTPS) ? urlRequest.IndexOf("https://", StringComparison.Ordinal) : urlRequest.IndexOf("http://", StringComparison.Ordinal); if (index == -1) { index = urlRequest.IndexOf("/", StringComparison.Ordinal); path = urlRequest.Substring(index); } else { index = urlRequest.IndexOf("//", StringComparison.Ordinal); path = urlRequest.Substring(index + 2); index = path.IndexOf("/", StringComparison.Ordinal); if (index > 0) { path = path.Substring(index); } } } JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.domain", serverName); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.port", serverPortName); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.connect_timeout", ""); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.response_timeout", ""); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.protocol", protocol); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.contentEncoding", ""); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.path", path.Replace(myStep.ServerName + "/", "")); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.method", httpMethod); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "HTTPSampler.follow_redirects", (followRedirects ? "true" : "false")); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "HTTPSampler.auto_redirects", "false"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "HTTPSampler.use_keepalive", "true"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "HTTPSampler.DO_MULTIPART_POST", "false"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.implementation", "HttpClient4"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "boolProp", "HTTPSampler.monitor", "false"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "HTTPSampler.embedded_url_re", ""); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "TestPlan.comments", "FiddlerID: " + request.id); // </HTTPSamplerProxy> xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("hashTree"); WriteHeaderManager(xmlWriter, request); // adding validations foreach (var validation in validations) { WriteElement(xmlWriter, validation); } // add "Logging HTML response in jmeter.log when test fail" //if (isPrimary) //{ // JMeterWrapper.WriteBeanShellAssertionModule(xmlWriter); //} // adding parameters extractor foreach (var extractor in parametersToExtract) { WriteElement(xmlWriter, extractor); } // adding the parameter extractors that were detected by comparing fiddler sessions if (page != null) { var parms = new List <JMeterRegExParameter>(); var constants = new List <JMeterConstant>(); foreach (var parameter in page.GetParametersToExtract()) { var description = "Used in pages: { " + string.Join(",", parameter.UsedInPages.Select(p => p.Id + "").ToArray()) + " } Original value: " + parameter.Values[0]; if (parameter.ParamExtractor is RegExpExtractor) { var valueSource = parameter.ParamExtractor as RegExpExtractor; var regExp = valueSource.RegExp; var extractFrom = GetExtractFrom(parameter.ExtractFromSection); var usedIn = GetUsedIn(parameter.TargetsOfPage(page)); var newValue = new JMeterRegExParameter( extractFrom, usedIn, parameter.VariableName, regExp, "$" + valueSource.GroupNumber + "$", parameter.Values[0], description ); parms.Add(newValue); } else { var newConstant = new JMeterConstant(parameter.VariableName, parameter.Values[0], description); constants.Add(newConstant); } } foreach (var paramExtractor in parms) { WriteElement(xmlWriter, paramExtractor); } if (constants.Count > 0) { // Write collection of constants /* * <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="Constants that couldn't be extracted from HTML" enabled="true"> * <collectionProp name="Arguments.arguments"> * <elementProp name="vUSUARIOMENUCODIGO" elementType="Argument"> * <stringProp name="Argument.name">vUSUARIOMENUCODIGO</stringProp> * <stringProp name="Argument.value">A7</stringProp> * <stringProp name="Argument.metadata">=</stringProp> * <stringProp name="Argument.desc">Used in pages: { 14,17 } Original value: A7</stringProp> * </elementProp> * </collectionProp> * </Arguments> * */ JMeterWrapper.WriteStartElement(xmlWriter, "Arguments", "ArgumentsPanel", "Arguments", "Constants that couldn't be extracted from HTML", "true"); JMeterWrapper.WriteStartElement(xmlWriter, "collectionProp", "Arguments.arguments"); foreach (var jMeterConstant in constants) { JMeterWrapper.WriteStartElement(xmlWriter, "elementProp", jMeterConstant.Name, "Argument"); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "Argument.name", jMeterConstant.Name); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "Argument.value", jMeterConstant.Value); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "Argument.metadata", "="); JMeterWrapper.WriteElementWithTextChildren(xmlWriter, "stringProp", "Argument.desc", jMeterConstant.Description); // </elementProp> xmlWriter.WriteEndElement(); } // </collectionProp> xmlWriter.WriteEndElement(); // </Arguments> xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("hashTree"); xmlWriter.WriteEndElement(); } } // </hashTree> xmlWriter.WriteEndElement(); }