带有 TestFX 的 TornadoFX 在每个 TestCase 后关闭视图

TornadoFX with TestFX close the View after every TestCase

我正在尝试使用 testfx 框架测试基本登录屏幕(使用 tornadofx 创建)。

我添加了 3 个测试用例,运行 没问题,但问题是它们使用的是前一个阶段,而不是创建一个新阶段。我希望测试用例独立 运行。

我正在测试 View() 而不是 App()。如果我使用 MyMainApp().start(stage) 然后使用 MyMainApp().stop(),我会得到所需的行为。
但是如何为视图和片段执行此操作。

下面是代码:

class LoginScreenFeatureTest : ApplicationTest() {

    override fun init() {
        FxToolkit.registerStage { Stage() }
    }

    override fun start(stage: Stage) {
        LoginScreen().openWindow()
        //MyMainApp().start(stage)
    }

    override fun stop() {
        FxToolkit.cleanupStages()
        //FxToolkit.toolkitContext().registeredStage.close()
        //MyMainApp().stop()
    }

    @Test fun should_contain_button() {
        // expect:
        verifyThat("#submitBut", hasText("SUBMIT"))
    }

    @Test fun should_click_on_button_and_pass_login() {
        //init
        //Why do I always need to erase text. I want a new stage for every test case.
        clickOn("#username").eraseText(10).write("validUser")
        clickOn("#password").eraseText(10).write("validPwd")
        clickOn("#orgId").eraseText(10).write("validOrg")

        // when:
        clickOn("#submitBut")

        // then:
        //verify success 
    }

    @Test fun should_click_on_button_and_fail_login() {
        //init
        clickOn("#username").eraseText(10).write("anyuser")
        clickOn("#password").eraseText(10).write("anypwd")
        clickOn("#orgId").eraseText(10).write("anyorg")

        // when:
        clickOn("#submitBut")

        // then:
        //verify fail 
    }
}

您可以添加 属性,您可以随时在 App() class.

中进行编辑
class MyMainApp: App() {
    override val primaryView: KClass<out View> = primaryViewMyApp

    companion object {
        var primaryViewMyApp: KClass<out View> = MyMainAppView::class
    }

    init {
        importStylesheet(<your stylesheet>)
    }

    override fun start(stage: Stage) {
        super.start(stage)
    }

    override fun stop() {
        super.stop()
    }
}

并且在测试中您可以使用您想要使用的任何视图。到目前为止我还没有尝试为 Fragment 实现一些东西...

    override fun init() {
        FxToolkit.registerStage { Stage() }
    }

    override fun start(stage: Stage) {
        MyMainApp.primaryViewGoApp = <your view>::class
        MyMainApp().start(stage)
    }

    override fun stop() {
        FxToolkit.cleanupStages()
        MyMainApp().stop()
    }