Пример #1
0
 internal void AddParameterToExtract(JMeterRegExParameter newParam)
 {
     ParametersToExtract.Add(newParam);
 }
Пример #2
0
        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("'", "&apos;") : 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&apos;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();
        }
Пример #3
0
        internal void AddRequest(Session httpReq, Page page, bool secondary = true, bool beanShell = true, bool gxApp = false)
        {
            if (IsInBlackList(httpReq))
            {
                return;
            }

            // find the Page of the request in the Page tree
            if (page != null)
            {
                page = page.FindSubPage(httpReq);
            }

            var referer = httpReq.oRequest.headers["Referer"];

            // if isn't static resource. Can be a follow redirect of last request or a new request
            if (httpReq.IsPrimaryRequest())
            {
                // this means the current primaryRequest is first request of the step
                if (Requests.Count == 0)
                {
                    var req = CreatePageRequest(httpReq, this, page, secondary, beanShell, gxApp);
                    Requests.Add(req);
                }
                else
                {
                    var lastPageRequest = Requests.Last();

                    // todo: should use the referer request instead of LastPrimaryRequest?
                    var lastFollowRedirectPage          = lastPageRequest.GetLastPrimaryRequest();
                    var fiddlerSessionOfLastPageRequest = lastFollowRedirectPage.FiddlerSession;

                    // the response code of the previous request was a redirect
                    if (fiddlerSessionOfLastPageRequest.IsRedirectByResponseCode())
                    {
                        var followRedirect = new AbstractFollowRedirect(httpReq, RedirectType.ByResponseCode, page);
                        followRedirect.Validations.Add(CreateDefaultValidationFromRequest(httpReq));

                        // if there are parameters in the URL, need to extract them to use in current request
                        var urlParameters = GetParametersFromURL(httpReq.fullUrl);
                        if (urlParameters != string.Empty)
                        {
                            const string regExp = @"Location: http://.*\?(.*)";

                            // if there aren't parameters to extract and to use created in the comparer, then create it here
                            if (page == null)
                            {
                                var paramName = NameGenerator.GetInstance().GetNewName();

                                // add parameter to extract
                                var newParam = new JMeterRegExParameter(
                                    ExtractFrom.Headers,
                                    new List <UseIn> {
                                    UseIn.Url
                                },
                                    paramName,
                                    regExp,
                                    "$1$",
                                    urlParameters,
                                    "Used in pages: { " + httpReq.id + " } Original value: " + urlParameters);

                                lastFollowRedirectPage.AddParameterToExtract(newParam);

                                // modify url to use the parameter
                                followRedirect.AddParameterToUse(newParam);
                            }
                            else
                            {
                                var paramName = NameGenerator.GetInstance().GetNewName();

                                // there are alredy parameters to use in the URL, don't add a newone
                                if (page.GetParametersToUseInURL().Count == 0)
                                {
                                    var param = new Parameter
                                    {
                                        ExtractFromSection = FiddlerSessionComparer.ExtractFrom.Headers,
                                        ExtractFromPage    = lastFollowRedirectPage.InfoPage,
                                        VariableName       = paramName,
                                        Values             = new List <string> {
                                            urlParameters
                                        },
                                        ParamExtractor = new RegExpExtractor(1, regExp),
                                    };

                                    param.AddParameterPage(page, UseToReplaceIn.Url, urlParameters, "${" + paramName + "}");

                                    lastFollowRedirectPage.InfoPage.AddPreparedParameterToExtract(param, ParameterContext.Default);
                                    page.AddPreparedParameterToUse(param);
                                }
                            }
                        }

                        lastPageRequest.AddFollowRedirect(followRedirect);

                        lastFollowRedirectPage.Validations.Clear();
                        lastFollowRedirectPage.Validations.Add(CreateResponseCodeValidation(lastPageRequest.ResponseCode));
                    }
                    // the response code of the previous request was a redirect
                    else if (fiddlerSessionOfLastPageRequest.IsRedirectByJavaScript())
                    {
                        var validations = new List <AbstractValidation> {
                            CreateDefaultValidationFromRequest(httpReq)
                        };
                        lastPageRequest.AddFollowRedirect(httpReq, RedirectType.ByJavaScript, page, validations);
                        lastPageRequest.Validations.Clear();
                        lastPageRequest.Validations.Add(CreateResponseCodeValidation(200));

                        // Add validation to the followRedirect: HTTP "200" + Content "Redirect"
                        if (fiddlerSessionOfLastPageRequest.IsGenexusRedirect())
                        {
                            lastPageRequest.Validations.Add(CreateAppearTextValidation("redirect", "This HTML should be a javascript redirect", false, true));

                            // This is done just when comparing two fiddler sessions
                            ////var parametersExtractor = CreateRegExpExtractorToGetRedirectParameters(lastPageRequest.FiddlerSession.GetResponseBodyAsString());
                            ////if (parametersExtractor != null)
                            ////{
                            ////    lastPageRequest.ParametersToExtract.Add(parametersExtractor);
                            ////}
                        }
                    }
                    // the last request wasn't a redirect, it's a new request in the step
                    else
                    {
                        // Constructor creates PageRequest with default validations
                        var req = CreatePageRequest(httpReq, this, page, secondary, beanShell, gxApp);
                        Requests.Add(req);
                    }
                }
            }
            else
            {
                // If secondary request has referer header
                if (!String.IsNullOrEmpty(referer))
                {
                    var refererRequest = GetRequestByUrl(referer);

                    if (refererRequest != null)
                    {
                        refererRequest.AddSecondaryRequest(httpReq);
                    }
                    else
                    {
                        if (Requests.Count > 0)
                        {
                            Requests.Last().AddSecondaryRequest(httpReq);
                        }
                        else
                        {
                            // Add it as a PrimaryRequest
                            // may be an error on GetRequestByUrl() ?
                            var req = CreatePageRequest(httpReq, this, page, secondary, beanShell, gxApp);
                            Requests.Add(req);
                        }
                    }
                }
                else
                {
                    Requests.Last().AddSecondaryRequest(httpReq);
                }
            }
        }
Пример #4
0
 public void AddParameterToUse(JMeterRegExParameter parameter)
 {
     ParametersToUse.Add(parameter);
 }