Below is the response:
<div class="input radio_buttons optional challenger_order_selected"><span class="radio"><input class="radio_buttons optional" id="challenger_order_selected_eulcrnhkvss5r0tqtg5obeziwwewdz09ls1usjdodytuditqdmnowe5lskzpmdvbpt0--3fb112e512edd2f77187705cbefeb5c479c85a80" name="challenger[order_selected]" type="radio" value="eUlCRnhkVSs5R0tqTG5obEZIWWEwdz09LS1USjdOdytuditQdmNoWE5LSkZpMDVBPT0=--3fb112e512edd2f77187705cbefeb5c479c85a80" /><label class="collection_radio_buttons" for="challenger_order_selected_eulcrnhkvss5r0tqtg5obeziwwewdz09ls1usjdodytuditqdmnowe5lskzpmdvbpt0--3fb112e512edd2f77187705cbefeb5c479c85a80">54</label></span></div>
<div class="input radio_buttons optional challenger_order_selected"><span class="radio"><input class="radio_buttons optional" id="challenger_order_selected_dennu2ewd3dptc8wl08ya0tkblhtzz09ls1ime5hqw5yenvfmjjdmfkyk3fzse53pt0--12dc0d52ba07f91b2957ce4a64aca7c812087239" name="challenger[order_selected]" type="radio" value="dENnU2Ewd3dpTC8wL08ya0tkblhTZz09LS1IME5hQW5yenVFMjJDMFkyK3FzSE53PT0=--12dc0d52ba07f91b2957ce4a64aca7c812087239" /><label class="collection_radio_buttons" for="challenger_order_selected_dennu2ewd3dptc8wl08ya0tkblhtzz09ls1ime5hqw5yenvfmjjdmfkyk3fzse53pt0--12dc0d52ba07f91b2957ce4a64aca7c812087239">53</label></span></div>
<div class="input radio_buttons optional challenger_order_selected"><span class="radio"><input class="radio_buttons optional" id="challenger_order_selected_zc91djdxcxc3oxflazgvahqxnvbyzz09ls1mcxvtrfp1dtrfuu1jyuntrwpvcuznpt0--004c87cba6be163627a29ecf097145307e875ff0" name="challenger[order_selected]" type="radio" value="ZC91djdXcXc3OXFlazgvaHQxNVBYZz09LS1McXVTRFp1dTRFUU1jYUNtRWpVcUZnPT0=--004c87cba6be163627a29ecf097145307e875ff0" /><label class="collection_radio_buttons" for="challenger_order_selected_zc91djdxcxc3oxflazgvahqxnvbyzz09ls1mcxvtrfp1dtrfuu1jyuntrwpvcuznpt0--004c87cba6be163627a29ecf097145307e875ff0">20</label></span></div>
<div class="input radio_buttons optional challenger_order_selected"><span class="radio"><input class="radio_buttons optional" id="challenger_order_selected_djvlznvtuytgvhhpn1iybnpob1nwut09ls1zwtvfl2vprfe0awdkkzbnwhlcumxrpt0--a3d2052afedd2987a5e31cfb11996ed7b9bb28e5" name="challenger[order_selected]" type="radio" value="djVLZnVtUytGVHhPN1IybnpOb1NWUT09LS1ZWTVFL2VPRFE0aWdKKzBnWHlCUmxRPT0=--a3d2052afedd2987a5e31cfb11996ed7b9bb28e5" /><label class="collection_radio_buttons" for="challenger_order_selected_djvlznvtuytgvhhpn1iybnpob1nwut09ls1zwtvfl2vprfe0awdkkzbnwhlcumxrpt0--a3d2052afedd2987a5e31cfb11996ed7b9bb28e5">244</label></span></div>
<div class="input radio_buttons optional challenger_order_selected"><span class="radio"><input class="radio_buttons optional" id="challenger_order_selected_shbjtuz1ajz5c0xuqxfuutl0bzzwut09ls1iexj0svdlnuzbzhjta2oryvg4utvbpt0--4bcb59d227c1658800f0c2a4d9ca70c59b002d22" name="challenger[order_selected]" type="radio" value="SHBjTUZ1ajZ5c0xuQXFUUTl0bzZWUT09LS1IeXJ0SVdlNUZBZHJTa2orYVg4UTVBPT0=--4bcb59d227c1658800f0c2a4d9ca70c59b002d22" /><label class="collection_radio_buttons" for="challenger_order_selected_shbjtuz1ajz5c0xuqxfuutl0bzzwut09ls1iexj0svdlnuzbzhjta2oryvg4utvbpt0--4bcb59d227c1658800f0c2a4d9ca70c59b002d22">101</label></span></div>
i need to find the highest number from the list that is 244 and the respective dynamic value also that is "djVLZnVtUytGVHhPN1IybnpOb1NWUT09LS1ZWTVFL2VPRFE0aWdKKzBnWHlCUmxRPT0=--a3d2052afedd2987a5e31cfb11996ed7b9bb28e5".
can you please help me how to write bean-shell for the same
You can use XPath Extractor to:
Identify the label with the largest value
Get "value" attribute of the relevant input
Both points 1 and 2 can be done in a single expression
Example XPath expression which does above will look like:
//div/span/label[not(text() <= ../../preceding-sibling::div/span/label/text()) and not(text() <=../../following-sibling::div/span/label/text())]/../input/#value
Disclaimer: above expression will work only against response data mentioned in your question, if your actual response has different markup - it might not be accurate.
Evidence:
Warning: the XPath expression is very memory and resource intensive, use it wisely.
References:
XPath 1.0 Language Specification
XPath Tutorial
Using the XPath Extractor in JMeter
Put it in beanshell postprocessor:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String response = prev.getResponseDataAsString();
Pattern pattern = Pattern.compile("<div class=\"input radio_buttons optional challenger_order_selected\"><span class=\"radio\"><input class=\"radio_buttons optional\" id=\".+?\" name=\"challenger[order_selected]\" type=\"radio\" value=\"(.+?)\" /><label class=\"collection_radio_buttons\" for=\".+?\">(\d+?)</label></span></div>");
Matcher matcher = pattern.matcher(response);
Integer maxLabel, curLabel = 0;
String value = "";
while (matcher.find())
{
curLabel = Integer.valueOf(matcher.group(2));
if(curLabel>maxLabel)
{
maxLabel=curLabel;
value = matcher.group(1);
}
}
vars.put("value", value);
Related
I have a Spring boot App with a Thymeleaf Template in which I define a th:object="${guest}" and I try to access it with the Asterisk syntax like th:value="*{lastName}".
If th:object="${guest} is null it produces an Error: Property or field 'firstName' cannot be found on null. I understand why the error appears. But what is the best way to handle it?
Since I am using th:object="${guest} to prefill values in an form. My current working solution is:
<form th:if="${guest}" th:object="${guest}" ... >
<form th:unless="${guest} ... >
and this is the part I do not understand why can't Thmyleafe workout in the Asterisk that the object is null? But the th:if / th:unless statment gets that th:object is null. There is no case in which accessing an attribute of a null Object is suitable.
And if the object is not there at all it is fine.
Is there a better way with less code?
I also tried:
th:value="*{lastName}?:_" - same error.
th:object="${guest}:?_" - this cant be parsed at all.
Is there a way without the th:if / th:unless. The optimal solution would be to do it in one line so everything stays clean. Can I somehow parse an if in the th:object${}
I could not find something in the documentation or else on the web. Only if Asterisk or ${..} is null
Full Code:
The Fragment:
<form th:fragment="guest_form">
<input type="text" id="firstName" name="firstName" placeholder="Vorname" th:value="*{firstName}" required />
<input type="text" id="lastName" name="lastName" placeholder="Nachname" th:value="*{lastName}" required />
<input type="tel" id="phone" name="phone" placeholder="Telefon Nummer" th:value="*{phone}" />
<input type="email" id="email" name="email" placeholder="Emailadresse" th:value="*{email}" />
<input type="street" id="street" name="street" placeholder="Straße" th:value="*{street}" />
<input type="city" id="city" name="city" placeholder="Stadt" th:value="*{city}" />
<input type="zip" id="zip" name="zip" placeholder="Postleitzahl" th:value="*{zip}" />
<input type="hidden" name="code" th:value="${code}" />
<input th:replace="::csrf_token" />
<input type="submit" id="submitButton" value="Bestätigen" />
</form>
The fragment use:
<form th:include=" fragments/fragments.html::guest_form" th:if="${guest}" th:object="${guest}" action="/register" method="POST"></form>
<form th:include="fragments/fragments.html::guest_form" th:unless="${guest}" action="/register" method="POST"></form>
The function:
#PostMapping("/register")
public String register(#RequestParam String code, Model model, HttpSession session,
RedirectAttributes redirectAttributes, #ModelAttribute("guest") #Valid Guest g,
BindingResult bindingResult) {
model.addAttribute("guest", g);
model.addAttribute("code", code);
// form is not fine
if (bindingResult.hasErrors()) {
return "guest/register";
}
// form is not fine
guestRepo.save(g);
Booking booking = new Booking(g, invRepo.findByCode(code).get());
bookingRepo.save(booking);
session.setAttribute(saBooking, booking);
return "redirect:/ticket";
}
g can be null because you can get redirect to this url - to fill out the form - in this moment g is null. I think I could check if g == null an then Guest g = new Guest(); but this doesnt target my question...
I'm using Spring+Thymeleaf to see and modify the users in a database. I would like to set the input fields to the actual values of an original user but I've tried with different styles and it doesn't work.
With the present configuration I can update information of users and see the id of original user (it's not in a input field) but I can't show the actual configuration in input field as default.
CONTROLLER:
#GetMapping(value = {"/", ""})
public String subusersPage(HttpSession session, Model model) {
String idUser = BaseController.getLoggedUser(session);
UserDTO userDTO = userService.getUserById(idUser);
model.addAttribute("subusersDTO", userService.getSubusersDTO(userDTO.getSubusers()));
model.addAttribute("populations", userDTO.getPopulations());
model.addAttribute("configurations", userDTO.getConfigurations());
model.addAttribute("attributes", userDTO.getAttributes());
model.addAttribute("subuserDTO", new SubuserDTO());
return "subusers";
}
HTML:
<th:block th:each="subuserDTO_original : ${subusersDTO}">
<hr>
<form action="#" th:action="#{/subusers/__${subuserDTO_original.id}__}" th:object="${subuserDTO}" method="post">
<div>
<p th:text="${'Id: ' + subuserDTO_original.id}"></p>
<p>Name: <input type="text" th:field="*{name}" th:name="name" th:value="${subuserDTO_original.name}"/></p>
<p>Population: <input type="text" th:field="*{population}" th:name="population" th:value="${subuserDTO_original.population}"/></p>
<p>Configuration: <input type="text" th:field="*{configuration}" th:name="configuration" th:value="${subuserDTO_original.configuration}"/></p>
<p>Attributes: <input type="text" th:field="*{attributes}" th:name="attributes" th:value="${subuserDTO_original.attributes}"/></p>
<p>
<button type="submit" th:name="action" th:value="update">Update</button>
<button type="submit" th:name="action" th:value="delete">Delete</button>
<button type="reset" th:name="action" th:value="clear">Clear</button>
</p>
</div>
</form>
<form action="#" th:action="#{/subusers/__${subuserDTO_original.id}__}" method="get">
<button type="submit">Default</button>
</form>
</th:block>
Any help will be very appreciated, thank you!
If you want to edit an existing user, then your th:object (which is ${subuserDTO} in this case) needs to be populated with the values of the original user. This is because when you use the attribute th:field="*{name}", it actually overwrites the name, id, and value of the html tag (which is why th:value="${subuserDTO_original.name}" isn't working.
Two other options you could do:
You could also set name="name" and use th:value instead.
Or another option, you could use ${subuserDTO_original} as your th:object.
I have a form and I want to set default value in the field below but it's not working.
<span>ID User:</span>
<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />
</div>
<div>
<span class="name-in">ID Room:</span>
<input type="text" th:value="${id}" th:field="*{room}" th:errorclass="field-error" />
</div>
I read some topic about this problem and I try to change as given below
th:attr="value = ${session.name}"
But It's still not working. Field ID User is empty. I don't know how to solve this problem.
Although your question contain less information, but i think you want to put default value for all field. If you like to do so change
`<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />`
to
<input type="text" name="userid" value="as your wish" th:errorclass="field-error" />
Instead of changing the html, you should instead set the value of *{userid} in your controller. That way you can keep your html the same:
// Controller
modelObject.setUserId(session.name);
// HTML
<input type="text" th:field="*{userid}" th:errorclass="field-error" />
I want to parsing my label name="predictDataTemp" in form into my controller, I already set the value form my label, but when I want to request the data still null
content.blade.php
<div class="form-group" align="center">
<label for="exampleResult" name="result">Result</label>
<label for="examplePredict" id="predictData" class="form-control">
<input type="hidden" name="predictDataTemp">
</label>
</div>
controller
public function result(Request $request){
$this->validate($request,[
'mCalories'=>'required',
'mCholesterol'=>'required',
'mFat'=>'required',
'mProtein'=>'required',
'mSugars'=>'required'
]);
$item= array();
array_push($item,array('Calories'=>$request->mCalories,'Cholesterol'=>$request->mCholesterol,'Fat'=>$request->mFat,'Protein'=>$request->mProtein,'Sugars'=>$request->mSugars,'Predict'=>$request->predictDataTemp));
return json_encode($item);
}
Your input has no value.
If you want to give it a value with jQuery (looking at your previous comments)
Give the input an id
<input type="hidden" name="predictDataTemp" id="predictDataTemp">
Then assign it in jQuery
$('#predictDataTemp').val('pass value here');
label don't have name attribute, it has only two attribute for and form so you can pass value in hidden input tag, Read this article
Instead
<label type="text" for="examplePredict" id="predictData" name="predictDataTemp" class="form-control"></label>
Use this
<label type="text" for="examplePredict" class="form-control"></label>
<input type="hidden" name="predictDataTemp" id="predictData" value="something">
I got a big XML. A snippet of that XML look like this:
<div class="x-column-inner" id="ext-gen422" style="width: 850px;">
<div id="ext-comp-1206" style="width: 14px;" class=" x-column">
<div tabindex="-1" class="x-form-item x-hide-label" id="ext-gen434">
<label class="x-form-item-label" style="width:100px;" for="ext-comp-1180" id="ext-gen435"></label>
<div style="padding-left:105px" id="x-form-el-ext-comp-1180" class="x-form-element">
<div class="x-form-check-wrap" id="ext-gen436" style="width: 14px; height: 28px;">
<input type="checkbox" name="ext-comp-1180" id="ext-comp-1180" autocomplete="off" class=" x-form-checkbox x-form-field">
<label class="x-form-cb-label" for="ext-comp-1180" id="ext-gen437"> </label>
</div></div> <div class="x-form-clear-left">
</div>
</div>
</div>
<div id="ext-comp-1207" style="width: 150px;" class=" x-column">
<label id="ext-comp-1203" style="width: 140px;">Add to Watchlist</label>
</div>
<div id="ext-comp-1208" style="width: 107px;" class=" x-column">
I need to find 'input' node of checkbox type based on label node having text 'Add to Watchlist'.
As both 'input' and 'label' node lies in different hierarchy, // syntax doesn't seem to work:
//div[label[contains(text(),'Add to Watchlist')]]
will just give parent div of child label.
I tried to start from the topmost node of this snippet
$x("//div[#class='x-column-inner' and //label[contains(text(),'Add to Watchlist')]]")
but that is giving 6 possible matches.
Note: #id attribute can't be used as this is getting assigned dynamically to nodes so next time page loads #id will be different.
I don't want to use position() predicate as that makes XPATH static and xpath may break with any change in position.
You could try something like this, but it looks very greedy... Basically what it does is searching in every axes of the input tags to see if there is an associated label tag. So for each input it searches in its ancestors, descendants and siblings.
There are certainly some smarter solutions.
//input[#type = 'checkbox' and (#id = ancestor::label/#for or #id = descendant::label/#for or #id = following::label/#for or #id = preceding::label/#for)]
However your snippet is not interesting no input tag will be matched, please consider providing a better snippet. It would improve the answers accuracy.
Edit : Here is a (non-tested) way to add the 'Add to Watchlist' constraint.
//input[#type = 'checkbox' and (#id = ancestor::label[. = 'Add to Watchlist']/#for or #id = descendant::label[. = 'Add to Watchlist']/#for or #id = following::label[. = 'Add to Watchlist']/#for or #id = preceding::label[. = 'Add to Watchlist']/#for)]
But once again, those xpath requests are very greedy and your are not guaranteed to match every input element associated to a label for example the following input won't be match in this snippet:
<div>
<div>
<label for="id">Add to Watchlist</label>
</div>
<div>
<input type="checkbox" id="id" />
</div>
<div>
There may be more efficient solutions in one xpath request, but you should consider doing several request.
For example, one request to find every for attribute value of the label elements with the text 'Add to Watchlist' and then doing another request to find the associated input elements.
I should also try to restrict your request to the scope a the underlying form element. Perhaps I will edit with a better request if I find the time.
Edit 2
Here is a working and smarter request
//form//input[#type = 'checkbox' and #id = ancestor::form[1]//label[. = 'Add to Watchlist']/#for]
You can confront it to this snippet
<html>
<form>
<label for="bar">Add to Watchlist</label>
<div>
<div>
<label for="id">Add to Watchlist</label>
</div>
<div>
<input type="checkbox" id="id" />
<input type="checkbox" id="foo" />
<input type="checkbox" id="bar" />
<input type="checkbox" />
<input type="checkbox" id="" />
</div>
</div>
</form>
<label for="foo">Add to Watchlist</label>
</html>
Bust the most important is that you understand how it works and why it is better. Please take the time to think about it.