Setting value of a PrimeFaces selectOneMenu
dherben Feb 28, 2012 7:18 AMHello all,
Has anyone found a way to test a Primefaces SelectOneMenu component using JSFUnit 2 / HtmlUnit? I can't select a value, and I can't get access to displayed values either.
First of all, the primefaces component renders a selectOneMenu, used for VAT percentage selection in a product form, as follows:
<div id="productDetailForm:productVatPercentage" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all ui-helper-clearfix" style="width: 75px; "> <div class="ui-helper-hidden-accessible"> <select id="productDetailForm:productVatPercentage_input" name="productDetailForm:productVatPercentage_input"> <option value="0.0">0.0%</option> <option value="6.0">6.0%</option> <option value="12.0" selected="selected">12.0%</option> <option value="21.0">21.0%</option> </select> </div> <input type="text" name="productDetailForm:productVatPercentage_editableInput" class="ui-selectonemenu-label ui-inputfield ui-corner-all" tabindex="-1" value="21.0" style="cursor: pointer; width: 59px; "> <div class="ui-selectonemenu-trigger ui-state-default ui-corner-right"> <span class="ui-icon ui-icon-triangle-1-s"></span> </div> </div>
When I check the values being sent over the wire in POST, I see the following:
productDetailForm:productVatPercentage_input:12.0 productDetailForm:productVatPercentage_editableInput:12.0%
So I've used the following java code:
private void selectVat(String percentage, JSFClientSession client) {
HtmlSelect vatPct =
(HtmlSelect) client.getElement("productDetailForm:productVatPercentage_input");
// set selected (percentage = "12.0")
vatPct.getOptionByValue(percentage).setSelected(true);
// unset default, just to make sure
vatPct.getOptionByValue("21.0").setSelected(false);
// make sure that 12.0 is the selected one
assertEquals(1, vatPct.getSelectedOptions().size());
assertEquals("12.0", vatPct.getSelectedOptions().get(0).getValueAttribute());
// get access to _editableInput field via parent
HtmlElement parent =
(HtmlElement) client.getElement("productDetailForm:productVatPercentage");
HtmlInput input = (HtmlInput) parent.getElementsByTagName("input").get(0);
input.setValueAttribute(percentage + "%");
}
However, when I submit the form and check the saved values, I see that the VAT percentage is set to the default 21.0% in the newly created product. I have made sure that the backend works: the correct percentage is saved when I run this in the browser.
If anyone has any idea on how to solve this issue, it would be much appreciated.
Thanks in advance,
Davy