Created
December 3, 2015 01:52
-
-
Save siordache/10fb0a65749c9122edda to your computer and use it in GitHub Desktop.
TestFX 4.0.1-alpha with Spock - adapted from https://gist.github.com/hastebrot/8375513
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'groovy' | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
compile 'org.codehaus.groovy:groovy-all:2.4.5' | |
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4' | |
testCompile 'org.testfx:testfx-core:4.0.1-alpha' | |
testCompile 'org.testfx:testfx-junit:4.0.1-alpha' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package github.gist.testfx | |
import javafx.scene.Parent | |
import javafx.scene.Scene | |
import javafx.stage.Stage | |
import org.testfx.framework.junit.ApplicationTest | |
import spock.lang.Specification | |
abstract class GuiSpecification extends Specification { | |
ApplicationTest fx | |
void setupStage(Closure<Parent> rootNodeFactory) { | |
fx = new GuiTestMixin(rootNodeFactory) | |
fx.internalBefore() | |
} | |
static class GuiTestMixin extends ApplicationTest { | |
final Closure<Parent> rootNodeFactory | |
def GuiTestMixin(Closure<Parent> rootNodeFactory) { | |
this.rootNodeFactory = rootNodeFactory | |
} | |
protected Parent getRootNode(stage) { | |
return rootNodeFactory.call(stage) as Parent | |
} | |
@Override | |
public void start(Stage stage) { | |
Scene scene = new Scene(getRootNode(stage)) | |
stage.scene = scene | |
stage.show() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package github.gist.testfx | |
import javafx.event.ActionEvent | |
import javafx.event.EventHandler | |
import javafx.scene.control.Button | |
import javafx.scene.control.Label | |
import javafx.scene.control.TextField | |
import javafx.scene.layout.HBox | |
import static org.testfx.api.FxAssert.verifyThat | |
import static org.testfx.matcher.base.NodeMatchers.hasText | |
class TestFxSampleSpec extends GuiSpecification { | |
def setup() { | |
setupStage { stage -> | |
def passwordField = new TextField(id: "password") | |
def submitButton = new Button(id: "submit", text: "submit") | |
def messageLabel = new Label(id: "message") | |
submitButton.onAction = { ActionEvent event -> | |
if (passwordField.text == "fidelio") { | |
messageLabel.text = "please enter!" | |
} | |
else { | |
messageLabel.text = "wrong password!" | |
} | |
} as EventHandler | |
return new HBox(passwordField, submitButton, messageLabel) | |
} | |
} | |
def "enters gate with right password"() { | |
when: | |
fx.clickOn("#password").write("fidelio") | |
fx.clickOn("#submit") | |
then: | |
verifyThat("#message", hasText("please enter!")) | |
} | |
def "enters gate with wrong password"() { | |
when: | |
fx.clickOn("#password").write("shibboleet") | |
fx.clickOn("#submit") | |
then: | |
verifyThat("#message", hasText("wrong password!")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tested this out with the
4.0.4-alpha
release, and it still works. However, one needs to be careful about usingfx.press(KeyCode)
.On Linux Mint 17.3, I found that after running my tests, where the CTRL key was pressed, anything I did on my computer assumed that the control key was still down despite it not being the case.
So, if one uses
fx.press(KeyCode)
, follow it up with the correspondingfx.release(keyCode)
in the test'scleanup
blockEdit: Or one could just use
fx.push(KeyCode)