Tech It Easy: December 2015

Tuesday, 22 December 2015

Geb - "get into the groove(y)" - the way I used it


Dear friends,

I am back with a new post cheers

Image Courtesy: http://www.123rf.com/photo_5468266_boy-with-computer.html

As a continuation to my earlier post on Geb(pronounced as 'Jeb')
http://techiteasypolicy.blogspot.in/2015/04/geb-very-groovy-browser-automation.html , I am here with a new tool/candy I created and using in my day to day work.


I personally feel that this is a groovier (funny) or viral way of using Geb though it is meant for Web Automation Testing.

The Problem:
Each time when I want to open my application in some browser like chrome for testing or what ever, the following are the sequence of things I do (it is pretty similar with every other web based project or application)

If you want, use it at your own discretion

1. Open the browser
2. Put the URL in the URL bar (or clicking the bookmark)
3. As my app starts with a login page, I provide username, password (is a trivial one and not that sensitive),
4. Click on Submit button to login  and proceed with my intended work.

Rescue:
Here comes the Geb script to rescue us,

To run the following script you need groovy in the path.

Then run the following command in command prompt (assuming that in the current path there is a file named appTrigger.groovy and it has the following piece of code in it)

groovy appTrigger.groovy

@Grab('org.gebish:geb-core:0.10.0')
@Grab('org.seleniumhq.selenium:selenium-chrome-driver:2.43.1')
@Grab('org.seleniumhq.selenium:selenium-support:2.43.1')
import geb.Browser

Browser.drive {
    go 'http://localhost:8080/myApp/' 
    $ '#gwt-debug-login-username' value 'Superuser'
    $ '#gwt-debug-login-password' value 'Superuser'
    $ '#gwt-debug-login-submit-button' click()
}

That's it, Geb does the heavy lifting for you.

Actually, it is not just login submission, its about the Web browser and automating user interactions with it. Hence you can extrapolate it and use it in your own way.

Going one step further (Externalizing the URL and username and password into a config.properties file)

@Grab('org.gebish:geb-core:0.10.0')
@Grab('org.seleniumhq.selenium:selenium-chrome-driver:2.43.1')
@Grab('org.seleniumhq.selenium:selenium-support:2.43.1')
import geb.Browser

config = new Properties()
new File('config.properties').withInputStream { config.load it }
Browser.drive {
    go config.url
    $ config.username_selector value config.username 
    $ config.password_selector value config.password
    $ config.button_selector click()
}


//config.properties
url=http://localhost:8080/myApp/
username=Superuser
password=Superuser

username_selector=#gwt-debug-login-username
password_selector=#gwt-debug-login-password
button_selector=#gwt-debug-login-submit-button

Going still further with Gradle's application plugin we can create a jvm application Distribution with which user with plain Java installed in their machine can run this tool. Long live Gradle :)

The distribution Zip file created by by above Gradle plugin has every thing required to run the app without the Sophisticated build tools and/or IDEs.

Code for the above said Gradle based App is in my GitHub repository https://github.com/PraveenGandhi/geb-based-tool-to-open-app, feel free to hack it.

 Hope you enjoy this.

PS: I have used password here, please don't use your sensitive passwords like net-banking credentials  etc.