Geb Tutorial – Browser Automation Testing Using Geb Tool

Geb (pronounced “jeb”) is the answer to the challenges of browser automation. It is a very effective tool to perform automation testing over the web.

Geb originated out of the need to make browser automation (initially for web testing) less complicated, hassle-free and more efficient. It may be utilized for programming, extracting data from the web and automating the manual web tasks. Additionally, Geb is a cross-browser tool for automation testing.

Geb functions as a developer-driven tool for automating the collaboration between web browsers and web content. It runs the WebDriver in Groovy language.

The beauty of Geb testing tool is that it combines the best features of Groovy programming language, jQuery, WebDriver and Page Object Modelling to provide powerful, robust & dynamic content inspection, selection and web interaction.

Geb Browser automation solution

What makes Geb unique when compared to other automation testing tools available in the market is its syntax. It is similar to jQuery that is normally used for querying the HTML pages easily. Secondly, it has integrated support for the Page Object pattern.

Geb provides great help for functional web testing through integration with some broadly used and common testing platforms including Spock, Grails, JUnit, Cucumber-JVM, TestNG, etc. We will see how Geb can be integrated with Grails framework in the later part of this article.

Practical Uses

As already discussed in the introduction of this Geb tutorial, it can be used:

  • As a Testing tool on multiple browsers like chrome, Firefox, Internet explorer, etc. (The same automation script can be run on different browsers to perform web testing of your application.)
  • To automate User acceptance and functional test cases.
  • To automate test scenarios created for functional or web testing of any application.
  • To cover the end to end testing including the UI (User Interface) validation and DB (Database) validation.
  • As a Developer’s tool for automating the interaction between a web browser and web content.

Advantages

  • Geb is a free, open-sourced tool. It is licensed under the Apache License, Version 2.0.
  • Easy and simple to automate web testing.
  • Geb’s Page Objects and Groovy DSL make tests readable to the extent that they almost look like plain English.
  • Runs the tests fast and thus saves the time and cost of testing.
  • Compatible with different browsers like IE, Firefox, Chrome, and HTMLUnit.
  • It executes the tests in the real browser. It is as if testing in the real environment- the one that the user would see.
  • It makes the regression testing easy. You can run the Geb automated test cases to check if any existing functionality is breaking after a fix or change in the application.
  • While using Geb for automation testing, minimal test code changes are required if there are any UI changes in your application. So, it reduces the effort & duplication of code.
  • It helps 360 degrees (or maximum) testing coverage within the single script.

Prerequisites

Before getting started, we need to download and install the software. At the central Maven repository, Geb is available as a single Geb-core jar. Click here to install it on your machine.

You will need the above Geb-core jar, a web driver implementation, and the selenium-support jar to get Geb working on your machine.

Please refer to the below installation and usage section of the book of Geb to install the tool and get it running => Geb installation and usage manual.

Getting Started

As already discussed, Geb can be integrated with different testing frameworks.

Depending on the framework you have chosen, you will need to install the related plugin.

For example: Grails (Grails is a very famous framework for web applications) to write automation test scripts and automate the test scenarios. If you wish to use Geb for your Grails functional testing, you can install the related plugin from here grails-geb plugin.  This plugin handles the baseUrl and reportsDir configuration items.

Learn with Example

Let me now show how to write a Geb script to automate a test scenario.

Take the below test scenario:

Test Scenario IDSoftwaretestinghelp-1Test Case IdSoftwaretestinghelp -1A
Test Case DescriptionVerify Softwaretestinghelp.com Page through search engineAutomation Script StatusIn Progress
Pre-requisite1 Browser
2. search engine
3. website - Softwaretestinghelp.com should exist
Pre-requisite Script NA

The execution steps are:

Launch google search engine
Verify if the search engine has been loaded successfully
Enter softwaretestinghelp.com in the search box
Wait for results to load
Verify if the first link in results is directing to softwaretestinghelp.com
If yes, open the link.
Wait until the website opens up.
Exit

Here is the Geb automation tool script for the above scenario:

------------

