widgetの検索方法
下記staticのimportを行う。import static org.hamcrest.Matchers.*;
import static org.eclipse.swtbot.swt.finder.matcher.WidgetMatcherFactory.*;
メソッド | 意味 |
---|---|
withText | 引数のテキストを持つwidgetを検索 |
withMnemonic | |
withLabel | 引数のラベルを持つwidgetを検索 |
withRegex | 正規表現を使ってwidgetを検索 |
inGroup | 引数で指定したSWTグループの中のwidgetを検索 |
widgetOfType | 指定した型のwidgetを検索 |
withStyle | 指定したStyle bitを持つwidgetを検索 |
withId | 指定したIDを持つwidgetを検索 |
- //テストのCTabItemを検索
- Matcher matcher = allOf(widgetOfType(CTabItem.class),withText("テスト"))))
- //検索ボタンを検索する場合
- matcher = allOf(withRegex("検索"), widgetOfType(Button.class),withStyle(SWT.PUSH, "SWT.PUSH"));
- //fooで始まる文字、もしくはbarグループに属するボタン型のwidget
- Matcher matcher = allOf(widgetOfType(Button.class), anyOf(withRegex("^foo.*"), inGroup("bar")));
自分でMatcherを作る場合
org.hamcrest.BaseMatcherを継承して、WithId matcherを作成する。- public class WithId extends BaseMatcher {
- private final String key;
- private final String value;
- public WithId(String key, String value) {
- this.key = key;
- this.value = value;
- }
- public boolean matches(Object obj) {
- if (obj instanceof Widget)
- return value.equals(((Widget) obj).getData(key));
- return false;
- }
- public void describeTo(Description description) {
- description.appendText("with key: ").appendText(key).appendText(" having value: ").appendText(value);
- }
- }
参考サイト