Today we are going to learn how to use Geb in Cucumber functional automated testing? If you are new to Cucumber than please go through my blog.
Cucumber Basic concept

Fist-of-all if you want use Geb in your application than add these lines in BuildConfig.groovy

 def gebVersion = "0.7.0"
 dependencies {
 compile "org.spockframework:spock-grails-support:0.7-groovy-2.0"
 compile "org.gebish:geb-spock:0.9.2"
 compile "org.codehaus.geb:geb-spock:$gebVersion"
}

 plugins {
 compile(":spock:0.7") {
            exclude "spock-grails-support"
        }
 compile ":geb:0.9.2"
}

and create a GebConfig.groovy in test/functional folder and create driver instance in that like

import org.openqa.selenium.firefox.FirefoxDriver

driver = {
    def driverInstance = new FirefoxDriver()
    driverInstance.manage().window().maximize()
}

Now create a Groovy script like env.groovy in support folder like test/cucumber/support and add these lines of code to open browser and initialize URL.

import geb.binding.BindingUpdater
import geb.Browser
import static cucumber.api.groovy.Hooks.*

Before (){
    bindingUpdater = new BindingUpdater (binding, new Browser ())
    bindingUpdater.initialize ()
}

After () {
    bindingUpdater.remove ()
}

Create a feature file as TestingPage.feature

Feature: Logging in to Application

  Scenario: Successful login as a user
    Given I go to the start page
    When I enter username "user1" and password "user123"
    Then I am logged in

Now create a folder page in test/functional and create TestingPage.groovy and add these lines of code.

class TestingPage extends Page{
    static URL = "auth/login"

    static content = {
        userName{$('#j_username')}
        pass{$('#j_password')}
        login{$('#loginButton')}
    }
    def login(String username, String password){
        userName = username
        pass = password
        login.click()
    }
}

We define static URL because it tells bowser to open different URL and inside static content we define all the fields which will be interacted by browser.We have defined a login method which will be called for login by TestingPage_step

Now define TestingPage_step in cucumber folder and add these lines of code.

import static cucumber.api.groovy.EN.Given
import static cucumber.api.groovy.EN.Then
import static cucumber.api.groovy.EN.When

Given(~'^I go to the start page$') { ->
    to TestingPage
    at TestingPage
}

When(~'^I enter username "([^"]*)" and password "([^"]*)"$') { String username, String password ->
    page.login(username,password)

}
Then(~'^I am logged in$') { ->
   assert true
} 

There is no need to create an instance of browser and use findby method of selenium. While execution
application will go to TestingPage and use all field to login.