Using storeXPath() in your Grails Webtest

Today I had to figure out how to save some content from a site during a running Webtest so that I am able to restore all changed data afterwards. Luckily Webtest provides the step storeXPath() which does the job.

But it was not that easy to realize my task due to the lack of good documentation and finding the correct xpath expression.

The following code snippet grabs the text value from a text input field with the id surname and saves it in the ant property xyz.

def testSomething() {
  webtest('Some description') {
    // saving initial value
    storeXPath(xpath: "id('surname')/@value", property: "xyz")
    // restore initial value
    setInputField(name: "surname", value: "#{xyz}")

Pretty simple in the end, but it took a while till I got it. Maybe it helps someone.

Date published:
Date modified: 2008-07-11
Keywords: Grails, Testing, storeXPath, Webtest