Introduction to GEB : Best way to Automation Testing
Posted By : Shiv Kumar | 14-Sep-2015
Geb is used for the browser automation testing. It provides the power of Web provider, Jquery and Groovy script in a unit. It is very useful for acceptance, web and functional testing as it supports testing frameworks like JUnit and Spock. It supports all those browsers whose webdriver we include in our application.
Installation : In your BuildConfig do the following configuration :
1. In grails.project.dependency.resolution section :
def gebVersion = "0.10.0"
def seleniumVersion = "2.45.0"
2. In dependencies section :
test("org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion") // Include web driver for firefox
test "org.gebish:geb-spock:$gebVersion" // Include support for spock
test "org.gebish:geb-junit4:$gebVersion" // Include support for Junit
3. In plugin section :
test ":geb:$gebVersion" // GEB version to install.
Now clean and do refresh dependencies.Run the project.
Usage :
Step 1 : First we have to define page structure which we have to test using Geb with the help of Module and Page Obkect pattern of GEB. Create a file named "GoogleSearchModule.groovy" with the following content :
import geb.Module
import geb.Page
class GoogleSearchModule extends Module{
// a parameterised value set when the module is included
def buttonValue
// the content DSL
static content = {
// name the search input control fields, defining it with the jQuery like navigator
field { $("input", name: "q") }
// the search button declares that it takes us to the results page, and uses the
// parameterised buttonValue to define itself
button(to: GoogleResultsPage) {
$("input", value: buttonValue)
}
}
}
class GoogleHomePage extends Page {
// pages can define their location, either absolutely or relative to a base
static url = "http://google.com/ncr"
// allow verifying that the browser is at the expected page
static at = { title == "Google" }
static content = {
// include the previously defined module
search { module GoogleSearchModule, buttonValue: "Google Search" }
}
}
class GoogleResultsPage extends Page {
static at = { title.endsWith "Google" }
static content = {
// reuse our previously defined module
search { module GoogleSearchModule, buttonValue: "Search" }
// content definitions can compose and build from other definitions
results(wait: true) {$('div.srg li').find('a')}
dropdownResult {$('ul.sbsb_b li')}
firstResultLink(wait: true) { $('div.srg li a') }
}
}
class WikipediaPage extends Page {
static at = { title == "Wikipedia" }
static content = {
englishkWikiLink (wait : true){
$('div.lang1 a')
}
}
}
}
Step 2: Page definition is completed. Now we have to right the test case for the above defined page using Spock and Geb syntax. Create a file named "GoogleWikiSpec.groovy" with the content :
import geb.spock.GebReportingSpec
class GoogleWikiSpec extends GebReportingSpec {
def "first result for wikipedia search should be wikipedia"() {
given:
to GoogleHomePage // "to" syntax is used to direct the page to GoogleHomePage which is defined in page definition
when:
search.field.value("wikipedia") // search is define in the page "GoogleResultsPage" to search any content on google page.
then:
waitFor(20) { at GoogleResultsPage} // waitFor is used to delay or wait for the ae response.
and:
dropdownResult.size() == 4
firstResultLink.text() == 'Wikipedia'
}
}
Like extending the IntegrationSpec in spock testing , here we are extending GebReportingSpec to support GEB syntax like "to GoogleHomePage".
Testing spec is completed here. Now Save the things and do "test-app" to run the above spec.
Doing this automatically direct to google page then search for Wikipedia page and test the length of suggestion dropdown along with the first result title.
This is the demo version for GEB. Wait for the Geb exploration further.
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shiv Kumar
Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.