I'm trying to test onChange even on one dropdown so that the value is changed
in the other dropdown.When I submit the form I'm not able to get the values for
any of the fields.
I have seen people getting the same error but there is no solution so far.
Thanks for helping in advance.
@Test
public void testDummyPage() throws Exception
{
TestPage testPage = new TestPage("id");
wicketTester.startPage(testPage);
// assert rendered field components
wicketTester.assertComponent("form:txtName", TextField.class);
wicketTester.assertComponent("form:department", DropDownChoice.class);
wicketTester.assertComponent("form:employee", DropDownChoice.class);
FormTester formTester1 = wicketTester.newFormTester("form");
formTester1.setValue("txtName", "amit");
formTester1.select("department", 0);
wicketTester.executeAjaxEvent("form:department", "onchange");
wicketTester.assertComponentOnAjaxResponse("form:employee");
formTester1.select("employee", 1);
formTester1.submit("submit");
Assert.assertEquals(formTester1.getTextComponentValue("txtName"),
"amit");
Assert.assertNotNull(wicketTester.getTagByWicketId("department").getValue());
Assert.assertNotNull(wicketTester.getTagByWicketId("employee").getValue());
DropDownChoice dropDownChoice = (DropDownChoice)
wicketTester.getComponentFromLastRenderedPage(
"form:department");
System.out.println(" department " +
formTester1.getForm().get("department"));
Assert.assertEquals((dropDownChoice.getModelObjectAsString()), "HR");
}
**************
public class TestPage extends WebPage
{
private String txtName;
private String department;
private String deptName;
private String deptId;
private String employee;
private DropDownChoice ddc1;
public TestPage(String id)
{
final Form addressForm = new Form("form", new
CompoundPropertyModel(this));
addressForm.setOutputMarkupId(true);
add(addressForm);
/*Address TextField*/
TextField addressTxtField = new TextField("txtName",
new PropertyModel(this, "txtName"), String.class);
addressTxtField.add(StringValidator.maximumLength(15));
addressForm.add(addressTxtField);
List<Dept> depList = new ArrayList();
Dept dept = new Dept();
dept.setDeptId("1");
dept.setDeptName("HR");
depList.add(dept);
Dept dept1 = new Dept();
dept1.setDeptName("AC");
dept1.setDeptId("2");
Dept dept2 = new Dept();
dept2.setDeptName("AD");
dept2.setDeptId("3");
depList.add(dept1);
depList.add(dept2);
ChoiceRenderer choiceRendererState = new ChoiceRenderer("deptName",
"deptId");
//addressForm.add(new DropDownChoice("department", new
PropertyModel(this, "department"), depList,
//choiceRendererState));
final DropDownChoice ddc = new DropDownChoice("department", new
PropertyModel(this, "department"), Arrays.<String>asList(
"HR", "IS", "FINANCE/ACCOUNTING"));
addressForm.add(ddc.setOutputMarkupId(true));
ddc1 = new DropDownChoice("employee", new PropertyModel(this,
"employee"), Arrays.<String>asList(
"EMP1", "EMP2", "EMP3"));
addressForm.add(ddc1.setOutputMarkupId(true));
ddc.add(new AjaxFormComponentUpdatingBehavior("onchange")
{
@Override
protected void onUpdate(AjaxRequestTarget target)
{
if (ddc.getModelObject() != null)
{
target.addComponent(ddc1.setChoices(Arrays.<String>asList("EMP1", "EMP2")));
}
}
});
addressForm.add(new Button("submit")
{
protected void onSubmit(AjaxRequestTarget target, Form form)
{
}
});
}
public String getTxtName()
{
return txtName;
}
public void setTxtName(String txtName)
{
this.txtName = txtName;
}
public String getDepartment()
{
return department;
}
public void setDepartment(String department)
{
this.department = department;
}
public String getDeptName()
{
return deptName;
}
public void setDeptName(String deptName)
{
this.deptName = deptName;
}
public String getDeptId()
{
return deptId;
}
public void setDeptId(String deptId)
{
this.deptId = deptId;
}
public String getEmployee()
{
return employee;
}
public void setEmployee(String employee)
{
this.employee = employee;
}
}
***********
TestPage.html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form wicket:id="form">
<input type="text" value="shalini" wicket:id="txtName">
<select wicket:id="department" />
<select wicket:id="employee" />
<input type="submit" value="submit" wicket:id="submit">
</form>
</body>
</html>
> Mit freundlichen Grüßen / Best regards
>
> BeQIK
>
> Shalini Verma
> HCTM/ETA
>
> Tel. +011 (650) 690 9211
>
>