[英]TestFX with JUnit4: How can I press the ENTER key in a certain TextField?
I want to test, if a certain TextField
(maybe there are several TextFields
) has an EventHandler
set via setOnAction
.我想测试某个
TextField
(可能有多个TextFields
)是否通过setOnAction
设置了EventHandler
。 In the test code I can set the content (eg "HelloWorld") into the TextField
.在测试代码中,我可以将内容(例如“HelloWorld”)设置到
TextField
中。 In my understanding I have to place the curser at the end of the text in the TextField and then, call press(KeyCode.ENTER)
.据我了解,我必须将光标放在 TextField 中文本的末尾,然后调用
press(KeyCode.ENTER)
。 Is there a TestFX call to place the curser aa certain point within a certain TextField?是否有 TestFX 调用将光标放置在某个 TextField 中的某个点? Or is there another way to test this?
还是有另一种方法来测试这个?
Regards, Jörg问候,约尔格
[EDIT] Here is an example: [编辑] 这是一个例子:
package testestfx;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
public class SimpleGui extends VBox {
private static final org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(SimpleGui.class);
private TextField field1;
private TextField field2;
private Label label;
public SimpleGui() {
field1 = new TextField("Textfield1");
field2 = new TextField("Textfield2");
field1.setOnAction((event) -> onActionDoThis());
field2.setOnAction((event) -> onActionDoThat());
label = new Label("Label");
getChildren().addAll(field1, field2 );
}
public TextField getField1() {
return field1;
}
public TextField getField2() {
return field2;
}
public Label getLabel() {
return label;
}
void onActionDoThis() {
logger.info("This");
label.setText("This");
}
void onActionDoThat() {
logger.info("That");
label.setText("That");
}
}
And here is the test:这是测试:
package testtestfx;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
import org.junit.Test;
import org.testfx.framework.junit.ApplicationTest;
import testestfx.SimpleGui;
import static org.junit.Assert.assertEquals;
public class TestSimpleGUI extends ApplicationTest {
private static final org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(TestSimpleGUI.class);
private SimpleGui gui;
@Override
public void start(final Stage stage) throws Exception {
super.start(stage);
gui = new SimpleGui();
stage.setScene(new Scene(gui));
stage.show();
}
@Test
public void testEnterKeystroke() {
TextField t2 = gui.getField2();
TextField t1 = gui.getField1();
Label l1 = gui.getLabel();
t2.setText("bar");
t1.setText("foo");
press(KeyCode.ENTER);
assertEquals("That", l1.getText());
}
}
The console output is:控制台output是:
15:43:29.366 [JavaFX Application Thread] [INFO] SimpleGui - This
org.junit.ComparisonFailure:
Expected :That
Actual :This
```
You can set the focus on a TextField like this:您可以像这样将焦点设置在 TextField 上:
myTextField.requestFocus();
kind regards亲切的问候
Amadeus阿玛迪斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.