Thursday, January 5, 2012

Getting text of selected option in Geb

Geb has very nice function to get value of selected option, but I was not able to find method to get it's text. Fortunately, it is still possible using ugly Selenium API and can be nicely hidden behind Page Object. For example, this is what I have in my Page Object:

myField {$("#my") }
myFieldValue {  new Select($("#my").getElement(0)).getFirstSelectedOption().getText() }

And this is how it looks in Spec:

when:
myField.value('B')

then:
myFieldValue == 'B'




Friday, December 30, 2011

Check element existance in Geb

In Geb, if you need to check if some element does exists on page or not the best looking way to do it is method isPresent(), so it can be called like:

!notNeededLink.present
otherLink.present

Of course if you plan to check non-existing elements like this they need to be defined as not required and with wait - false. For example:

notNeededLink(to: SomePage, required:false, wait: false) {$('a', text:'Some')}

Tuesday, December 20, 2011

Too many idle database connections with Tomcat

I recently had problem when there were too many open database connections in Tomcat. Everything was right, but number is far more then is defined in Tomcat resources.
After some investigation, I have found that problem was that datasources were defined in wrong place - conf/context.xml, and when they are defined there, they are picked for every Web application that is defined in this Tomcat instance (including management and utility), therefore you will have all your connections multiplied by number of applications you have.
Fortunately, it is easy to fix by defining resources under conf/Catalina/localhost/ROOT.xml (ROOT if application is defined under root, otherwise, name of your application), in which case, resources will be provided only for specified application.

Tuesday, December 6, 2011

Passing upstream parameters to downstream builds in Jenkins

Jenkins has cool Pipeline plugin, which helps to create build pipelines, that for example, can organize and visualize deployment process. Plugin is very cool and looks great, but lacks few basic features, like passing generated parameters or variables from upstream builds to downstream.
For example, I wanted to pass SVN revision number into downstream builds.
Fortunately it is easy to do it with little hacking. Here is what I did.
  1. Add Groovy plugin.
  2. Add new build step Execute system Groovy script, now it is possible to hook into Jenkins internals.
  3. Then just add Groovy script there:
import hudson.model.*
def thr = Thread.currentThread()
def build = thr?.executable
build.addAction(new ParametersAction(new StringParameterValue('SVN_UPSTREAM', build.getEnvVars()['SVN_REVISION'])))


What this script does it just creates new parameter and adds SVN_REVISION from environment variables there. And pipeline can pass parameters with this mechanism to downstream builds (all of them).
Easy!

Monday, November 28, 2011

Encoding problems in Grails

If there are problems with charsets while submitting forms in Tomcat, and headers for responses/requests looks correct, you can try to fix it by adding URIEncoding="UTF-8" into your server.xml. This fixed things nicely to me. Read more about it at http://wiki.apache.org/tomcat/FAQ/CharacterEncoding.

Thursday, November 24, 2011

MySQL dump without locking tables

If you just do MySQL dump without any parameters, it locks tables and consumes a lot of memory. If you want to avoid it and you have InnoDB, best way to do it is to add single-transaction (run in single transaction) and q (dump one row at once) properties, for example like:

mysqldump -q --single-transaction -u login -ppassword db > dump.sql


Monday, November 14, 2011

Time difference with DST

If you need to calculate time difference that include Daylight Saving Time in Groovy, it is super simple with groovy.time.TimeCategory:

TimeDuration hours = TimeCategory.minus(end.time, start.time)