Вопрос

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 = "bad@test.com"
    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 = "good@test.com"
    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') }

  }
}
Это было полезно?

Решение

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
    "test@test.com" | "password"
    "foo"           | "bar"        
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top