import geb.Browser
Browser.drive {
go "http://google.com/"

//verify if we are on the correct page
assert title=="Google"

//enter softwaretestinghelp.com into the search field
$("input",name:"q").value("softwaretestinghelp.com")

//wait for the change to results page to happen
//(google updates the page dynamically without a new request)
waitFor{ title.endsWith("Google Serach")}

//is the first link to softwaretestinghelp.com?
def firstLink = $("li.g,0).find("a.l")
assert firstLink.text()= ="Software Testing Help - A Must Visit Software Testing Portal"

//click the link
firstLink.click()

//wait for Google's javascript
waitFor { title = ="Software Testing Help - A Must Visit Software Testing Portal" }
}

You can now try writing a simple GEB script on your own referencing the above example.

Database validation testing through Geb script:

Any web automation testing is divided into three parts:

  • UI Validation – Validating the data reflected on the user interface (front end) before & after the automation test scenario run.
  • DB Validation – Validating the data reflected in the database (backend) before & after the automation test scenario run.
  • Actual Test flow/ Script flow.

The Geb script written to automate a test scenario can contain the code for all of the above three sections.

The Geb script in the above example section was for automating the test flow and UI validation. Similarly, you can write a test script for database validation.

For any DB validation test, you can always use the below template as an outline for your code:

def validateDB(/*define all variables here*/) {
def errorMessages = ""
try {
Configuration conf = (new ConfigurationLoader()).getConf()
def sql = Sql.newInstance(conf.readValue("dbPath", ""), conf.readValue("dbUserName", ""), conf.readValue("dbPassword", ""), conf.readValue("dbDriverName", ""))

/* Populate any required variables */
}

/* Give print commands here to print required values */
def qry = /* select statement to pull all required values from database */
println "SQL=$qry"
sql.eachRow(qry) { row ->

/*  ‘if’ block to perform validation and returning error in case of any variations */
}
catch(Exception e) {
println "EEEE=$e"
}
return errorMessages
}

Few useful Methods in Geb

  • When your test case scenario involves multiple tabs and windows: Whenever you come across an application that opens up new windows or tabs, For example when clicking on a link with a target attribute set, you can make use of withWindow() and withNewWindow() methods to execute code in the context of other windows.
  • The drive method: Browser class contains a static method – drive(). This method gives an extra convenience to Geb scripting. All top level method calls and property accesses are implied to be against the browser.
  • Making Requests: Browser instances uphold a baseUrl property that is employed to resolve all relative URLs. It is usually most preferable to define your base URLs with trailing slashes and not to use leading slashes on relative URLs.
  • Changing the Page: With the help of useful page() methods, it is feasible to change the page instance without making a new request.
  • Quitting the Browser: The browser object provides quit() and close() methods (that simply handover the task to the base driver).

Drawbacks of this tool

  • Geb executes WebDriver in the Groovy language. The whole idea behind this is to make the use of WebDriver easier and simpler. So, when you using Webdriver through Geb, only Groovy programming language will be supported. But, if you directly use WebDriver, it supports many languages like Ruby, C#, Python, Java.
  • I would not suggest the use of Geb for small projects – It works awesome for enormous tasks but takes a hit on small activities. If your web application does not contain multiple pages and forms through which the information needs to flow, you may discover Geb really costs you additional time than it spares.
  • It is very particular about what environment your website application utilizes. Geb is required to be well integrated into a specific environment to make it function fine.

More resources:

Conclusion

Geb is very useful in automating test case scenarios. It is useful to automate web, functional and user acceptance testing. It supports multiple browsers and can be integrated with different frameworks. It combines the power of WebDriver, elegance of jQuery Selection, the robustness of Page Object Modelling and expressiveness of Groovy.

Geb scripts are both developer and user-friendly providing better test coverage and accelerated testing while making it more efficient at the same time.

About the author: This is a guest post by Priya K. She is having 4+ years of experience in IT Services with expertise in Testing and support for various applications.

Feel free to post your Geb automation testing queries in comments.




2 comments ↓

#1 Kiram

Thank you for the getting started tutorial.

#2 Vuong Le Dai

Someones can compare this framework with Selenium ?

Leave a Comment