有没有办法在 SWT-Widgets 上自动生成 ID,以便 UI-Tests 可以引用它们?我知道我可以使用 seData 手动设置 id,但我想以某种通用的方式为现有应用程序实现此功能。
问问题
1048 次
1 回答
8
Display.getCurrent().getShells();
您可以使用和递归地为应用程序中的所有 shell 分配 ID Widget.setData();
。
设置 ID
Shell []shells = Display.getCurrent().getShells();
for(Shell obj : shells) {
setIds(obj);
}
您可以使用方法访问应用程序中所有活动的(未释放的)Shell Display.getCurrent().getShells();
。您可以遍历每个的所有子级,并使用方法Shell
为每个子级分配一个 ID 。Control
Widget.setData();
private Integer count = 0;
private void setIds(Composite c) {
Control[] children = c.getChildren();
for(int j = 0 ; j < children.length; j++) {
if(children[j] instanceof Composite) {
setIds((Composite) children[j]);
} else {
children[j].setData(count);
System.out.println(children[j].toString());
System.out.println(" '-> ID: " + children[j].getData());
++count;
}
}
}
如果Control
是 aComposite
它可能在复合材料中具有控件,这就是我在示例中使用递归解决方案的原因。
按 ID 查找控件
现在,如果您想在其中一个 shell 中找到一个 Control,我会建议一种类似的递归方法:
public Control findControlById(Integer id) {
Shell[] shells = Display.getCurrent().getShells();
for(Shell e : shells) {
Control foundControl = findControl(e, id);
if(foundControl != null) {
return foundControl;
}
}
return null;
}
private Control findControl(Composite c, Integer id) {
Control[] children = c.getChildren();
for(Control e : children) {
if(e instanceof Composite) {
Control found = findControl((Composite) e, id);
if(found != null) {
return found;
}
} else {
int value = id.intValue();
int objValue = ((Integer)e.getData()).intValue();
if(value == objValue)
return e;
}
}
return null;
}
使用该方法,您可以通过它的 IDfindControlById()
轻松找到一个。Control
Control foundControl = findControlById(12);
System.out.println(foundControl.toString());
链接
于 2012-12-05T12:47:50.963 回答