SOAPUI中Groovy脚本实现两文件夹同名XML指定标签对比求助
Hey there! Nice job getting the file lists pulled together already—you’re halfway there. Let’s expand your code to add the XML tag comparison logic you need. Here’s how to do it step by step:
Step 1: Filter for matching filenames
First, we need to find XML files that exist in both folders. Instead of just checking if the full lists are identical, we’ll grab the intersection of the two file lists to get only the common filenames.
Step 2: Define your target tag
You’ll need to specify the XPath of the XML tag you want to compare. For example, if you’re checking a tag like <order><id>123</id></order>, your XPath would be //order/id.
Step 3: Compare tags for each matching file
We’ll use SoapUI’s built-in XmlHolder class (super handy for parsing XML and running XPath queries) to extract the tag values from each file, then compare them and log any differences.
Here’s the full updated code:
def fileList1 = [] File folder1 = new File("Path of basefiles") folder1.eachFileRecurse FileType.FILES, { f -> if (f.isFile() && f.name.endsWith('.xml')) { def filename1 = f.name // Simplified—no need for [0..-1] since it's the full filename fileList1.add(filename1) log.info "Base file found: ${filename1}" } } if (fileList1.size() < 1) { testRunner.fail("No files found in base folder") } def fileList2 = [] File folder2 = new File("Path of response files") folder2.eachFileRecurse FileType.FILES, { f -> if (f.isFile() && f.name.endsWith('.xml')) { def filename2 = f.name // Simplified here too fileList2.add(filename2) log.info "Response file found: ${filename2}" } } if (fileList2.size() < 1) { testRunner.fail("No files found in response folder") } // Get only filenames present in both folders def commonFiles = fileList1.intersect(fileList2) if (commonFiles.isEmpty()) { log.info "No matching XML files found between the two folders." return } // UPDATE THIS: Replace with your target tag's XPath def targetXPath = "//your/target/tag/path" commonFiles.each { filename -> try { // Get full paths to both files def baseFile = new File(folder1.path, filename) def responseFile = new File(folder2.path, filename) // Parse XML with SoapUI's XmlHolder def baseXmlHolder = new com.eviware.soapui.support.XmlHolder(baseFile.text) def responseXmlHolder = new com.eviware.soapui.support.XmlHolder(responseFile.text) // Extract tag values using XPath def baseValue = baseXmlHolder.getNodeValue(targetXPath) def responseValue = responseXmlHolder.getNodeValue(targetXPath) // Compare and log results if (baseValue == responseValue) { log.info "✅ File: ${filename} - Target tag values match: ${baseValue}" } else { log.error "❌ File: ${filename} - Target tag values differ!" log.error "Base file value: ${baseValue}" log.error "Response file value: ${responseValue}" } } catch (Exception e) { log.error "⚠️ Error processing file: ${filename}", e } }
Key Notes:
- Update the XPath: Make sure to replace
//your/target/tag/pathwith the actual XPath for the tag you want to compare. If you’re not sure how to write the XPath, you can use SoapUI’s XPath tester (in the request editor) to get the correct path. - Error Handling: The
try-catchblock will catch issues like missing files, invalid XML, or tags that don’t exist in a file, so you’ll get clear error logs instead of the script crashing. - Simplified Filename Handling: I removed the
[0..-1]sincef.namealready gives you the full filename—no need to slice the string.
This script will loop through all matching XML files, compare your specified tag, and log whether the values match or differ. Perfect for validating responses against base files!
内容的提问来源于stack exchange,提问作者Apoorva Odugoudar

