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.

About Sven Lange

I am a software engineer at Orientation in Objects in Mannheim (Germany) whose scope is mainly on web development related stuff.
This entry was posted in Grails, Testing, Webtest and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>