jemmy fx测试fx ui在单击按钮时给出了IllegalStateException

时间:2015-05-28 06:51:19

标签: java junit jemmyfx

我想测试我的javafx UI。我有一个Application.class,它具有main函数并加载一个场景(一个登录屏幕)。我的测试代码

@Before
public void startApp() throws InterruptedException {
    startApp(Application.class);
    scene = new SceneDock();
    this.username = new TextInputControlDock(scene.asParent(), "txtFieldUsername");
    this.password = new TextInputControlDock(scene.asParent(), "txtFieldPassword");
    this.btnLogin = new LabeledDock(scene.asParent(), "Login", StringComparePolicy.EXACT);
    this.btnCancel = new LabeledDock(scene.asParent(), "Cancel", StringComparePolicy.EXACT);

}

@Test
public void loginScreenMustContainTwoButtonsCancelAndLogin() throws Exception {
    assertEquals(Button.class, new LabeledDock(scene.asParent(), "Cancel",
            StringComparePolicy.EXACT).wrap().getControl().getClass());
    assertEquals(Button.class, new LabeledDock(scene.asParent(), "Login",
            StringComparePolicy.EXACT).wrap().getControl().getClass());
}

@Test
public void loginScreenMustContainTwoTextFieldsUsernameAndPassword() throws Exception {
    TextInputControlDock username = new TextInputControlDock(scene.asParent(), "txtFieldUsername");
    TextInputControlDock password = new TextInputControlDock(scene.asParent(), "txtFieldPassword");
    assertTrue(username.wrap().getControl() instanceof TextField);
    assertTrue(password.wrap().getControl() instanceof PasswordField);      
}


@Ignore
@Test(expected=TimeoutExpiredException.class)
public void loginWindowHasAnErrorLabel() throws Exception {
    NodeDock errorLabel = new NodeDock(scene.asParent(), Label.class, "lblErrorMessage");
    assertTrue(errorLabel.wrap().getControl() instanceof Label);
}

@Test
public void loginButtonWithNoInputShowsErrorText() throws Exception {
    log.debug("Clicking login button");
    btnLogin.wrap().mouse().click(1);
    log.debug(scene);

}

private void startApp(Class<AvalancheClient> app) {
    // TODO Auto-generated method stub
    AppExecutor.executeNoBlock(app);
}

我添加此文loginButtonWithNoInputShowsErrorText后,我总是收到以下错误

Exception in thread "Thread-7" java.lang.IllegalStateException: Application launch must not be called more than once

为什么会这样。我的代码基于我在互联网上找到的openjfx样本,因为我还没有找到关于jemmyfx的分析文档和参考资料。你能帮帮我一点吗?

1 个答案:

答案 0 :(得分:0)

我发现错误了...... AppExecutor必须在@BeforeClass注释方法(静态)中调用,而不是在@Before方法中调用。它现在有效。