将 System.out.println 替换为 class groovy soapui 之外的变量

replace System.out.println with variable outside of class groovy soapui

我有来自 git 的以下代码,它在 eclipse 中 100% 有效。它在控制台上将 excel 文件输出为 Xml 格式。当我 copy/paste 将其放入 soapUI Groovy 脚本并点击播放时,我没有看到任何事情发生。我尝试用 log.info 替换 println 但无济于事,所以我尝试将 return 和 xml 放入变量中,但我不知道该怎么做这个,因为它看起来需要在 class 之外发生 --> 我对如何从 main 获取信息到测试用例 属性 感到困惑。我也尝试过使用 mainclass 内部的 testRunner 变量作为 getter 和 setter 但是......错误,错误和更多错误大声笑。

感谢任何反馈

import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.xssf.usermodel.*
import org.apache.poi.ss.util.*
import org.apache.poi.ss.usermodel.*
import java.io.*



class GroovyExcelParser {
  //http://poi.apache.org/spreadsheet/quick-guide.html#Iterator

  def parse(path) {
    InputStream inp = new FileInputStream(path)
    Workbook wb = WorkbookFactory.create(inp);

    Sheet sheet = wb.getSheetAt(0);

    Iterator<Row> rowIt = sheet.rowIterator()
    Row row = rowIt.next()
    def headers = getRowData(row)

    def rows = []
    while(rowIt.hasNext()) {
      row = rowIt.next()
      rows << getRowData(row)
    }
    [headers, rows]
  }

  def getRowData(Row row) {
    def data = []
    for (Cell cell : row) {
      getValue(row, cell, data)
    }
    data
  }

  def getRowReference(Row row, Cell cell) {
    def rowIndex = row.getRowNum()
    def colIndex = cell.getColumnIndex()
    CellReference ref = new CellReference(rowIndex, colIndex)
    ref.getRichStringCellValue().getString()
  }

  def getValue(Row row, Cell cell, List data) {
    def rowIndex = row.getRowNum()
    def colIndex = cell.getColumnIndex()
    def value = ""
    switch (cell.getCellType()) {
      case Cell.CELL_TYPE_STRING:
        value = cell.getRichStringCellValue().getString();
        break;
      case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            value = cell.getDateCellValue();
        } else {
            value = cell.getNumericCellValue().toInteger();
        }
        break;
      case Cell.CELL_TYPE_BOOLEAN:
        value = cell.getBooleanCellValue();
        break;
      case Cell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
      default:
        value = ""
    }
    data[colIndex] = value
    data
  }

  def toXml(header, row) {
    def obj = "<object>\n"
    row.eachWithIndex { datum, i ->
      def headerName = header[i]
      obj += "\t<$headerName>$datum</$headerName>\n"
    }
    obj += "</object>"
  }

  public static void main(String[]args) {

    def filename = 'RaNdOmxlsxFile.xslx'

    GroovyExcelParser parser = new GroovyExcelParser()

    def (headers, rows) = parser.parse(filename)
    System.out.println 'Headers'
    System.out.println '------------------'
    headers.each { header ->
      System.out.println header
    }
    System.out.println "\n"
    System.out.println 'Rows'
    System.out.println '------------------'
    rows.each { row ->
      System.out.println parser.toXml(headers, row)
    }
  }
}

基本上有多种方法可以达到同样的目的:

  1. 你应该可以在 soapui.
  2. 的日志文件中看到它
  3. 否则,打开命令提示符 -> 转到 SOAPUI_HOME/bin、运行 soapui.bat/.sh 文件和 运行 groovy 脚本你应该能够在命令 window 控制台中看到输出。
  4. 从 main 方法中复制代码并将其放在 class 之外,然后将 System.out.println() 替换为 log.info 并从 class 中删除 main 方法。然后运行groovy脚本.

希望您只想看到输出文本。