Search code examples
grailsspockgeb

Can I run a spock test over and over with different values for a form?


I'm new to Spock and Geb and am using them in my Grails 1.3.7 application. I have a LoginSpec and LoginPage working. Now what I want to do is execute the LoginSpec many times, specifying several different username / password combinations. I'm unclear on how I should approach this.

class LoginSpec extends geb.spock.GebReportingSpec {

  def "At the login page"() {
    when: "we visit the login page"
    to LoginPage
    then:
    at LoginPage
  }

  def "failed login"() {
    when: "a user goes to the login page and enters invalid data"
    to LoginPage
    loginForm.j_username = "[email protected]"
    loginForm.j_password = "password"
    loginButton.click()
    then:
    at LoginPage
  }

  def "successful login"() {
    when: "a user goes to the login page and enters valid data"
    to LoginPage
    loginForm.j_username = "[email protected]"
    loginForm.j_password = "password"
    loginButton.click()
    then:
    at DashboardPage
  }
}

class LoginPage extends Page {

  static url = "login/auth"

  static at = { heading == "sign in" }

  static content = {
    heading { $(".page-header>h2").text() }
    loginButton(to: [DashboardPage, LoginPage]) { $('#sign_in').find("button.primary") }
    loginForm { $('#loginForm') }

  }
}

Solution

  • You can use Spock's support for data driven testing:

    def "successful login"() {
        when: "a user goes to the login page and enters valid data"
        to LoginPage
        loginForm.j_username = username
        loginForm.j_password = password
        loginButton.click()
    
        then:
        at DashboardPage
    
        where:
        username        | password
        "[email protected]" | "password"
        "foo"           | "bar"        
    }