How to test jmeter's response and pass parameters? - java

I am researching jmeter and I have a question.
My first question: in case : (github )
input autocapitalize="off" autocorrect="off" autofocus="autofocus" class="form-control input-block" id="login_field" name="login" tabindex="1" type="text"
input class="form-control form-control input-block" id="password" name="password" tabindex="2" type="password"
In case the website don't have field "name", how can I pass the param to website ? can we use css or xpath to pass the param to website ?
My second question:
How to test the response value from the site ? (from the picture the response data not right, still on login page)
Thanks for reading and supporting me to correct this ...

As per HTML Forms article
The Name Attribute
To be submitted correctly, each input field must have a name attribute.
Actually you should't worry about HTML markup, JMeter acts on protocol level and provides record-and-replay functionality. See Apache JMeter Proxy Step by Step for configuration instructions.
You can use Response Assertion to add a check whether response is still login page or not. For example if the user is logged in - he shouldn't see username input any more. See How to Use JMeter Assertions in Three Easy Steps article for more information on conditionally failing JMeter samplers.

Related

Jmeter Xpath and Regular expression

I need to extract jsessionid value only 8969424C7F388504711149044AB03FE9 from the below code
<input type="hidden" name="execution" value="e1s1" />
<input type="hidden" name="_eventId" value="submit" />
<a id="forgotP" href='forgotPassword?service=https%3A%2F%2Fbg-cn-host%3A8443%2Fcloudsolutions%2Fj_spring_cas_security_check%3Bjsessionid%3D8969424C7F388504711149044AB03FE9'>Forgot password?</a>
<input class="btn-submit" name="submit" value="Log In
I tried following options but no luck
//*[starts-with(#href,'forgotPassword')]/substring-after(#href, 'jsessionid%3D')
substring-after(/#href, 'jsessionid%3D')
//*[#id='forgotP']/contains(substring-after(., 'jsessionid%3D'))
name="(.+)"\stype="hidden"value="(.+)"
Need both Xpath and Regular expression
Correct XPath Expression would be:
substring-after(//a[starts-with(#href,'forgot')]/#href,'jsessionid%3D')
If your response is not XML/XHTML-compliant make sure that "Use Tidy (tolerant parser)" box is checked
You can test XPath Expressions against response data using View Results Tree listener.
However I believe you can do it a lot easier. Given JSESSIONID is just a cookie, it should be perfectly handled by the HTTP Cookie Manager, besides XPath Extractor is known to be resource intensive and it is recommended to avoid using it where possible.
Add the following line to user.properties file (lives under /bin folder of your JMeter installation)
CookieManager.save.cookies=true
Restart JMeter
Add HTTP Cookie Manager to your Test Plan
Wherever you need JSESSIONID value you can use ${COOKIE_JSESSIONID} variable
For more information see Using the HTTP Cookie Manager guide.
Try this Regex,
3Bjsessionid\%3D(\w+)'>Forgot password
or
3Bjsessionid\%3D([0-9A-Z]+)'>Forgot password
I am not giving JMeter regex snapshot assuming you know Regex extractor in JMeter. For Xpath I think your Xpath query is correct.

Mule: Http->xml

Mule version: 3,5 (Actually, mule studio)
I'm new at using mule and can't seem to figure out how to do the following properly:
Want to achieve:
Send data from a html form to mule which then splits the payload and sends one part to one java program and the other to another.
Html form:
<form method="GET" action="http://localhost:8081">
<input type="text" name="name"/>
<input type="text" name="lastname"/>
<input type="submit" value="Submit" />
</form>
This points to localhost and the port which is set in MuleStudio.
Real Question:
What could I use to transform the data from the inbound http to xml?
Side Question:
I can send the whole payload to one javaprogram. (first a POJO and then to the actual program. - is this the right way or can I send it to the program and skipping the POJO?)
Since your main concern is about transformation, let's only address it.
If you're using the Enterprise Edition, you can use DataMapper to generate the XML you need. Creating a schema representing the target XML would help a lot.
Otherwise, if you're using the Community Edition, you can use different options:
A Groovy component using the excellent MarkupBuilder to generate the target XML right from the inbound message payload,
A chain of standard transformers that first transform the submitted data to a generic XML form (object-to-xml-transformer) then transform it to the desired form (xslt-transformer).
I'm sure there are other options, but these should get you going :)

Trying to use a #RequestParam field in Spring form [duplicate]

Consider this form:
<form action="http://www.blabla.com?a=1&b=2" method="GET">
<input type="hidden" name="c" value="3" />
</form>
When submitting this GET form, the parameters a and b are disappearing.
Is there a reason for that?
Is there a way of avoiding this behaviour?
Isn't that what hidden parameters are for to start with...?
<form action="http://www.example.com" method="GET">
<input type="hidden" name="a" value="1" />
<input type="hidden" name="b" value="2" />
<input type="hidden" name="c" value="3" />
<input type="submit" />
</form>
I wouldn't count on any browser retaining any existing query string in the action URL.
As the specifications (RFC1866, page 46; HTML 4.x section 17.13.3) state:
If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.
Maybe one could percent-encode the action-URL to embed the question mark and the parameters, and then cross one's fingers to hope all browsers would leave that URL as it (and validate that the server understands it too). But I'd never rely on that.
By the way: it's not different for non-hidden form fields. For POST the action URL could hold a query string though.
In HTML5, this is per-spec behaviour.
See Association of controls and forms - Form submission algorithm.
Look at "4.10.22.3 Form submission algorithm", step 17. In the case of a GET form to an http/s URI with a query string:
Let destination be a new URL that is equal to the action except that
its <query> component is replaced by query (adding a U+003F QUESTION
MARK character (?) if appropriate).
So, your browser will trash the existing "?..." part of your URI and replace it with a new one based on your form.
In HTML 4.01, the spec produces invalid URIs - most browsers didn't actually do this though...
See Forms - Processing form data, step four - the URI will have a ? appended, even if it already contains one.
What you can do is using a simple foreach on the table containing the GET information. For example in PHP :
foreach ($_GET as $key => $value) {
$key = htmlspecialchars($key);
$value = htmlspecialchars($value);
echo "<input type='hidden' name='$key' value='$value'/>";
}
As the GET values are coming from the user, we should escape them before printing on screen.
You should include the two items (a and b) as hidden input elements as well as C.
I had a very similar problem where for the form action, I had something like:
<form action="http://www.example.com/?q=content/something" method="GET">
<input type="submit" value="Go away..." />
</form>
The button would get the user to the site, but the query info disappeared so the user landed on the home page rather than the desired content page. The solution in my case was to find out how to code the URL without the query that would get the user to the desired page. In this case my target was a Drupal site, so as it turned out /content/something also worked. I also could have used a node number (i.e. /node/123).
If you need workaround, as this form can be placed in 3rd party systems, you can use Apache mod_rewrite like this:
RewriteRule ^dummy.link$ index.php?a=1&b=2 [QSA,L]
then your new form will look like this:
<form ... action="http:/www.blabla.com/dummy.link" method="GET">
<input type="hidden" name="c" value="3" />
</form>
and Apache will append 3rd parameter to query
When the original query has array, for php:
foreach (explode("\n", http_build_query($query, '', "\n")) as $keyValue) {
[$key, $value] = explode('=', $keyValue, 2);
$key = htmlspecialchars(urldecode($key), ENT_COMPAT | ENT_HTML5);
$value = htmlspecialchars(urldecode($value), ENT_COMPAT | ENT_HTML5);
echo '<input type="hidden" name="' . $key . '" value="' . $value . '"' . "/>\n";
}
To answer your first question yes the browser does that and the reason is
that the browser does not care about existing parameters in the action URL
so it removes them completely
and to prevent this from happening use this JavaScript function that I wrote
using jQuery in:
function addQueryStringAsHidden(form){
if (form.attr("action") === undefined){
throw "form does not have action attribute"
}
let url = form.attr("action");
if (url.includes("?") === false) return false;
let index = url.indexOf("?");
let action = url.slice(0, index)
let params = url.slice(index);
url = new URLSearchParams(params);
for (param of url.keys()){
let paramValue = url.get(param);
let attrObject = {"type":"hidden", "name":param, "value":paramValue};
let hidden = $("<input>").attr(attrObject);
form.append(hidden);
}
form.attr("action", action)
}
My observation
when method is GET and form is submitted, hidden input element was sent as query parmater. Old params in action url were wiped out. So basically in this case, form data is replacing query string in action url
When method is POST, and form is submitted, Query parameters in action url were intact (req.query) and input element data was sent as form data (req.body)
So short story long, if you want to pass query params as well as form data, use method attribute as "POST"
This is in response to the above post by Efx:
If the URL already contains the var you want to change, then it is added yet again as a hidden field.
Here is a modification of that code as to prevent duplicating vars in the URL:
foreach ($_GET as $key => $value) {
if ($key != "my_key") {
echo("<input type='hidden' name='$key' value='$value'/>");
}
}
Your construction is illegal. You cannot include parameters in the action value of a form. What happens if you try this is going to depend on quirks of the browser. I wouldn't be surprised if it worked with one browser and not another. Even if it appeared to work, I would not rely on it, because the next version of the browser might change the behavior.
"But lets say I have parameters in query string and in hidden inputs, what can I do?" What you can do is fix the error. Not to be snide, but this is a little like asking, "But lets say my URL uses percent signs instead of slashes, what can I do?" The only possible answer is, you can fix the URL.
I usually write something like this:
foreach($_GET as $key=>$content){
echo "<input type='hidden' name='$key' value='$content'/>";
}
This is working, but don't forget to sanitize your inputs against XSS attacks!
<form ... action="http:/www.blabla.com?a=1&b=2" method ="POST">
<input type="hidden" name="c" value="3" />
</form>
change the request method to' POST' instead of 'GET'.

How can I call setParameter in a request object?

This is probably due to my misunderstanding and incomplete information of JSP and JSTL. I have a web page where I have input elements such as
<input name="elementID" value="${param.elementID}"/>
When I am trying to save the form, I check for that elementID and other elements to conform to certain constraints "numeric, less than XXX". I show an error message if they don't. All the parameters are saved and user does not need to type it again after fixing the error.
After saved, when I am redirecting to the same page for the object to be edited, I am looking a way to set the parameter like request.setParameter("elementID",..) Is there a way to do this ? However the only thing I can find is request.setAttribute.
HTTP responses does not support passing parameters.
JSP/Servelets allows you to either use request.setAttribute or session.setAttribute for that purpose. Both methods are available when processing the page you're redirecting to, So basically, you got it right...
Also, from what you describe, you may want to check client-side validation: don't submit the form until you're validating it using client-side scripting (javascript)
After the servlet processes the form, (ie. saves the user input in the database), have the servlet forward (not redirect, because that would lose the request params) the request to the same jsp which contains the form. So there is no need to set the params since the servlet is just passing back the same request object.
The jsp which contains the form should have inputs similar to this:
<form>
...
<input type="text" value="${elementid}"/>
...
</form>
The syntax ${varname} is EL. So if the elementid already has a value, it that textfield will contain that value. Alternatively if you have not used EL and/or JSTL, you use scriptlets (but that is highly unadvisable, EL and/or JSTL should be the way):
<form>
...
<input type="text" value="<%= request.getParameter("elementid") %>"/>
...
</form>
I had to include <%# page isELIgnored="false"%> to my jsp to allow code like ${elementid} to work

How do I send data between servlets?

I am pretty new to servlets and web development in general.
So basically I have a servlet that queries a database and returns some values, like a name. What I want is to turn the name into a link that opens a details page for that name (which another servlet would handle). How can I send the name to the other servlet so it can query a database for the relevant details?
Maybe I'm taking the wrong approach?
Edit: I am using Tomcat 5.5
Pass it as request parameter.
Either add it to the query string of the URL of the link to the other servlet which is then available by request.getParameter("name") in the doGet() method.
link
Or add it as a hidden input field in a POST form which submits to the other servlet which is then available by request.getParameter("name") in the doPost() method.
<form action="otherservlet" method="post">
<input type="hidden" name="name" value="${name}" />
<input type="submit" />
</form>
See also:
Servlets info page - contains a Hello World
Not sure if I understand correctly, but you may look at javax.servlet.RequestDispatcher and forward the url to the second servlet.
The url could be created using the name:
http://myhost.mydomain/my.context/servlet2.do?name=John
I would create the URL either in the first servlet or in a client using a configurable template for the URL. This way both servlets are clearly separated - you can even have each one on different machine.

Categories