groovy 如何将从Excel文件创建的列表小写

cygmwpex  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(160)

我已经在这个问题上纠结了好几天了。我不明白失败的原因。我用的是Katalon,我试着用Groovy作为主语言,但是对于一些函数我用的是Java。
我想做的是,首先我读取一个Excel文件,从一个列中获取值,然后我把它存储在一个字符串列表中,最后我想把这个列表小写。

  • 从excel中获取列表效果很好。
  • 将list更改为小写可以很好地工作。
  • 但由于某种原因,toLowerCase()拒绝在我从excel文件中得到的列表上工作。在任何其他列表上它都工作得很好。

这是主脚本(测试用例)

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
import static java.util.stream.Collectors.*
import java.lang.String as String

List<String> lst_ListFromExcel = new ArrayList()

//this works fine
lst_ListFromExcel = CustomKeywords.'com.utils.ExcelFiles.getCellValuesList'(xlFilePath, 0, 3)

//list printed correctly
WebUI.comment("lst_ListFromExcel = $lst_ListFromExcel")

//Copying list I got from excel to a new one. The first time it worked
List<String> resultsList = new ArrayList<String>(lst_ListFromExcel)

WebUI.comment("resultsList = $resultsList")

//Here is where I got the error. However, with any other list it works fine.
resultsList = resultsList.collect{it.toLowerCase()}

WebUI.comment("resultsList = $resultsList")

这是我创建的自定义关键字,用于获取列的值并将其存储在字符串列表中:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat
import java.text.SimpleDateFormat
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.apache.poi.xssf.usermodel.XSSFWorkbook

    // xlFilePath in this format C:\folder\folder\file.xlsx
    @Keyword
    def getCellValuesList(String xlFilePath, int sheetIndex, int columnIndex) {
        List<String> rsltValuesList = new ArrayList<>()
        def count = 0
        int column_index_1 = 0;
        File file = new File(xlFilePath);
        Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
        Sheet sheet = workbook.getSheetAt(sheetIndex);
        column_index_1 = columnIndex
        Row row = sheet.getRow(0);
        for (Row r: sheet) {
            if (r.getRowNum() == 0) continue; //hearders
            Cell c_1 = r.getCell(column_index_1);
            if (c_1 != null && c_1.getCellType() != Cell.CELL_TYPE_BLANK) {
                System.out.print("  " + c_1 + "\n");
                rsltValuesList.add(c_1)
                count++
            }
        }
        WebUI.comment("Values count = $count")
        WebUI.comment("rsltValuesList = $rsltValuesList")
        return rsltValuesList
    }

最后,我得到的错误是这个。我只有在使用从excel文件中得到的列表时才得到这个错误。对于任何其他列表,这个都可以正常工作。

10-09-2022 08:33:27 PM resultsList = resultsList.collect({ -> ... })

Elapsed time: 0.058s

Test Cases/Temp/Temp-ToLowerCase FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: org.apache.poi.xssf.usermodel.XSSFCell.toLowerCase() is applicable for argument types: () values: []
    at Script1665186880146$_run_closure1.doCall(Script1665186880146.groovy:32)

我没有使用org.apache.poi.xssf.usermodel.XSSFCell,正如我所说的,这只发生在我从Excel文件中获得的列表上,即lst_ListFromExcelresultsList
我会很感激你的帮助

62lalag4

62lalag41#

添加toString()应该会有所帮助

resultsList.collect{it.toString().toLowerCase()}

相关问题