InputAgent
Type
InputAgent can be used on any input component such as textbox or datebox. Those components that users can type string in, we can mimic it with InputAgent.type(String) . Those are not able to type in string like slider, we should use InputAgent.input(Object) .
We use a todo list application to demonstrate InputAgent usage. Here is the application's UI:
The following test case verifies "Add" function, we enter values into 3 fields: item name, priority, and date, and click "Add" button. Then we inspect each listcell of a listitem to verify that a to-do item is added to the listbox.
TodoTest.java
public class TodoTest {
@Test
public void test() {
//visit the target page
DesktopAgent desktop = Zats.newClient().connect("/todo.zul");
//find components
ComponentAgent itemName = desktop.query("textbox");
ComponentAgent priority = desktop.query("intbox");
ComponentAgent date = desktop.query("datebox");
//add
//itemName.as(InputAgent.class).type("one-item");
itemName.type("one-item");
priority.type("3");
date.type("2012-03-16");
desktop.query("button[label='Add']").click();
//verify each listcell's label
ComponentAgent listbox = desktop.query("listbox");
List<ComponentAgent> cells = listbox.queryAll("listitem").get(0).getChildren();
assertEquals("one-item",cells.get(0).as(Listcell.class).getLabel());
assertEquals("3",cells.get(1).as(Listcell.class).getLabel());
assertEquals("2012/03/16",cells.get(2).as(Listcell.class).getLabel());
}
}
- The formal usage of InputAgent is to retrieve from a ComponentAgent . (line 14)
- As seen in the previous example, this is also a shortcut method. (line 15)
- Although priority is an intbox, we still provide a String as the parameter. The string will be parsed to an integer internally, if failed we'll get an exception. (line 16)
- When typing in a Datebox, use the date format that you have specified in Datebox's "format" attribute. The same rule applies to timebox. (line 17)
- The query syntax means "retrieve a button whose label is 'Add'". (line 18)
- If we call ComponentAgent.query() , it'll only query the ComponentAgent's child components. Here, we find listitem to get listcell. (line 22)
Typing
If you want to mimic one user is typing, you should use InputAgent.typing(String) . It's similar as type(String) but it triggers onChanging event instead of onChange event.
For example, to achieve auto-complete feature, developers usually listen onChanging event of a textbox and perform post-processing.
desktopAgent.query("textbox").as(InputAgent.class).typing("a");
Select
select(int, int) is used to mark selection of a range of text by starting and ending index (ending index is exclusive).
desktopAgent.query("textbox").as(Input.class).select(0,3);
Input
Among input components, only slider that a user cannot type text in. So we provide a method to generalize input operation. Users don't have to care how the value is inputted into a component, but only care what value. Because slider accepts integer input, we should pass int as a parameter.
desktop.query("slider").as(InputAgent.class).input(40);
Supported Components
Bandbox, Combobox, Textbox | 5, 6 | |
Datebox, Decimalbox, Doublebox, Doublespinner, Intbox, Longbox, Spinner, Timebox | 5, 6 | input string should match "format" attribute's pattern |
CKEditor | 5, 6 | only support type(String) , typing(String) |
Colorbox | 5, 6 | only support type(String) |
Slider | 5, 6 | only support Input(Object) |