Getting started with Grails functional tests using Geb + Spock
Introduction
There are a number of different functional test frameworks that can be used with Grails. Each of which can be incorporated into the overall test infrastructure of your Grails app by including the relevant plugin:Grails doesn't hold any opinion on which one to choose. This is fair, and gives developers choice, but it can also be a headache; one has to make the choice of which framework, and also do the necessary setup and configuration. The functional test framework of the day is currently Geb, which builds on top of another functional framework in WebDriver. Geb can be used with the test runners JUnit, TestNG, or Spock. Spock, like Geb is the tool dejour and they are commonly used together. The best thing about Spock tests is that they are easy to read.
New Dependencies in your BuildConfig
To run your functional tests you need to include a number of libraries and plugins in your BuildConfig.groovy. Here is what you typically need to merge in:grails.project.dependency.resolution = { def gebVersion = "0.7.0" def seleniumVersion = "2.25.0" dependencies { // Geb Spock integration test "org.codehaus.geb:geb-spock:$gebVersion" // Various webdrivers to drive your tests in different browsers test "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion" test "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion" test "org.seleniumhq.selenium:selenium-ie-driver:$seleniumVersion" test("org.seleniumhq.selenium:selenium-htmlunit-driver:$seleniumVersion") { exclude 'xml-apis' } } plugins { test ":geb:0.6.3" test ":spock:0.6" // Not required, but very useful in speeding up working with functional tests compile ":functional-test-development:0.9.3" compile ":funky-test-load:0.3.9" } }
Add Geb Config
Create a new file test/functional/GebConfig.groovy Add the following content:/* This is the Geb configuration file. See: http://www.gebish.org/manual/current/configuration.html */ import org.openqa.selenium.htmlunit.HtmlUnitDriver import org.openqa.selenium.firefox.FirefoxDriver import org.openqa.selenium.chrome.ChromeDriver // Use htmlunit as the default // See: http://code.google.com/p/selenium/wiki/HtmlUnitDriver driver = { def driver = new HtmlUnitDriver() driver.javascriptEnabled = true driver } environments { // run as “grails -Dgeb.env=chrome test-app” // See: http://code.google.com/p/selenium/wiki/ChromeDriver chrome { driver = { new ChromeDriver() } } // run as “grails -Dgeb.env=firefox test-app” // See: http://code.google.com/p/selenium/wiki/FirefoxDriver firefox { driver = { new FirefoxDriver() } } }
Install Chrome Driver
The above listed chrome-driver library in BuildConfig is not enough to run your tests in Chrome. You also need to install on your development system an OS specific driver that acts as a bridge between what is included by the BuildConfig and the browser itself. You can find this driver here: http://code.google.com/p/selenium/wiki/ChromeDriver- Download, unzip the driver suitable for your development machine Place it somewhere nice I put mine in: ~/Dev/apps/chromedriver/
- Add that folder location to your PATH export PATH=$PATH:~/Dev/apps/chromedriver
Define a Spec
The test specifications and pages are obviously going to be different for each app. I'm including a sample here, but to find out how to write specs and pages for your application consult the very comprehensive Book of Geb.Sample Spec (test/functional/SignUpSpec.groovy):
import geb.spock.GebReportingSpec import geb.spock.GebSpec import spock.lang.* import pages.* @Stepwise class SignUpSpec extends GebSpec { def "signup new customer"() { when: to SignUpFormPage, campaign: 1 firstName = "Eamonn" lastName = "O'Connell" username = "eamonn2@stratus5.com" password = "x2l43j23lk4mallls" passwordConfirmation = "x2l43j23lk4mallls" orgName = "eamotest" addressLine1 = "GEC" city = "Dublin" signUpButton.click() then: at SignUpCompletePage } }
Sample Page (test/functional/pages/SignUpFormPage.groovy)
package pages class SignUpFormPage extends geb.Page { static at = { heading.text() == "Sign Up" } static url = "signUp" static content = { firstName { $("#firstName") } lastName { $("#lastName") } username { $("#username") } password { $("input[name=password]") } passwordConfirmation { $("input[name=password2]") } signUpButton { $("#submit") } orgName { $("#orgName") } addressLine1 { $("input[name='address.addressLine1']") } addressLine2 { $("input[name='address.addressLine2']") } city { $("input[name='address.city']") } county { $("input[name='address.county']") } zip { $("input[name='address.zip']") } tel { $("#tel") } } }As you can see from the above defining tests using Specs and Pages is a thing of beauty.
Executing your tests
The typical way of running your grails tests is to run the test-app command like as follows:grails test-app functional:
This works well enough, but you can find you end up waiting a lot as each test run requires the starting up of an instance of your grails app. An improvement to this process is to use the functional-test-development plugin included in the BuildConfig above. If you have included this plugin, you can run the following command:
grails develop-functional-tests
This will start up an instance of your application, and then display a console prompt with which you can run functional tests with over and over again. You can also restart the running app from the console, and there is a very handy feature where hitting the return key will run the previous test command.
grails funky-test-load
The funky test load plugin is my functional test runner of choice. It is actually inspired in part by the functional-test-development plugin. With it you can run your tests just as you would with the functional-test-development plugin, but it also enables you to also run the same test concurrently. Imagine emulating ten users signing up to your test server with a few keyboard strokes. I find it a real help in being able to reproduce troublesome concurrent issues like optimistic locking errors.
Tip: To run your test with Chrome add -Dgeb.env=chrome to the develop-functional-test console.
You can run into a few hiccups to be aware of:
- The Grails app that runs is using the test environment, so be sure your Config.groovy is ready for that.
- The plugin specifies memory settings for the app to run. If you have already specified memory settings in the JAVA_OPTS or GRAILS_OPTS environment variables then it will pull the memory settings from that. Otherwise it will add default ones. Now in my case, I have memory settings defined in JAVA_OPTS, and the plugin's code for parsing was not grabbing all of the settings. I had to hack the plugin a bit to get it working. It could be just how I defined the settings or it could be a bug that might well be fixed by the time you read this.
Credits
- http://code.google.com/p/spock/wiki/SpockBasics
- https://github.com/geb/geb-example-grails
- http://www.gebish.org/manual/current/index.html
- http://code.google.com/p/selenium/wiki/ChromeDriver
- https://github.com/alkemist/grails-functional-test-development
- http://grails.org/doc/latest/guide/testing.html#functionalTesting
- https://github.com/geb/geb/tree/master/module/geb-grails
Great post & clear explanation on Functional testing
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.
DeleteJava Training in Chennai Core Java Training in Chennai Core Java Training in Chennai
Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai
Thanks for providing details about Software Testing
ReplyDeleteThanks for your informative article on UFT automation testing tool. Your post helped me to understand the features and functionality of QTP automation testing tool. QTP training
ReplyDeleteThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
ReplyDeleteInformatica Training in chennai | QTP Training in Chennai
new horizon security services richmond va I am really impressed with your efforts and really pleased to visit this post.
ReplyDeleteTechnology place a vital part in humans ecosystem. So in order to survive one must be up to date. Thanks for sharing this information in here. Keep blogging article like this. I have bookmarked this page for future reference.
ReplyDeleteHadoop Training Chennai | Big Data Training| JAVA training in Chennai
Bed Bug Treatment Leesburg VA It is really a great and useful piece of information. I am glad that you shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.
ReplyDeleteReally awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
ReplyDeleteOracle Training In Chennai | Hadoop Training In Chennai
Best Java Training Institute In ChennaiThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
ReplyDeleteresidential dumpster va Galaxy Transfer is based in Northern Virginia and is a division of EnviroSolutions, Inc. GT was purchased by EnviroSolutions, Inc. in June 2013.
ReplyDeleteExcellent Post!! I am really very happy to found such helpful and fascinating post that is written in well manner. Keep posting.
ReplyDeleteQTP Training in Chennai | Software Testing Training in Chennai | Selenium Training in Chennai
too good piece of information, I had come to know about your site from my friend sajid, bangalore,i have read atleast 7 posts of yours by now, and let me tell you, your web-page gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new post, once again hats off to you! Thanks a lot once again, Regards, informatica mdm training in hyderabad,informatica training in hyderabad
ReplyDeleteGreat post!Thanks for sharing useful and very understanding information.Industrial safety course in chennai|Industrial safety course in chennai|fire and safety courses in chennai|Iosh course in chennai|nebosh course in India
ReplyDeleteMelbourne Motorcycle Accident Attorney Very interesting topic will bookmark your site to check if you write more about in the future.
ReplyDeleteWe are offering high-quality Wordpress Plugins, Android IOS, Website Design And Development at most cost effective and affordable rates. Company About Us
ReplyDeleteI am really impressed with your efforts and really pleased to visit this post.safety courses in chennai|Nebosh courses in chennai|IOSH courses in chennai|safety courses in chennai|Diploma in safety courses training institute in chennai
ReplyDeleteExcellent post. Happy to visit your post. Thanks for sharing.
ReplyDeleteweb design company in chennai
Excellant content. To know the details and importance of python course visit below. Python is an object oriented high level programming language which is built in data structures combined with dynamic typing and dynamic binding making it very attractive for rapid application development.
ReplyDeletePython Training in Chennai | Python Course in Chennai
ReplyDeleteReally awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog
http://hadooptraininginhyderabad.co.in/salesforce-training-in-hyderabad/
what size dumpster do i need Established in 2003, EnviroSolutions is a vertically integrated solid waste collection, disposal and recycling company located in the Northeast and Mid-Atlantic regions of the United States.
ReplyDeleteSyntax:
ReplyDeletegrails application development
Feeling Excited After Visting Blog,Great Posting about Grails functional tests.
Rolex Watches Authentic Mens & Ladies Rolex Datejust, President Watches for Sale at JavyEstrella.com.
ReplyDeleteThanks for Sharing the valuable information and thanks for sharing the wonderful article..We are glad to see such a wonderful article..
ReplyDeleteQTP Training in Chennai | QTP Training Institute in Chennai | QTP Training
Thanks for Sharing the valuable information and thanks for sharing the wonderful article..We are glad to see such a wonderful article..
ReplyDeletesas training in chennai
nice posting....
ReplyDeletebase-sas training in chennai
Excellent post. Happy to visit your post. Thanks for sharing.
ReplyDeletemsbi training in chennai
Great post!Thanks for sharing useful and very understanding information.
ReplyDeletesas training in chennai
Much obliged to you for requiring significant investment to give us a portion of the valuable and restrictive data with us.
ReplyDeleteRegards,
SAS Training in Chennai | SAS Course in Chennai | SAS Training Institute in Chennai
Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
ReplyDeleteRegards,'
QTP Training Institutes in Chennai | Selenium Training Institutes in Chennai
Very Informative post.. must be shared..
ReplyDeletePMP Training in Chennai | Primavera Training in Chennai
Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
ReplyDeletePHP Training in Chennai | PHP course in Chennai
Awesome Post! I like writing style, how you describing the topics throughout the post. I hope many web reader will keep reading your post at the end, Thanks for sharing your view.
ReplyDeleteRegards,
PHP Institutes in Chennai|PHP Training Center in Chennai
Good Information Eamonn O'Connell!! Really It will be more helpful for the beginners and also working professionals. I will try this and come back to you.
ReplyDeleteSelenium training in Chennai | Best Selenium training institute in Chennai
Such a great articles in my carrier, It's wonderful commands like easiest understand words of knowledge in information's.
ReplyDeleteSelenium Training in Chennai
ReplyDeleteGood and nice blog post, thanks for sharing your information.. it is very useful to me.. keep rocks and updating..
Software Testing Training in chennai
It is a very nice article including a lot of viral content. I am going to share it on social media. Get the fire crackers online in chennai.
ReplyDeleteThanks for your marvelous posting! It is very useful and good. Come on. I want to introduce an get app installs, I try it and I feel it is so good to rank app to top in app store search results, have you ever heard it?
ReplyDeleteInformative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post. Also great with all of the valuable information on mobile app and you are doing well.
ReplyDeleteMobile application developers in Chennai | Android application developers in Chennai
This is very good blog for learners, Thanks for sharing valuable content on MSBI Online Training
ReplyDeleteReally informative data on MSBI, looking for best msbi online training institute ?
ReplyDeleteIt was so good to read and useful to improve my knowledge as updated one.Thanks to Sharing.
ReplyDeleteInformatica Training In Chennai | Hadoop Training In Chennai | Sap MM Training In Chennai
Thanks for sharing this niche useful informative post to our knowledge.
ReplyDeletebrochure designers in chennai | brochure design company in chennai