Skip to content

Instantly share code, notes, and snippets.

@siordache
Created December 3, 2015 01:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siordache/10fb0a65749c9122edda to your computer and use it in GitHub Desktop.
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
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'
}
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()
}
}
}
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!"))
}
}
@JordanMartinez
Copy link

JordanMartinez commented Jun 22, 2016

I tested this out with the 4.0.4-alpha release, and it still works. However, one needs to be careful about using fx.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 corresponding fx.release(keyCode) in the test's cleanup block

Edit: Or one could just use fx.push(KeyCode)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment