首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TestFX与黄瓜配合使用

TestFX与黄瓜配合使用
EN

Stack Overflow用户
提问于 2018-10-17 11:12:45
回答 1查看 1K关注 0票数 0

我试图在TestFX中使用Cucumber,但无法从应用程序中获取任何节点。

我有另一类工作良好的TestFX,还有一类黄瓜,它也很好用。但我得到了

org.loadui.testfx.exceptions.NoNodesFoundException:没有与“TextInputControl有文本”匹配的节点“您可以找到这个标签”。

TestFXBase :

代码语言:javascript
复制
import javafx.scene.Node;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.testfx.api.FxToolkit;
import org.testfx.framework.junit.ApplicationTest;

import java.util.concurrent.TimeoutException;


public class TestFXBase extends ApplicationTest {

    private static boolean isHeadless = false;

    @BeforeClass
    public static void setupHeadlessMode() {
       if(isHeadless){
            System.setProperty("testfx.robot", "glass");
            System.setProperty("testfx.headless", "true");
            System.setProperty("prism.order", "sw");
            System.setProperty("prism.text", "t2k");
            System.setProperty("java.awt.headless", "true");
        }


    }

    @Before
    public void setUpClass() throws Exception {
        ApplicationTest.launch(Main.class);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.show();
    }

    @After
    public void afterEachTest() throws TimeoutException {

        FxToolkit.hideStage();
        release(new KeyCode[]{});
        release(new MouseButton[]{});
    }

    /* Helper method to retrieve Java FX GUI Components */
    public <T extends Node> T find (final  String query){
        return (T) lookup(query).queryAll().iterator().next();
    }

}

这是我的testfx基类,我的黄瓜运行器和逐步防御扩展了这个类。

StepDefs:

代码语言:javascript
复制
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import org.junit.Test;


public class MyStepdefs extends TestFXBase  {
    @Test
    @Given("^That \"([^\"]*)\" Exists$")
    public void thatExists(String arg0) throws Throwable {
        rightClickOn("#rect");
    }
    @Test
    @Then("^Which is \"([^\"]*)\"$")
    public void whichIs(String arg0) throws Throwable {
        System.out.println(arg0);
    }
}

Runner:

代码语言:javascript
复制
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;


@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "pretty" })
public class MyRunner extends TestFXBase{}

特性:

特征:标签文本存在吗? 场景:您能找到这个标签文本吗? 考虑到“你能找到这个标签” 那就是“伟大”

因此,参数被传递了,但是TestFX没有在我的黄瓜运行程序中启动应用程序,只是尝试查找节点。有一个类可以扩展TestFXBase并完美地工作。我怎样才能解决这个问题?

编辑:我的依赖项是

代码语言:javascript
复制
  <dependency>
            <groupId>org.loadui</groupId>
            <artifactId>testFx</artifactId>
            <version>3.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.jfxtras</groupId>
            <artifactId>openjfx-monocle</artifactId>
            <version>1.8.0_20</version>
        </dependency>

        <dependency>
            <groupId>org.testfx</groupId>
            <artifactId>testfx-core</artifactId>
            <version>4.0.6-alpha</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>org.testfx</groupId>
            <artifactId>testfx-junit</artifactId>
            <version>4.0.6-alpha</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-21 13:46:22

这里的解决方案是将setupHeadlessMode和setUpClass方法的内容移到TestFXBase类初始化器中:

代码语言:javascript
复制
static {
    if (isHeadless) {
        System.setProperty("testfx.robot", "glass");
        System.setProperty("testfx.headless", "true");
        System.setProperty("prism.order", "sw");
        System.setProperty("prism.text", "t2k");
        System.setProperty("java.awt.headless", "true");
    }

    try {
        ApplicationTest.launch(Main.class);
    } catch (Exception e) {
        // oh no
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52853515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档