Book Image

SoapUI Cookbook

By : Rupert Anderson
Book Image

SoapUI Cookbook

By: Rupert Anderson

Overview of this book

Table of Contents (19 chapters)
SoapUI Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Testing files with Groovy


Sometimes, we'll need to test whether a web service has created a file or certain file content, for example, a log message. This recipe looks at a few ways to test file existence and content using Groovy. The examples are fairly simple, but hopefully effective enough for most needs!

Getting ready

I have provided a sample project FileTests in the chapter 4 workspace.

How to do it...

Let's start with checking whether a file exists at a given path:

def fileName = "/temp/new_invoices.txt"
def testFile = new File(fileName)
if (!testFile.exists()) testRunner.fail("File $fileName does not exist.")
  else log.info "File $fileName exists."

To check whether a file contains a given text string, use the following code:

def fileName = "/temp/catalina.2013-08-23.log"
def searchString = "o12345"

def testFile = new File(fileName)
def found = false
testFile.eachLine{line ->
  if (line.contains(searchString)) {
    log.info "Found in line: $line"
    found = true
  }
}
if (!found) ...