[英]Choose one of three identical swing components using AssertJ
我使用AssertJ测试我的swing应用程序。 当我尝试使用此代码时
frame.button(JButtonMatcher.withText("text").andShowing()).click();`
我收到此错误:
Found more than one component using matcher org.assertj.swing.core.matcher.JButtonMatcher[
name=<Any>, text='text', requireShowing=true]
因为我以一种形式具有三个相同的组件,所以不能更改该形式的名称或标题。 有什么建议吗?
最好和最简单的解决方案是,如果您可以给按钮指定不同的名称。
请记住:组件的name
与它显示的text
是不同的! 您的按钮可能全部向用户显示“文本”,但仍具有诸如“ button1”,“ button2”,“ button3”之类的名称。
在这种情况下,您可以写
frame.button(JButtonMatcher.withName("button1").withText("text").andShowing()).click();
下一种可能性是给包含按钮的面板指定不同的名称,例如“ panel1”,“ panel2”,“ panel3”。
如果可以实现,则可以编写
frame.panel("panel1").button(JButtonMatcher.withText("text").andShowing()).click();
最后一种也是最糟糕的情况是编写自己的GenericTypeMatcher
/ NamedComponentMatcherTemplate
,该NamedComponentMatcherTemplate
仅返回与给定文本匹配的第n
个按钮。
请注意 :
有了这些警告后,代码如下:
public class MyJButtonMatcher extends NamedComponentMatcherTemplate<JButton> {
private String text;
private int index;
public MyJButtonMatcher(String text, int index) {
super(JButton.class);
this.text = text;
this.index = index;
requireShowing(true);
}
@Override
protected boolean isMatching(JButton button) {
if (isNameMatching(button.getName()) && arePropertyValuesMatching(text, button.getText())) {
return index-- == 0;
} else {
return false;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.