Hope this is not a stupid question.
For Regression testing, I wrote a small tool which uses Selenium to bring up the screen, verifies the loaded screen data against the database (multiple tables) and generates a report with screenshots (in case of errors).
Being a lazy guy that I am, instead of writing one class for single use case (~60 use cases for a screen), I wrote one single class which can accept multiple configuration files as a parameter. The configuration file dictates the flow of the testcase (in steps), the mapping of the xpath/id of the form field against the database query, the queries etc.
It all works well but the problem is that the configuration file is an XML. Neighboring projects are interested and would like to use the tool and I want them to easily understand the tool and customize to their needs. XML, in my opinion, is inappropriate here. Besides, the mapping between screen form fields and database columns are the same for many testcases which use the same screen for different use case combination. It would be great if there could be inheritance there instead of copying the content.
So, I am hoping to write a small DSL which goes something like
open application
load editClient window
switchTo generalTab
verify generalTab{
if dataValidFor clientName then addInfoToReport else addErrorToReport
if dataValidFor clientAddress then addInfoToReport else addErrorToReport
if confidentialData visible then addInfoToReport else addErrorToReport
}
...
...
You get the idea. All I am planning to do is to translate the DSL to Java (or Groovy, if need be) method calls in the background. I understand that the requirement is not that huge to warrant a powerful library like Antlr. However, my experience with Groovy is very limited that I don’t even know whether it is possible in Groovy.
I referred to this presentation and it looks amazing. However, I am worried about the capability to include blocks in the DSL as in
verify generalTab{
...
}
PS : I am no expert in Lexers and Parsers (non-computer science under-grad) but I managed to self-learn ANTLR and played around with it for few weeks couple of years ago. Again, I have very little experience with Groovy.
I’m also no expert in groovy’s DSL, but have been playing with it a bit and i think your case is doable. But it is large.
Writing
Groovy seems to resolves to
so a way near what you want would be to intercept method missing call (‘generalTab’ seems like a html component id to me, please correct me if i’m wrong).
You will need: a
verify()method and amethodMissing()method.Your
ifandelses… uh, can we swap it forwhenandotherwise? Just to avoid groovy’s own reserved words 😉Those double words after the
ifmake the whole thing pretty ugly. It would be better if you could use a dot or just one word.Resolves to
Which will be weird to parse. It is better if you could do something like:
I did the following:
And it works. It prints (randomly):
It got pretty ugly. It’s more of a proof of concept. You need to separate the classes using BaseScripts, GroovyShell, delegate it to other classes, and the likes. You will also need to model it neatly, considering a class for reports and so. But so far, i think it’s doable. Rather large, though.
My reading suggestions:
Guillaume Laforge shows a script DSL for a robot in mars:
http://www.slideshare.net/glaforge/going-to-mars-with-groovy-domainspecific-languages
The art of Groovy’s command expressions:
http://www.canoo.com/blog/2011/12/08/the-art-of-groovy-command-expressions-in-dsls/
This is an email i sent to the groovy list today, once i managed to finish a DSL over JFugue for my personal use:
http://groovy.329449.n5.nabble.com/Method-chaining-in-DSL-Instructions-td5711254.html
It’s on github:
https://github.com/wpiasecki/glissando