Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Created January 11, 2014 19:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hastebrot/8375513 to your computer and use it in GitHub Desktop.
Save hastebrot/8375513 to your computer and use it in GitHub Desktop.
// PLUGINS.
apply plugin: "groovy"
// PROJECT.
version = "0.0.1"
sourceCompatibility = 1.7
targetCompatibility = 1.7
// DEPENDENCIES.
repositories {
maven { name = "maven central"; url = "http://repo1.maven.org/maven2/" }
}
dependencies {
compile "org.codehaus.groovy:groovy-all:2.2.1"
testCompile "junit:junit:4.11"
testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
testCompile "org.loadui:testFx:3.0.0"
}
package github.gist.testfx
import javafx.scene.Parent
import org.loadui.testfx.GuiTest
import spock.lang.Specification
abstract class GuiSpecification extends Specification {
GuiTest fx
void setupStage(Closure rootNodeFactory) {
fx = new GuiTestMixin()
fx.setRootNodeFactory(rootNodeFactory)
fx.setupStage()
}
static class GuiTestMixin extends GuiTest {
Closure rootNodeFactory
protected Parent getRootNode() {
return rootNodeFactory.call() as Parent
}
}
}
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 org.junit.experimental.categories.Category
import org.loadui.testfx.categories.TestFX
import static org.loadui.testfx.Assertions.verifyThat
import static org.loadui.testfx.controls.Commons.hasText
@Category(TestFX.class)
class TestFxSampleSpec extends GuiSpecification {
def setup() {
setupStage {
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.click("#password").type("fidelio")
fx.click("#submit")
then:
verifyThat("#message", hasText("please enter!"))
}
def "enters gate with wrong password"() {
when:
fx.click("#password").type("shibboleet")
fx.click("#submit")
then:
verifyThat("#message", hasText("wrong password!"))
}
}
@siordache
Copy link

The above code adapted for TestFX 4.0.1-alpha: https://gist.github.com/siordache/10fb0a65749c9122edda

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