Search code examples
spockgeb

Geb/Spock login each def


Is there a easy way in Geb/Spock to ensure a login happens before all functional tests?

For instance my login test looks like

def "login"() {
        when:
            to Login
        and:
            login(username,password)
        then:
            at Dashboard
        where:
            username   | password
            "X"        | "X"
    }

This is quite alot of code to put into each other test.


Solution

  • Create an abstract base spec which all of your specs that require logging in can extend:

    abstract class LoginBaseSpec extends GebReportingSpec{
    
      def setupSpec(){
        when:
          to Login
    
        and:
          login(username, password)
    
        then:
          at Dashboard  
      }
    

    This setupSpec() method in the super spec will be executed before anything in the extending specs.