// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package demo.geb

import geb.Page
import geb.spock.GebSpec

import org.springframework.boot.test.context.SpringBootTest

import demo.AppConfiguration

class IndexPage extends Page {
    static url = 'http://localhost:8080/'
    static at = { title.startsWith('Index') }
}

class LoginPage extends Page {
    static url = 'http://localhost:8080/login'
    static at = { title.startsWith('Login') }
    static content = {
        email { $('#email') }
        password { $('#password') }
        submit { $("#login input[type=submit]") }
    }
}

@SpringBootTest(classes = AppConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class LoginSpec extends GebSpec {
    def 'go to login'() {
        when:
        to LoginPage

        then:
        at LoginPage
    }

    def 'success login'() {
        setup:
        to LoginPage

        when:
        email().value "users@tapestry.apache.org"
        password().value "Tapestry5"
        submit().click()

        then:
        at IndexPage
    }

    def 'fail login'() {
        setup:
        to LoginPage

        when:
        email().value "users@tapestry.apache.org"
        password().value "xxx"
        submit().click()

        then:
        at LoginPage
    }
}
