grails - 我可以使用表单的不同值反复运行 spock 测试吗?

标签 grails spock geb

我是 Spock 和 Geb 的新手,正在我的 Grails 1.3.7 应用程序中使用它们。我有一个 LoginSpec 和 LoginPage 正在工作。现在我想做的是多次执行 LoginSpec,指定几种不同的用户名/密码组合。我不清楚我应该如何处理这个问题。

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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99fbf8fdd9edfceaedb7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>"
    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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6b1b9b9b296a2b3a5a2f8b5b9bb" rel="noreferrer noopener nofollow">[email protected]</a>"
    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') }

  }
}

最佳答案

您可以使用 Spock 对数据驱动测试的支持:

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
    "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="601405131420140513144e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>" | "password"
    "foo"           | "bar"        
}

关于grails - 我可以使用表单的不同值反复运行 spock 测试吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9570117/

相关文章:

hibernate - 仅将一些属性绑定(bind)到 grails 域对象上?

data-binding - 从 Controller 中的参数绑定(bind) Grails 日期

groovy - 使用Spock展开的所有可能的参数排列

grails - 确定Spock测试的执行顺序

unit-testing - 在 : block in Spock test 中传递一个值

grails 数据绑定(bind)集合

session - Grails 3 Controller 范围

gradle - 如何使用不同的参数多次运行 Spock 规范?

spock - 如何使用 athaydes 插件仅对 geb 中的失败步骤进行屏幕截图

selenium - Gradle/Spock/Geb org.openqa.selenium.firefox.NotConnectedException在Linux上可在Windows上使用