The Geb tool, pronounced as “jeb”, is a powerful instrument for resolving the complexities of browser automation, making it highly efficient for executing automation tests on the internet.
Originally devised to simplify and streamline automation processes across various web browsers, Geb is excellent for implementing programming tasks, extracting data from web sources, and automating web tasks that are typically manually carried out. Moreover, Geb delivers a cross-browser solution for automation testing.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
In the role of a tool driven by developers, Geb automates interactions between web browsers and web content, running WebDriver in Groovy language.
One of the attractive aspects of the Geb testing instrument is that it blends the finest attributes of the Groovy programming language, jQuery, WebDriver, and Page Object Modeling. This combination delivers powerful, robust, and flexible content inspection, selection, and web interaction.
Geb distinguishes itself from other automation testing tools in the market with its unique syntax. Its syntax is akin to jQuery, a tool frequently used for querying HTML pages in an easy manner. Furthermore, Geb incorporates support for the Page Object pattern.
Geb offers substantial support for functional web testing through integration with commonly used testing platforms such as Spock, Grails, JUnit, Cucumber-JVM, and TestNG. In the second part of this article, we will explore how Geb integrates with the Grails framework.
Lessons You’ll Learn:
Practical Uses
Earlier in this Geb guide’s introduction, we highlighted its uses:
- Testing across multiple browsers such as Chrome, Firefox, Internet Explorer, etc. (A single automation script can be employed on diverse browsers to test your internet application)
- Automating the execution of user acceptance and functional test cases
- Automating test scenarios designed specifically for functional or web testing of any application
- Incorporating end-to-end testing, including UI (User Interface) validation and DB (Database) validation
- Serving as a developer’s tool to automate interactions between a web browser and web content
Advantages
- Geb is a free, open-source tool licensed under the Apache License, Version 2.0
- Provides effortless and efficient automation testing for the web
- The combination of Geb’s Page Objects and Groovy DSL result in highly-readable tests, akin to plain English
- Rapid test execution, thus saving time and reducing testing expenses
- Compatible with diverse browsers, including IE, Firefox, Chrome, and HTMLUnit
- Carries out tests in an authentic browser environment, replicating the user’s viewpoint
- Facilitates regression testing by executing automated test cases to check if preexisting functionality breaks after an application’s modification or fix
- Requires minimal test code modifications when application’s UI changes, thus reducing effort and code duplication
- Allows for extensive testing coverage through a single script
Prerequisites
Prior to commencing, it’s necessary to download and install essential software. MarktPlatz has made Geb available in the form of a single Geb-core jar file in the central Maven repository. To install Geb on your device, please click here.
To launch Geb on your device, you will require the Geb-core jar file, a web driver implementation, and the selenium-support jar file.
Be sure to refer to the Geb user manual’s installation and usage section for thorough instructions on how to install and utilize the tool. Visit the Geb installation and usage manual.
Getting Started
As stated previously, Geb can be integrated with a variety of testing frameworks.
Based on the framework you select, you will need to install the relevant plugin.
Example: Grails (A well-liked framework for web applications which can be found here) can be utilized to write automation test scripts and automate test scenarios. If you wish to use Geb for your Grails functional testing, you can install the relevant plugin from this grails-geb plugin link. This plugin takes care of baseUrl and reportsDir configuration items.
Learn with Example
Let’s illustrate how to compose a Geb script to automate a test scenario.
Consider this test scenario as an example:
Test Scenario ID | Softwaretestinghelp-1 | Test Case Id | Softwaretestinghelp -1A |
Test Case Description | Validate Softwaretestinghelp.com Page through search engine | Automation Script Status | Underway |
Pre-requisite | 1 Browser 2. Search engine 3. Existence of the website – Softwaretestinghelp.com |
Pre-requisite Script | Not Applicable |
The steps to execute include:
Initialize Google search engine |
Confirm successful loading of search engine |
Key in ‘softwaretestinghelp.com’ in search box |
Await loading of results |
Confirm if the first link in results leads to ‘softwaretestinghelp.com’ |
If true, open the link. |
Wait for website to load. |
Exit |
The Geb automation tool script for the above scenario would be as follows:
import geb.Browser Browser.drive { go "http://google.com/" //confirmation of being on the correct page assert title=="Google" //insert 'softwaretestinghelp.com' into search field $("input",name:"q").value("softwaretestinghelp.com") //wait for page results update //(Google dynamically updates the page without issuing a new request) waitFor{ title.endsWith("Google Search")} //is the first link pointing to 'softwaretestinghelp.com'? def firstLink = $("li.g",0).find("a.l") assert firstLink.text() == "Software Testing Help - A Must Visit Software Testing Portal" //click on the link firstLink.click() //wait for Google's javascript to run waitFor { title == "Software Testing Help - A Must Visit Software Testing Portal" } }
You can now attempt to write a simple GEB script independently, using the above example as a guide.
Database validation testing through Geb script:
Web automation testing can be split into three sections:
- UI Validation – Evaluating the data displayed on the user interface (front end) before and after the execution of the automation test scenario.
- DB Validation – Evaluating the data reflected in the database (backend) before and after the execution of the automation test scenario.
- Actual Test Flow or Script Flow.
The Geb script that automates a test scenario can include code for all three sections mentioned above.
The Geb script in the proceeding example dealt with automating the test flow and UI validation. In a similar manner, you could write a test script for database validation.
For any DB validation test, below is a template that can serve as a framework 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 */ } /* Insert print commands here to print required values */ def qry = /* select statement to retrieve all required values from the database */ println "SQL=$qry" sql.eachRow(qry) { row -> /* 'if' block to perform validation and return an error in case of any variances */ } catch(Exception e) { println "EEEE=$e" } return errorMessages }
Few useful Methods in Geb
- When your test case scenario involves multiple tabs and windows: When dealing with an application that opens in new windows or tabs, for instance, clicking on a link with a target attribute set, you can use the withWindow() and withNewWindow() methods to execute code in other windows.
- The drive method: The Browser class houses a static method called drive(). This method adds extra convenience to scripting with Geb. All top-level method calls and property accesses are implied to be against the browser.
- Making Requests: Browser instances have a baseUrl property used to resolve all relative URLs. It is recommended to define base URLs with trailing slashes and not to use leading slashes on relative URLs.
- Changing the Page: With the help of useful page() methods, you can change the page instance without making a new request.
- Quitting the Browser: The browser object offers quit() and close() methods (which delegate the task to the base driver).
Drawbacks of this tool
- The Geb tool operates WebDriver in Groovy language. The primary objective behind this is to simplify the use of WebDriver. Hence, when using WebDriver through Geb, support is restricted to the Groovy programming language only. Nevertheless, WebDriver directly supports multiple languages such as Ruby, C#, Python, and Java.
- I would hesitate to recommend Geb for small-scale projects. Although it functions well for large tasks, it involves additional overhead for smaller operations. If your web application lacks multiple pages and forms that require information flow, you may find Geb more time-consuming than time-saving.
- Geb operation is contingent upon the environment in which your web application operates. To properly function, Geb requires robust integration into a specific environment.
More resources:
- Check out the Book of Geb here for exhaustive documentation and examples.
- Here is a sample project that exemplifies the integration of Geb with Grails.
Conclusion
Geb is a handy tool for automating test case scenarios including user acceptance testing, web and functional testing. It supports multiple browsers and integrates well with various frameworks. It harnesses the power of WebDriver, the sophistication of jQuery selection, the reliability of Page Object modelling and the expressivity of Groovy.
Geb scripts offer a user-friendly experience for developers, enhancing test coverage, streamlining testing, and improving efficiency.
About the author: This guest post is authored by Priya K. She possesses over 4 years of experience in IT Services, specializing in Testing and support for various applications.
Please feel encouraged to post your queries regarding Geb automation testing in the comments section.