将TestFX与黄瓜一起使用

时间:2018-10-17 11:12:45

标签: java javafx cucumber testfx

我正在尝试将Cucumber与TestFX结合使用,但是无法从应用程序中获取任何节点。

我还有一类可以正常工作的TestFX和另一类也可以正常工作的Cucumber。但是我得到了

  

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

TestFXBase:

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对此进行了扩展。

StepDefs:

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);
    }
}

亚军:

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并且可以完美地工作。我该如何解决这个问题?

编辑:我的依赖项是

  <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>

1 个答案:

答案 0 :(得分:1)

这里的解决方案是将setupHeadlessMode和setUpClass方法的内容移至TestFXBase类初始化程序:

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
    }
}