当前位置: 首页 > 知识库问答 >
问题:

使用where子句在GEB Cookie中进行Spock测试

司寇研
2023-03-14

我配置了一些geb测试,根据web应用程序中的登录尝试检查不同的消息。因为消息和输入字段将在第三次登录尝试时更改。

登录是基于向特定电话号码发送密码的两步登录,因此在第一页LoginPage中,用户介绍他们的Id和电话号码,然后将其重定向到第二页ValidationLoginPage,用户在其中介绍接收到的密码。

我想检查一下,在第二个页面中,用户只能引入三个错误的密码,在第四次尝试时,引入密码的输入将消失,并显示一条不同的消息,指示没有更多的尝试。

def "Test max loging attempts"(){
  given:
    if(loginAttempt == 1)
      to LoginPage
      loginModule.startLogin(cfg.user.id,cfg.user.phone)
    }
  when:
    at LoginValidationPage
    assert  $('div.box_id_header h3').text() == 'Verify your code'
    assert  $('#code').css('display').contains('block')
    loginModule.verifyPassword('WRONGPASSWORD')
  then:
    at LoginValidationPage
    println "Attempt ${loginAttempt}"
    if(loginAttempt == 4){
      // last attempt
      assert    $('#code').css('display') == 'none' 
      assert  $('#divCodeErrorMsg').text().contains('No more attempts')
    }else{
      assert    $('#code').css('display').contains('block')
      assert  $('#divCodeErrorMsg').text().contains('Wrong password. Try again.')
    }
  where:            
    loginAttempt << (1..4)
}

此外,还可以使用where:以更简洁的方式避免检查loginattem变量,以避免多次运行givide:部分,或者还有更好的方法不使用where:

共有1个答案

鲁泰宁
2023-03-14

问题是您可能误解并因此滥用了Spock的where:块。它被设计为参数化一个特征方法,该方法的每个运行包括一个独立的特征。如果您@unroll您的特性,即使对于where:参数的每一组,也会生成一个新方法。因为特性应该是相互独立的,理论上可以以任何给定的顺序运行,甚至并行运行,所以测试夹具需要重置才能运行它们。这就是cookie的情况,因为您滥用了where:特性来实现一个简单的循环。

您的代码中还有其他一些小问题,比如atcheck在when:块中没有断言atcheck。即使at生成false,如果不使用assert,也不会产生任何后果。您只能在then:expect:块中跳过assert,但不能在givid:when:中跳过,也不能在闭包或helper方法中跳过,我将在示例代码中使用它们。

这样怎么样?

package de.scrum_master.stackoverflow.foo

import geb.spock.GebReportingSpec

class LoginTest extends GebReportingSpec {
  def loginModule = new LoginModule()
  def cfg = new Config()

  def "Test max login attempts"() {
    given: "we are at the login page"
    browser.config.autoClearCookies = false
    to LoginPage

    when: "logging in 4x with wrong credentials"
    (1..4).each {
      loginWithWrongCredentials(it, it < 4)
    }

    then: "we get a 'no more attempts' error message"
    $('#codi').css('display') == 'none'
    $('#divCodiValidacioError').text().contains('No more attempts')
  }

  def loginWithWrongCredentials(int loginAttempt, boolean checkForWrongPasswordMessage) {
    println "Login attempt ${loginAttempt}"
    loginModule.startLogin(cfg.user.id, cfg.user.phone)
    assert at(LoginValidationPage)
    assert $('div.box_id_header h3').text() == 'Verify your code'
    assert $('#code').css('display').contains('block')
    loginModule.verifyPassword('WRONGPASSWORD')
    assert at(LoginValidationPage)
    if (checkForWrongPasswordMessage) {
      assert $('#codi').css('display').contains('block')
      assert $('#divCodiErrorMsg').text().contains('Wrong password. Try again.')
    }
  }
}
 类似资料:
  • 编辑:我使用的是spock版本0.7-Groovy-2.0

  • 如何将行号用作where子句的一部分? 我有,假设,25个数据,我只想得到前10行或得到从11到20的行。 如何使用行号?我无法执行下面的查询 更新:问题是,在我的where子句中,row_number在表UPDATE2:Thanks Guys中是未知的!不管怎样,我找到了这个。选择行号=行号的位置

  • 如果不存在则创建表(int(11)不为空AUTO_INCREMENT,char(50)不为空,主键())engine=innoDB DEFAULT charset=latin1 AUTO_INCREMENT=11; 表tblcontent 如果不存在(int(11)不为NULL AUTO_INCREMENT,char(100)不为NULL,text不为NULL,date不为NULL,char(50

  • 问题内容: 在WHERE子句中有使用SELECT语句描述的名称吗?这是好/不好的做法吗? 这会是更好的选择吗? 它远没有那么优雅,但是运行起来比以前的版本要快。我不喜欢它,因为它在GUI中没有非常清晰地显示(并且SQL初学者需要理解它)。我可以将其分为两个独立的查询,但是随后事情变得混乱了…… 注意:我不仅需要日期和分数(例如姓名) 问题答案: 称为相关子查询。它有它的用途。

  • 问题内容: 我有一个简单的选择查询- 我希望此结果按提供名称的顺序排序,也就是说,结果集中的第一行应该是名称= BBB的那一行,第二行是AAA,第三行是ZZZ。 在SQL Server中这可能吗?我想知道如果有一种简单且简短的方法(例如大约5到6行代码)来做到这一点。 问题答案: 您可以创建一个有序的拆分函数: 然后略微更改您的输入(一个逗号分隔的列表,而不是三个单独的字符串):