Hello, This blog post is for beginner’s who are new to testing. We use Geb(The browser automation tool) with grails to test our application in this blog.

First of all what is Geb?
Geb is a browser automation tool use web-content and web-browser for automation testing. This is a mixup power of webDriver, content selection with jQuery and use Page object modeling and groovy language to make it robust. For further information go to this link :- Geb Book

My moto in this blog is to help you start writing your functional test with Geb in grails:-

First of all we need some configuration setting to our grails application. Steps for configuration:-

  • Open your BuildConfig.groovy file and make these entry:-
     dependencies {
            test("org.seleniumhq.selenium:selenium-chrome-driver:2.41.0")
            test("org.seleniumhq.selenium:selenium-firefox-driver:2.41.0")       
            // You usually only need one of these, but this project uses both
            test "org.gebish:geb-spock:0.9.3"
            test "org.gebish:geb-junit4:0.9.3"
        }
    
     Plugin{
       test ":geb:0.9.3" //This geb version support grails version 2.3.5 and later.
      }
    
    
  • Now make a directory under test folder with name functional.Right click on this directory and mark this directory as Test Source Root.
  • Now create a sub directory under functional directory with name pages.
    Note :- All page classes will come under this directory.
  • Create a groovy config file under functional directory with name GebConfig.groovy and write the following code :-
  • import org.openqa.selenium.firefox.FirefoxDriver
    
    driver = { new FirefoxDriver() }
    
    environments {
        firefox {
            driver = { new FirefoxDriver() }
        }
    }
    
  • I am writing this test to test my company website.
    Create below given page classes in pages folder :-
  • Google.groovy

    package pages
    import geb.Page
    
    class Google extends Page{
    
        static url = "http://google.com/ncr"
        static at ={
          title == "Google"
        }
        static content = {
            search { $("input", value:"Google Search")}
            field { $("input", name: "q") }
        }
    }
    
  • GoogleResultPage.groovy

    package pages
    import geb.Page
    
    class GoogleResultPage extends Page{
        static at = { title.endsWith "Google Search" }
        static content = {
            results { $("li.g") }
            result { i -> results[i] }
            resultLink { i -> result(i).find("a.l") }
            firstResultLink { resultLink(0) }
        }
    }
    
  • JellyFishTechnologies.groovy

    package pages
    import geb.Page
    
    class JellyFishTechnologies extends Page{
        static at = { title.find(~/(JellyFish Technologies)/)}
    }
    
  • Now make a groovy file under functional folder.This is actually our test file which use above page classes for testing.
  • JellyFishSpec.groovy
    import geb.spock.GebReportingSpec
    import pages.*
    
    class JellyFishSpec extends GebReportingSpec{
    
     def "Check for jellyfish Technologies"(){
    
         when:
         to Google
    
         then:
         assert at(Google)
    
         when:
         field.value("jellyfish technologies")
    
         then:
         waitFor { at GoogleResultPage }
    
         when:
         assert firstResultLink.text() == "Contact Us"
    
         then:
         firstResultLink.click()
         waitFor { at JellyFishTechnologies }
     }
    }
    
  • Now run this command from console :- grails test-app -functional

Hope this helps 🙂 . If you have something to add here, please do share, Thanks.